1 //===- SelectionDAG.cpp - Implement the SelectionDAG data structures ------===//
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 SelectionDAG class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/CodeGen/SelectionDAG.h"
14 #include "SDNodeDbgValue.h"
15 #include "llvm/ADT/APFloat.h"
16 #include "llvm/ADT/APInt.h"
17 #include "llvm/ADT/APSInt.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/BitVector.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/None.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/Triple.h"
26 #include "llvm/ADT/Twine.h"
27 #include "llvm/Analysis/BlockFrequencyInfo.h"
28 #include "llvm/Analysis/MemoryLocation.h"
29 #include "llvm/Analysis/ProfileSummaryInfo.h"
30 #include "llvm/Analysis/ValueTracking.h"
31 #include "llvm/CodeGen/FunctionLoweringInfo.h"
32 #include "llvm/CodeGen/ISDOpcodes.h"
33 #include "llvm/CodeGen/MachineBasicBlock.h"
34 #include "llvm/CodeGen/MachineConstantPool.h"
35 #include "llvm/CodeGen/MachineFrameInfo.h"
36 #include "llvm/CodeGen/MachineFunction.h"
37 #include "llvm/CodeGen/MachineMemOperand.h"
38 #include "llvm/CodeGen/RuntimeLibcalls.h"
39 #include "llvm/CodeGen/SelectionDAGAddressAnalysis.h"
40 #include "llvm/CodeGen/SelectionDAGNodes.h"
41 #include "llvm/CodeGen/SelectionDAGTargetInfo.h"
42 #include "llvm/CodeGen/TargetFrameLowering.h"
43 #include "llvm/CodeGen/TargetLowering.h"
44 #include "llvm/CodeGen/TargetRegisterInfo.h"
45 #include "llvm/CodeGen/TargetSubtargetInfo.h"
46 #include "llvm/CodeGen/ValueTypes.h"
47 #include "llvm/IR/Constant.h"
48 #include "llvm/IR/Constants.h"
49 #include "llvm/IR/DataLayout.h"
50 #include "llvm/IR/DebugInfoMetadata.h"
51 #include "llvm/IR/DebugLoc.h"
52 #include "llvm/IR/DerivedTypes.h"
53 #include "llvm/IR/Function.h"
54 #include "llvm/IR/GlobalValue.h"
55 #include "llvm/IR/Metadata.h"
56 #include "llvm/IR/Type.h"
57 #include "llvm/IR/Value.h"
58 #include "llvm/Support/Casting.h"
59 #include "llvm/Support/CodeGen.h"
60 #include "llvm/Support/Compiler.h"
61 #include "llvm/Support/Debug.h"
62 #include "llvm/Support/ErrorHandling.h"
63 #include "llvm/Support/KnownBits.h"
64 #include "llvm/Support/MachineValueType.h"
65 #include "llvm/Support/ManagedStatic.h"
66 #include "llvm/Support/MathExtras.h"
67 #include "llvm/Support/Mutex.h"
68 #include "llvm/Support/raw_ostream.h"
69 #include "llvm/Target/TargetMachine.h"
70 #include "llvm/Target/TargetOptions.h"
71 #include "llvm/Transforms/Utils/SizeOpts.h"
72 #include <algorithm>
73 #include <cassert>
74 #include <cstdint>
75 #include <cstdlib>
76 #include <limits>
77 #include <set>
78 #include <string>
79 #include <utility>
80 #include <vector>
81 
82 using namespace llvm;
83 
84 /// makeVTList - Return an instance of the SDVTList struct initialized with the
85 /// specified members.
86 static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
87   SDVTList Res = {VTs, NumVTs};
88   return Res;
89 }
90 
91 // Default null implementations of the callbacks.
92 void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
93 void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
94 void SelectionDAG::DAGUpdateListener::NodeInserted(SDNode *) {}
95 
96 void SelectionDAG::DAGNodeDeletedListener::anchor() {}
97 
98 #define DEBUG_TYPE "selectiondag"
99 
100 static cl::opt<bool> EnableMemCpyDAGOpt("enable-memcpy-dag-opt",
101        cl::Hidden, cl::init(true),
102        cl::desc("Gang up loads and stores generated by inlining of memcpy"));
103 
104 static cl::opt<int> MaxLdStGlue("ldstmemcpy-glue-max",
105        cl::desc("Number limit for gluing ld/st of memcpy."),
106        cl::Hidden, cl::init(0));
107 
108 static void NewSDValueDbgMsg(SDValue V, StringRef Msg, SelectionDAG *G) {
109   LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
110 }
111 
112 //===----------------------------------------------------------------------===//
113 //                              ConstantFPSDNode Class
114 //===----------------------------------------------------------------------===//
115 
116 /// isExactlyValue - We don't rely on operator== working on double values, as
117 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
118 /// As such, this method can be used to do an exact bit-for-bit comparison of
119 /// two floating point values.
120 bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
121   return getValueAPF().bitwiseIsEqual(V);
122 }
123 
124 bool ConstantFPSDNode::isValueValidForType(EVT VT,
125                                            const APFloat& Val) {
126   assert(VT.isFloatingPoint() && "Can only convert between FP types");
127 
128   // convert modifies in place, so make a copy.
129   APFloat Val2 = APFloat(Val);
130   bool losesInfo;
131   (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
132                       APFloat::rmNearestTiesToEven,
133                       &losesInfo);
134   return !losesInfo;
135 }
136 
137 //===----------------------------------------------------------------------===//
138 //                              ISD Namespace
139 //===----------------------------------------------------------------------===//
140 
141 bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
142   if (N->getOpcode() == ISD::SPLAT_VECTOR) {
143     unsigned EltSize =
144         N->getValueType(0).getVectorElementType().getSizeInBits();
145     if (auto *Op0 = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
146       SplatVal = Op0->getAPIntValue().truncOrSelf(EltSize);
147       return true;
148     }
149     if (auto *Op0 = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) {
150       SplatVal = Op0->getValueAPF().bitcastToAPInt().truncOrSelf(EltSize);
151       return true;
152     }
153   }
154 
155   auto *BV = dyn_cast<BuildVectorSDNode>(N);
156   if (!BV)
157     return false;
158 
159   APInt SplatUndef;
160   unsigned SplatBitSize;
161   bool HasUndefs;
162   unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
163   return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
164                              EltSize) &&
165          EltSize == SplatBitSize;
166 }
167 
168 // FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
169 // specializations of the more general isConstantSplatVector()?
170 
171 bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
172   // Look through a bit convert.
173   while (N->getOpcode() == ISD::BITCAST)
174     N = N->getOperand(0).getNode();
175 
176   if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
177     APInt SplatVal;
178     return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnesValue();
179   }
180 
181   if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
182 
183   unsigned i = 0, e = N->getNumOperands();
184 
185   // Skip over all of the undef values.
186   while (i != e && N->getOperand(i).isUndef())
187     ++i;
188 
189   // Do not accept an all-undef vector.
190   if (i == e) return false;
191 
192   // Do not accept build_vectors that aren't all constants or which have non-~0
193   // elements. We have to be a bit careful here, as the type of the constant
194   // may not be the same as the type of the vector elements due to type
195   // legalization (the elements are promoted to a legal type for the target and
196   // a vector of a type may be legal when the base element type is not).
197   // We only want to check enough bits to cover the vector elements, because
198   // we care if the resultant vector is all ones, not whether the individual
199   // constants are.
200   SDValue NotZero = N->getOperand(i);
201   unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
202   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
203     if (CN->getAPIntValue().countTrailingOnes() < EltSize)
204       return false;
205   } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
206     if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
207       return false;
208   } else
209     return false;
210 
211   // Okay, we have at least one ~0 value, check to see if the rest match or are
212   // undefs. Even with the above element type twiddling, this should be OK, as
213   // the same type legalization should have applied to all the elements.
214   for (++i; i != e; ++i)
215     if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
216       return false;
217   return true;
218 }
219 
220 bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
221   // Look through a bit convert.
222   while (N->getOpcode() == ISD::BITCAST)
223     N = N->getOperand(0).getNode();
224 
225   if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
226     APInt SplatVal;
227     return isConstantSplatVector(N, SplatVal) && SplatVal.isNullValue();
228   }
229 
230   if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
231 
232   bool IsAllUndef = true;
233   for (const SDValue &Op : N->op_values()) {
234     if (Op.isUndef())
235       continue;
236     IsAllUndef = false;
237     // Do not accept build_vectors that aren't all constants or which have non-0
238     // elements. We have to be a bit careful here, as the type of the constant
239     // may not be the same as the type of the vector elements due to type
240     // legalization (the elements are promoted to a legal type for the target
241     // and a vector of a type may be legal when the base element type is not).
242     // We only want to check enough bits to cover the vector elements, because
243     // we care if the resultant vector is all zeros, not whether the individual
244     // constants are.
245     unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
246     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
247       if (CN->getAPIntValue().countTrailingZeros() < EltSize)
248         return false;
249     } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Op)) {
250       if (CFPN->getValueAPF().bitcastToAPInt().countTrailingZeros() < EltSize)
251         return false;
252     } else
253       return false;
254   }
255 
256   // Do not accept an all-undef vector.
257   if (IsAllUndef)
258     return false;
259   return true;
260 }
261 
262 bool ISD::isBuildVectorAllOnes(const SDNode *N) {
263   return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
264 }
265 
266 bool ISD::isBuildVectorAllZeros(const SDNode *N) {
267   return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
268 }
269 
270 bool ISD::isBuildVectorOfConstantSDNodes(const SDNode *N) {
271   if (N->getOpcode() != ISD::BUILD_VECTOR)
272     return false;
273 
274   for (const SDValue &Op : N->op_values()) {
275     if (Op.isUndef())
276       continue;
277     if (!isa<ConstantSDNode>(Op))
278       return false;
279   }
280   return true;
281 }
282 
283 bool ISD::isBuildVectorOfConstantFPSDNodes(const SDNode *N) {
284   if (N->getOpcode() != ISD::BUILD_VECTOR)
285     return false;
286 
287   for (const SDValue &Op : N->op_values()) {
288     if (Op.isUndef())
289       continue;
290     if (!isa<ConstantFPSDNode>(Op))
291       return false;
292   }
293   return true;
294 }
295 
296 bool ISD::allOperandsUndef(const SDNode *N) {
297   // Return false if the node has no operands.
298   // This is "logically inconsistent" with the definition of "all" but
299   // is probably the desired behavior.
300   if (N->getNumOperands() == 0)
301     return false;
302   return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
303 }
304 
305 bool ISD::matchUnaryPredicate(SDValue Op,
306                               std::function<bool(ConstantSDNode *)> Match,
307                               bool AllowUndefs) {
308   // FIXME: Add support for scalar UNDEF cases?
309   if (auto *Cst = dyn_cast<ConstantSDNode>(Op))
310     return Match(Cst);
311 
312   // FIXME: Add support for vector UNDEF cases?
313   if (ISD::BUILD_VECTOR != Op.getOpcode() &&
314       ISD::SPLAT_VECTOR != Op.getOpcode())
315     return false;
316 
317   EVT SVT = Op.getValueType().getScalarType();
318   for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
319     if (AllowUndefs && Op.getOperand(i).isUndef()) {
320       if (!Match(nullptr))
321         return false;
322       continue;
323     }
324 
325     auto *Cst = dyn_cast<ConstantSDNode>(Op.getOperand(i));
326     if (!Cst || Cst->getValueType(0) != SVT || !Match(Cst))
327       return false;
328   }
329   return true;
330 }
331 
332 bool ISD::matchBinaryPredicate(
333     SDValue LHS, SDValue RHS,
334     std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
335     bool AllowUndefs, bool AllowTypeMismatch) {
336   if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
337     return false;
338 
339   // TODO: Add support for scalar UNDEF cases?
340   if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
341     if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
342       return Match(LHSCst, RHSCst);
343 
344   // TODO: Add support for vector UNDEF cases?
345   if (ISD::BUILD_VECTOR != LHS.getOpcode() ||
346       ISD::BUILD_VECTOR != RHS.getOpcode())
347     return false;
348 
349   EVT SVT = LHS.getValueType().getScalarType();
350   for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
351     SDValue LHSOp = LHS.getOperand(i);
352     SDValue RHSOp = RHS.getOperand(i);
353     bool LHSUndef = AllowUndefs && LHSOp.isUndef();
354     bool RHSUndef = AllowUndefs && RHSOp.isUndef();
355     auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
356     auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
357     if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
358       return false;
359     if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
360                                LHSOp.getValueType() != RHSOp.getValueType()))
361       return false;
362     if (!Match(LHSCst, RHSCst))
363       return false;
364   }
365   return true;
366 }
367 
368 ISD::NodeType ISD::getVecReduceBaseOpcode(unsigned VecReduceOpcode) {
369   switch (VecReduceOpcode) {
370   default:
371     llvm_unreachable("Expected VECREDUCE opcode");
372   case ISD::VECREDUCE_FADD:
373   case ISD::VECREDUCE_SEQ_FADD:
374     return ISD::FADD;
375   case ISD::VECREDUCE_FMUL:
376   case ISD::VECREDUCE_SEQ_FMUL:
377     return ISD::FMUL;
378   case ISD::VECREDUCE_ADD:
379     return ISD::ADD;
380   case ISD::VECREDUCE_MUL:
381     return ISD::MUL;
382   case ISD::VECREDUCE_AND:
383     return ISD::AND;
384   case ISD::VECREDUCE_OR:
385     return ISD::OR;
386   case ISD::VECREDUCE_XOR:
387     return ISD::XOR;
388   case ISD::VECREDUCE_SMAX:
389     return ISD::SMAX;
390   case ISD::VECREDUCE_SMIN:
391     return ISD::SMIN;
392   case ISD::VECREDUCE_UMAX:
393     return ISD::UMAX;
394   case ISD::VECREDUCE_UMIN:
395     return ISD::UMIN;
396   case ISD::VECREDUCE_FMAX:
397     return ISD::FMAXNUM;
398   case ISD::VECREDUCE_FMIN:
399     return ISD::FMINNUM;
400   }
401 }
402 
403 bool ISD::isVPOpcode(unsigned Opcode) {
404   switch (Opcode) {
405   default:
406     return false;
407 #define BEGIN_REGISTER_VP_SDNODE(SDOPC, ...)                                   \
408   case ISD::SDOPC:                                                             \
409     return true;
410 #include "llvm/IR/VPIntrinsics.def"
411   }
412 }
413 
414 /// The operand position of the vector mask.
415 Optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
416   switch (Opcode) {
417   default:
418     return None;
419 #define BEGIN_REGISTER_VP_SDNODE(SDOPC, LEGALPOS, TDNAME, MASKPOS, ...)        \
420   case ISD::SDOPC:                                                             \
421     return MASKPOS;
422 #include "llvm/IR/VPIntrinsics.def"
423   }
424 }
425 
426 /// The operand position of the explicit vector length parameter.
427 Optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
428   switch (Opcode) {
429   default:
430     return None;
431 #define BEGIN_REGISTER_VP_SDNODE(SDOPC, LEGALPOS, TDNAME, MASKPOS, EVLPOS)     \
432   case ISD::SDOPC:                                                             \
433     return EVLPOS;
434 #include "llvm/IR/VPIntrinsics.def"
435   }
436 }
437 
438 ISD::NodeType ISD::getExtForLoadExtType(bool IsFP, ISD::LoadExtType ExtType) {
439   switch (ExtType) {
440   case ISD::EXTLOAD:
441     return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
442   case ISD::SEXTLOAD:
443     return ISD::SIGN_EXTEND;
444   case ISD::ZEXTLOAD:
445     return ISD::ZERO_EXTEND;
446   default:
447     break;
448   }
449 
450   llvm_unreachable("Invalid LoadExtType");
451 }
452 
453 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
454   // To perform this operation, we just need to swap the L and G bits of the
455   // operation.
456   unsigned OldL = (Operation >> 2) & 1;
457   unsigned OldG = (Operation >> 1) & 1;
458   return ISD::CondCode((Operation & ~6) |  // Keep the N, U, E bits
459                        (OldL << 1) |       // New G bit
460                        (OldG << 2));       // New L bit.
461 }
462 
463 static ISD::CondCode getSetCCInverseImpl(ISD::CondCode Op, bool isIntegerLike) {
464   unsigned Operation = Op;
465   if (isIntegerLike)
466     Operation ^= 7;   // Flip L, G, E bits, but not U.
467   else
468     Operation ^= 15;  // Flip all of the condition bits.
469 
470   if (Operation > ISD::SETTRUE2)
471     Operation &= ~8;  // Don't let N and U bits get set.
472 
473   return ISD::CondCode(Operation);
474 }
475 
476 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, EVT Type) {
477   return getSetCCInverseImpl(Op, Type.isInteger());
478 }
479 
480 ISD::CondCode ISD::GlobalISel::getSetCCInverse(ISD::CondCode Op,
481                                                bool isIntegerLike) {
482   return getSetCCInverseImpl(Op, isIntegerLike);
483 }
484 
485 /// For an integer comparison, return 1 if the comparison is a signed operation
486 /// and 2 if the result is an unsigned comparison. Return zero if the operation
487 /// does not depend on the sign of the input (setne and seteq).
488 static int isSignedOp(ISD::CondCode Opcode) {
489   switch (Opcode) {
490   default: llvm_unreachable("Illegal integer setcc operation!");
491   case ISD::SETEQ:
492   case ISD::SETNE: return 0;
493   case ISD::SETLT:
494   case ISD::SETLE:
495   case ISD::SETGT:
496   case ISD::SETGE: return 1;
497   case ISD::SETULT:
498   case ISD::SETULE:
499   case ISD::SETUGT:
500   case ISD::SETUGE: return 2;
501   }
502 }
503 
504 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
505                                        EVT Type) {
506   bool IsInteger = Type.isInteger();
507   if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
508     // Cannot fold a signed integer setcc with an unsigned integer setcc.
509     return ISD::SETCC_INVALID;
510 
511   unsigned Op = Op1 | Op2;  // Combine all of the condition bits.
512 
513   // If the N and U bits get set, then the resultant comparison DOES suddenly
514   // care about orderedness, and it is true when ordered.
515   if (Op > ISD::SETTRUE2)
516     Op &= ~16;     // Clear the U bit if the N bit is set.
517 
518   // Canonicalize illegal integer setcc's.
519   if (IsInteger && Op == ISD::SETUNE)  // e.g. SETUGT | SETULT
520     Op = ISD::SETNE;
521 
522   return ISD::CondCode(Op);
523 }
524 
525 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
526                                         EVT Type) {
527   bool IsInteger = Type.isInteger();
528   if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
529     // Cannot fold a signed setcc with an unsigned setcc.
530     return ISD::SETCC_INVALID;
531 
532   // Combine all of the condition bits.
533   ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
534 
535   // Canonicalize illegal integer setcc's.
536   if (IsInteger) {
537     switch (Result) {
538     default: break;
539     case ISD::SETUO : Result = ISD::SETFALSE; break;  // SETUGT & SETULT
540     case ISD::SETOEQ:                                 // SETEQ  & SETU[LG]E
541     case ISD::SETUEQ: Result = ISD::SETEQ   ; break;  // SETUGE & SETULE
542     case ISD::SETOLT: Result = ISD::SETULT  ; break;  // SETULT & SETNE
543     case ISD::SETOGT: Result = ISD::SETUGT  ; break;  // SETUGT & SETNE
544     }
545   }
546 
547   return Result;
548 }
549 
550 //===----------------------------------------------------------------------===//
551 //                           SDNode Profile Support
552 //===----------------------------------------------------------------------===//
553 
554 /// AddNodeIDOpcode - Add the node opcode to the NodeID data.
555 static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC)  {
556   ID.AddInteger(OpC);
557 }
558 
559 /// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
560 /// solely with their pointer.
561 static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
562   ID.AddPointer(VTList.VTs);
563 }
564 
565 /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
566 static void AddNodeIDOperands(FoldingSetNodeID &ID,
567                               ArrayRef<SDValue> Ops) {
568   for (auto& Op : Ops) {
569     ID.AddPointer(Op.getNode());
570     ID.AddInteger(Op.getResNo());
571   }
572 }
573 
574 /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
575 static void AddNodeIDOperands(FoldingSetNodeID &ID,
576                               ArrayRef<SDUse> Ops) {
577   for (auto& Op : Ops) {
578     ID.AddPointer(Op.getNode());
579     ID.AddInteger(Op.getResNo());
580   }
581 }
582 
583 static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned short OpC,
584                           SDVTList VTList, ArrayRef<SDValue> OpList) {
585   AddNodeIDOpcode(ID, OpC);
586   AddNodeIDValueTypes(ID, VTList);
587   AddNodeIDOperands(ID, OpList);
588 }
589 
590 /// If this is an SDNode with special info, add this info to the NodeID data.
591 static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
592   switch (N->getOpcode()) {
593   case ISD::TargetExternalSymbol:
594   case ISD::ExternalSymbol:
595   case ISD::MCSymbol:
596     llvm_unreachable("Should only be used on nodes with operands");
597   default: break;  // Normal nodes don't need extra info.
598   case ISD::TargetConstant:
599   case ISD::Constant: {
600     const ConstantSDNode *C = cast<ConstantSDNode>(N);
601     ID.AddPointer(C->getConstantIntValue());
602     ID.AddBoolean(C->isOpaque());
603     break;
604   }
605   case ISD::TargetConstantFP:
606   case ISD::ConstantFP:
607     ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
608     break;
609   case ISD::TargetGlobalAddress:
610   case ISD::GlobalAddress:
611   case ISD::TargetGlobalTLSAddress:
612   case ISD::GlobalTLSAddress: {
613     const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
614     ID.AddPointer(GA->getGlobal());
615     ID.AddInteger(GA->getOffset());
616     ID.AddInteger(GA->getTargetFlags());
617     break;
618   }
619   case ISD::BasicBlock:
620     ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
621     break;
622   case ISD::Register:
623     ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
624     break;
625   case ISD::RegisterMask:
626     ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
627     break;
628   case ISD::SRCVALUE:
629     ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
630     break;
631   case ISD::FrameIndex:
632   case ISD::TargetFrameIndex:
633     ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
634     break;
635   case ISD::LIFETIME_START:
636   case ISD::LIFETIME_END:
637     if (cast<LifetimeSDNode>(N)->hasOffset()) {
638       ID.AddInteger(cast<LifetimeSDNode>(N)->getSize());
639       ID.AddInteger(cast<LifetimeSDNode>(N)->getOffset());
640     }
641     break;
642   case ISD::PSEUDO_PROBE:
643     ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
644     ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
645     ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
646     break;
647   case ISD::JumpTable:
648   case ISD::TargetJumpTable:
649     ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
650     ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
651     break;
652   case ISD::ConstantPool:
653   case ISD::TargetConstantPool: {
654     const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
655     ID.AddInteger(CP->getAlign().value());
656     ID.AddInteger(CP->getOffset());
657     if (CP->isMachineConstantPoolEntry())
658       CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
659     else
660       ID.AddPointer(CP->getConstVal());
661     ID.AddInteger(CP->getTargetFlags());
662     break;
663   }
664   case ISD::TargetIndex: {
665     const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
666     ID.AddInteger(TI->getIndex());
667     ID.AddInteger(TI->getOffset());
668     ID.AddInteger(TI->getTargetFlags());
669     break;
670   }
671   case ISD::LOAD: {
672     const LoadSDNode *LD = cast<LoadSDNode>(N);
673     ID.AddInteger(LD->getMemoryVT().getRawBits());
674     ID.AddInteger(LD->getRawSubclassData());
675     ID.AddInteger(LD->getPointerInfo().getAddrSpace());
676     break;
677   }
678   case ISD::STORE: {
679     const StoreSDNode *ST = cast<StoreSDNode>(N);
680     ID.AddInteger(ST->getMemoryVT().getRawBits());
681     ID.AddInteger(ST->getRawSubclassData());
682     ID.AddInteger(ST->getPointerInfo().getAddrSpace());
683     break;
684   }
685   case ISD::MLOAD: {
686     const MaskedLoadSDNode *MLD = cast<MaskedLoadSDNode>(N);
687     ID.AddInteger(MLD->getMemoryVT().getRawBits());
688     ID.AddInteger(MLD->getRawSubclassData());
689     ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
690     break;
691   }
692   case ISD::MSTORE: {
693     const MaskedStoreSDNode *MST = cast<MaskedStoreSDNode>(N);
694     ID.AddInteger(MST->getMemoryVT().getRawBits());
695     ID.AddInteger(MST->getRawSubclassData());
696     ID.AddInteger(MST->getPointerInfo().getAddrSpace());
697     break;
698   }
699   case ISD::MGATHER: {
700     const MaskedGatherSDNode *MG = cast<MaskedGatherSDNode>(N);
701     ID.AddInteger(MG->getMemoryVT().getRawBits());
702     ID.AddInteger(MG->getRawSubclassData());
703     ID.AddInteger(MG->getPointerInfo().getAddrSpace());
704     break;
705   }
706   case ISD::MSCATTER: {
707     const MaskedScatterSDNode *MS = cast<MaskedScatterSDNode>(N);
708     ID.AddInteger(MS->getMemoryVT().getRawBits());
709     ID.AddInteger(MS->getRawSubclassData());
710     ID.AddInteger(MS->getPointerInfo().getAddrSpace());
711     break;
712   }
713   case ISD::ATOMIC_CMP_SWAP:
714   case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
715   case ISD::ATOMIC_SWAP:
716   case ISD::ATOMIC_LOAD_ADD:
717   case ISD::ATOMIC_LOAD_SUB:
718   case ISD::ATOMIC_LOAD_AND:
719   case ISD::ATOMIC_LOAD_CLR:
720   case ISD::ATOMIC_LOAD_OR:
721   case ISD::ATOMIC_LOAD_XOR:
722   case ISD::ATOMIC_LOAD_NAND:
723   case ISD::ATOMIC_LOAD_MIN:
724   case ISD::ATOMIC_LOAD_MAX:
725   case ISD::ATOMIC_LOAD_UMIN:
726   case ISD::ATOMIC_LOAD_UMAX:
727   case ISD::ATOMIC_LOAD:
728   case ISD::ATOMIC_STORE: {
729     const AtomicSDNode *AT = cast<AtomicSDNode>(N);
730     ID.AddInteger(AT->getMemoryVT().getRawBits());
731     ID.AddInteger(AT->getRawSubclassData());
732     ID.AddInteger(AT->getPointerInfo().getAddrSpace());
733     break;
734   }
735   case ISD::PREFETCH: {
736     const MemSDNode *PF = cast<MemSDNode>(N);
737     ID.AddInteger(PF->getPointerInfo().getAddrSpace());
738     break;
739   }
740   case ISD::VECTOR_SHUFFLE: {
741     const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
742     for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
743          i != e; ++i)
744       ID.AddInteger(SVN->getMaskElt(i));
745     break;
746   }
747   case ISD::TargetBlockAddress:
748   case ISD::BlockAddress: {
749     const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
750     ID.AddPointer(BA->getBlockAddress());
751     ID.AddInteger(BA->getOffset());
752     ID.AddInteger(BA->getTargetFlags());
753     break;
754   }
755   } // end switch (N->getOpcode())
756 
757   // Target specific memory nodes could also have address spaces to check.
758   if (N->isTargetMemoryOpcode())
759     ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
760 }
761 
762 /// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
763 /// data.
764 static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
765   AddNodeIDOpcode(ID, N->getOpcode());
766   // Add the return value info.
767   AddNodeIDValueTypes(ID, N->getVTList());
768   // Add the operand info.
769   AddNodeIDOperands(ID, N->ops());
770 
771   // Handle SDNode leafs with special info.
772   AddNodeIDCustom(ID, N);
773 }
774 
775 //===----------------------------------------------------------------------===//
776 //                              SelectionDAG Class
777 //===----------------------------------------------------------------------===//
778 
779 /// doNotCSE - Return true if CSE should not be performed for this node.
780 static bool doNotCSE(SDNode *N) {
781   if (N->getValueType(0) == MVT::Glue)
782     return true; // Never CSE anything that produces a flag.
783 
784   switch (N->getOpcode()) {
785   default: break;
786   case ISD::HANDLENODE:
787   case ISD::EH_LABEL:
788     return true;   // Never CSE these nodes.
789   }
790 
791   // Check that remaining values produced are not flags.
792   for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
793     if (N->getValueType(i) == MVT::Glue)
794       return true; // Never CSE anything that produces a flag.
795 
796   return false;
797 }
798 
799 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
800 /// SelectionDAG.
801 void SelectionDAG::RemoveDeadNodes() {
802   // Create a dummy node (which is not added to allnodes), that adds a reference
803   // to the root node, preventing it from being deleted.
804   HandleSDNode Dummy(getRoot());
805 
806   SmallVector<SDNode*, 128> DeadNodes;
807 
808   // Add all obviously-dead nodes to the DeadNodes worklist.
809   for (SDNode &Node : allnodes())
810     if (Node.use_empty())
811       DeadNodes.push_back(&Node);
812 
813   RemoveDeadNodes(DeadNodes);
814 
815   // If the root changed (e.g. it was a dead load, update the root).
816   setRoot(Dummy.getValue());
817 }
818 
819 /// RemoveDeadNodes - This method deletes the unreachable nodes in the
820 /// given list, and any nodes that become unreachable as a result.
821 void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
822 
823   // Process the worklist, deleting the nodes and adding their uses to the
824   // worklist.
825   while (!DeadNodes.empty()) {
826     SDNode *N = DeadNodes.pop_back_val();
827     // Skip to next node if we've already managed to delete the node. This could
828     // happen if replacing a node causes a node previously added to the node to
829     // be deleted.
830     if (N->getOpcode() == ISD::DELETED_NODE)
831       continue;
832 
833     for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
834       DUL->NodeDeleted(N, nullptr);
835 
836     // Take the node out of the appropriate CSE map.
837     RemoveNodeFromCSEMaps(N);
838 
839     // Next, brutally remove the operand list.  This is safe to do, as there are
840     // no cycles in the graph.
841     for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
842       SDUse &Use = *I++;
843       SDNode *Operand = Use.getNode();
844       Use.set(SDValue());
845 
846       // Now that we removed this operand, see if there are no uses of it left.
847       if (Operand->use_empty())
848         DeadNodes.push_back(Operand);
849     }
850 
851     DeallocateNode(N);
852   }
853 }
854 
855 void SelectionDAG::RemoveDeadNode(SDNode *N){
856   SmallVector<SDNode*, 16> DeadNodes(1, N);
857 
858   // Create a dummy node that adds a reference to the root node, preventing
859   // it from being deleted.  (This matters if the root is an operand of the
860   // dead node.)
861   HandleSDNode Dummy(getRoot());
862 
863   RemoveDeadNodes(DeadNodes);
864 }
865 
866 void SelectionDAG::DeleteNode(SDNode *N) {
867   // First take this out of the appropriate CSE map.
868   RemoveNodeFromCSEMaps(N);
869 
870   // Finally, remove uses due to operands of this node, remove from the
871   // AllNodes list, and delete the node.
872   DeleteNodeNotInCSEMaps(N);
873 }
874 
875 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
876   assert(N->getIterator() != AllNodes.begin() &&
877          "Cannot delete the entry node!");
878   assert(N->use_empty() && "Cannot delete a node that is not dead!");
879 
880   // Drop all of the operands and decrement used node's use counts.
881   N->DropOperands();
882 
883   DeallocateNode(N);
884 }
885 
886 void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
887   assert(!(V->isVariadic() && isParameter));
888   if (isParameter)
889     ByvalParmDbgValues.push_back(V);
890   else
891     DbgValues.push_back(V);
892   for (const SDNode *Node : V->getSDNodes())
893     if (Node)
894       DbgValMap[Node].push_back(V);
895 }
896 
897 void SDDbgInfo::erase(const SDNode *Node) {
898   DbgValMapType::iterator I = DbgValMap.find(Node);
899   if (I == DbgValMap.end())
900     return;
901   for (auto &Val: I->second)
902     Val->setIsInvalidated();
903   DbgValMap.erase(I);
904 }
905 
906 void SelectionDAG::DeallocateNode(SDNode *N) {
907   // If we have operands, deallocate them.
908   removeOperands(N);
909 
910   NodeAllocator.Deallocate(AllNodes.remove(N));
911 
912   // Set the opcode to DELETED_NODE to help catch bugs when node
913   // memory is reallocated.
914   // FIXME: There are places in SDag that have grown a dependency on the opcode
915   // value in the released node.
916   __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
917   N->NodeType = ISD::DELETED_NODE;
918 
919   // If any of the SDDbgValue nodes refer to this SDNode, invalidate
920   // them and forget about that node.
921   DbgInfo->erase(N);
922 }
923 
924 #ifndef NDEBUG
925 /// VerifySDNode - Sanity check the given SDNode.  Aborts if it is invalid.
926 static void VerifySDNode(SDNode *N) {
927   switch (N->getOpcode()) {
928   default:
929     break;
930   case ISD::BUILD_PAIR: {
931     EVT VT = N->getValueType(0);
932     assert(N->getNumValues() == 1 && "Too many results!");
933     assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
934            "Wrong return type!");
935     assert(N->getNumOperands() == 2 && "Wrong number of operands!");
936     assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
937            "Mismatched operand types!");
938     assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
939            "Wrong operand type!");
940     assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
941            "Wrong return type size");
942     break;
943   }
944   case ISD::BUILD_VECTOR: {
945     assert(N->getNumValues() == 1 && "Too many results!");
946     assert(N->getValueType(0).isVector() && "Wrong return type!");
947     assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
948            "Wrong number of operands!");
949     EVT EltVT = N->getValueType(0).getVectorElementType();
950     for (const SDUse &Op : N->ops()) {
951       assert((Op.getValueType() == EltVT ||
952               (EltVT.isInteger() && Op.getValueType().isInteger() &&
953                EltVT.bitsLE(Op.getValueType()))) &&
954              "Wrong operand type!");
955       assert(Op.getValueType() == N->getOperand(0).getValueType() &&
956              "Operands must all have the same type");
957     }
958     break;
959   }
960   }
961 }
962 #endif // NDEBUG
963 
964 /// Insert a newly allocated node into the DAG.
965 ///
966 /// Handles insertion into the all nodes list and CSE map, as well as
967 /// verification and other common operations when a new node is allocated.
968 void SelectionDAG::InsertNode(SDNode *N) {
969   AllNodes.push_back(N);
970 #ifndef NDEBUG
971   N->PersistentId = NextPersistentId++;
972   VerifySDNode(N);
973 #endif
974   for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
975     DUL->NodeInserted(N);
976 }
977 
978 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
979 /// correspond to it.  This is useful when we're about to delete or repurpose
980 /// the node.  We don't want future request for structurally identical nodes
981 /// to return N anymore.
982 bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
983   bool Erased = false;
984   switch (N->getOpcode()) {
985   case ISD::HANDLENODE: return false;  // noop.
986   case ISD::CONDCODE:
987     assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
988            "Cond code doesn't exist!");
989     Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
990     CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
991     break;
992   case ISD::ExternalSymbol:
993     Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
994     break;
995   case ISD::TargetExternalSymbol: {
996     ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
997     Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
998         ESN->getSymbol(), ESN->getTargetFlags()));
999     break;
1000   }
1001   case ISD::MCSymbol: {
1002     auto *MCSN = cast<MCSymbolSDNode>(N);
1003     Erased = MCSymbols.erase(MCSN->getMCSymbol());
1004     break;
1005   }
1006   case ISD::VALUETYPE: {
1007     EVT VT = cast<VTSDNode>(N)->getVT();
1008     if (VT.isExtended()) {
1009       Erased = ExtendedValueTypeNodes.erase(VT);
1010     } else {
1011       Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1012       ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1013     }
1014     break;
1015   }
1016   default:
1017     // Remove it from the CSE Map.
1018     assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1019     assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1020     Erased = CSEMap.RemoveNode(N);
1021     break;
1022   }
1023 #ifndef NDEBUG
1024   // Verify that the node was actually in one of the CSE maps, unless it has a
1025   // flag result (which cannot be CSE'd) or is one of the special cases that are
1026   // not subject to CSE.
1027   if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1028       !N->isMachineOpcode() && !doNotCSE(N)) {
1029     N->dump(this);
1030     dbgs() << "\n";
1031     llvm_unreachable("Node is not in map!");
1032   }
1033 #endif
1034   return Erased;
1035 }
1036 
1037 /// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1038 /// maps and modified in place. Add it back to the CSE maps, unless an identical
1039 /// node already exists, in which case transfer all its users to the existing
1040 /// node. This transfer can potentially trigger recursive merging.
1041 void
1042 SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1043   // For node types that aren't CSE'd, just act as if no identical node
1044   // already exists.
1045   if (!doNotCSE(N)) {
1046     SDNode *Existing = CSEMap.GetOrInsertNode(N);
1047     if (Existing != N) {
1048       // If there was already an existing matching node, use ReplaceAllUsesWith
1049       // to replace the dead one with the existing one.  This can cause
1050       // recursive merging of other unrelated nodes down the line.
1051       ReplaceAllUsesWith(N, Existing);
1052 
1053       // N is now dead. Inform the listeners and delete it.
1054       for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1055         DUL->NodeDeleted(N, Existing);
1056       DeleteNodeNotInCSEMaps(N);
1057       return;
1058     }
1059   }
1060 
1061   // If the node doesn't already exist, we updated it.  Inform listeners.
1062   for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1063     DUL->NodeUpdated(N);
1064 }
1065 
1066 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1067 /// were replaced with those specified.  If this node is never memoized,
1068 /// return null, otherwise return a pointer to the slot it would take.  If a
1069 /// node already exists with these operands, the slot will be non-null.
1070 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1071                                            void *&InsertPos) {
1072   if (doNotCSE(N))
1073     return nullptr;
1074 
1075   SDValue Ops[] = { Op };
1076   FoldingSetNodeID ID;
1077   AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1078   AddNodeIDCustom(ID, N);
1079   SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1080   if (Node)
1081     Node->intersectFlagsWith(N->getFlags());
1082   return Node;
1083 }
1084 
1085 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1086 /// were replaced with those specified.  If this node is never memoized,
1087 /// return null, otherwise return a pointer to the slot it would take.  If a
1088 /// node already exists with these operands, the slot will be non-null.
1089 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1090                                            SDValue Op1, SDValue Op2,
1091                                            void *&InsertPos) {
1092   if (doNotCSE(N))
1093     return nullptr;
1094 
1095   SDValue Ops[] = { Op1, Op2 };
1096   FoldingSetNodeID ID;
1097   AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1098   AddNodeIDCustom(ID, N);
1099   SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1100   if (Node)
1101     Node->intersectFlagsWith(N->getFlags());
1102   return Node;
1103 }
1104 
1105 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1106 /// were replaced with those specified.  If this node is never memoized,
1107 /// return null, otherwise return a pointer to the slot it would take.  If a
1108 /// node already exists with these operands, the slot will be non-null.
1109 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1110                                            void *&InsertPos) {
1111   if (doNotCSE(N))
1112     return nullptr;
1113 
1114   FoldingSetNodeID ID;
1115   AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1116   AddNodeIDCustom(ID, N);
1117   SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1118   if (Node)
1119     Node->intersectFlagsWith(N->getFlags());
1120   return Node;
1121 }
1122 
1123 Align SelectionDAG::getEVTAlign(EVT VT) const {
1124   Type *Ty = VT == MVT::iPTR ?
1125                    PointerType::get(Type::getInt8Ty(*getContext()), 0) :
1126                    VT.getTypeForEVT(*getContext());
1127 
1128   return getDataLayout().getABITypeAlign(Ty);
1129 }
1130 
1131 // EntryNode could meaningfully have debug info if we can find it...
1132 SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
1133     : TM(tm), OptLevel(OL),
1134       EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
1135       Root(getEntryNode()) {
1136   InsertNode(&EntryNode);
1137   DbgInfo = new SDDbgInfo();
1138 }
1139 
1140 void SelectionDAG::init(MachineFunction &NewMF,
1141                         OptimizationRemarkEmitter &NewORE,
1142                         Pass *PassPtr, const TargetLibraryInfo *LibraryInfo,
1143                         LegacyDivergenceAnalysis * Divergence,
1144                         ProfileSummaryInfo *PSIin,
1145                         BlockFrequencyInfo *BFIin) {
1146   MF = &NewMF;
1147   SDAGISelPass = PassPtr;
1148   ORE = &NewORE;
1149   TLI = getSubtarget().getTargetLowering();
1150   TSI = getSubtarget().getSelectionDAGInfo();
1151   LibInfo = LibraryInfo;
1152   Context = &MF->getFunction().getContext();
1153   DA = Divergence;
1154   PSI = PSIin;
1155   BFI = BFIin;
1156 }
1157 
1158 SelectionDAG::~SelectionDAG() {
1159   assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1160   allnodes_clear();
1161   OperandRecycler.clear(OperandAllocator);
1162   delete DbgInfo;
1163 }
1164 
1165 bool SelectionDAG::shouldOptForSize() const {
1166   return MF->getFunction().hasOptSize() ||
1167       llvm::shouldOptimizeForSize(FLI->MBB->getBasicBlock(), PSI, BFI);
1168 }
1169 
1170 void SelectionDAG::allnodes_clear() {
1171   assert(&*AllNodes.begin() == &EntryNode);
1172   AllNodes.remove(AllNodes.begin());
1173   while (!AllNodes.empty())
1174     DeallocateNode(&AllNodes.front());
1175 #ifndef NDEBUG
1176   NextPersistentId = 0;
1177 #endif
1178 }
1179 
1180 SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1181                                           void *&InsertPos) {
1182   SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1183   if (N) {
1184     switch (N->getOpcode()) {
1185     default: break;
1186     case ISD::Constant:
1187     case ISD::ConstantFP:
1188       llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1189                        "debug location.  Use another overload.");
1190     }
1191   }
1192   return N;
1193 }
1194 
1195 SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1196                                           const SDLoc &DL, void *&InsertPos) {
1197   SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1198   if (N) {
1199     switch (N->getOpcode()) {
1200     case ISD::Constant:
1201     case ISD::ConstantFP:
1202       // Erase debug location from the node if the node is used at several
1203       // different places. Do not propagate one location to all uses as it
1204       // will cause a worse single stepping debugging experience.
1205       if (N->getDebugLoc() != DL.getDebugLoc())
1206         N->setDebugLoc(DebugLoc());
1207       break;
1208     default:
1209       // When the node's point of use is located earlier in the instruction
1210       // sequence than its prior point of use, update its debug info to the
1211       // earlier location.
1212       if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1213         N->setDebugLoc(DL.getDebugLoc());
1214       break;
1215     }
1216   }
1217   return N;
1218 }
1219 
1220 void SelectionDAG::clear() {
1221   allnodes_clear();
1222   OperandRecycler.clear(OperandAllocator);
1223   OperandAllocator.Reset();
1224   CSEMap.clear();
1225 
1226   ExtendedValueTypeNodes.clear();
1227   ExternalSymbols.clear();
1228   TargetExternalSymbols.clear();
1229   MCSymbols.clear();
1230   SDCallSiteDbgInfo.clear();
1231   std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
1232             static_cast<CondCodeSDNode*>(nullptr));
1233   std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
1234             static_cast<SDNode*>(nullptr));
1235 
1236   EntryNode.UseList = nullptr;
1237   InsertNode(&EntryNode);
1238   Root = getEntryNode();
1239   DbgInfo->clear();
1240 }
1241 
1242 SDValue SelectionDAG::getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT) {
1243   return VT.bitsGT(Op.getValueType())
1244              ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1245              : getNode(ISD::FP_ROUND, DL, VT, Op, getIntPtrConstant(0, DL));
1246 }
1247 
1248 std::pair<SDValue, SDValue>
1249 SelectionDAG::getStrictFPExtendOrRound(SDValue Op, SDValue Chain,
1250                                        const SDLoc &DL, EVT VT) {
1251   assert(!VT.bitsEq(Op.getValueType()) &&
1252          "Strict no-op FP extend/round not allowed.");
1253   SDValue Res =
1254       VT.bitsGT(Op.getValueType())
1255           ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1256           : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1257                     {Chain, Op, getIntPtrConstant(0, DL)});
1258 
1259   return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1260 }
1261 
1262 SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT) {
1263   return VT.bitsGT(Op.getValueType()) ?
1264     getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1265     getNode(ISD::TRUNCATE, DL, VT, Op);
1266 }
1267 
1268 SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT) {
1269   return VT.bitsGT(Op.getValueType()) ?
1270     getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1271     getNode(ISD::TRUNCATE, DL, VT, Op);
1272 }
1273 
1274 SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT) {
1275   return VT.bitsGT(Op.getValueType()) ?
1276     getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1277     getNode(ISD::TRUNCATE, DL, VT, Op);
1278 }
1279 
1280 SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT,
1281                                         EVT OpVT) {
1282   if (VT.bitsLE(Op.getValueType()))
1283     return getNode(ISD::TRUNCATE, SL, VT, Op);
1284 
1285   TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
1286   return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1287 }
1288 
1289 SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT) {
1290   EVT OpVT = Op.getValueType();
1291   assert(VT.isInteger() && OpVT.isInteger() &&
1292          "Cannot getZeroExtendInReg FP types");
1293   assert(VT.isVector() == OpVT.isVector() &&
1294          "getZeroExtendInReg type should be vector iff the operand "
1295          "type is vector!");
1296   assert((!VT.isVector() ||
1297           VT.getVectorElementCount() == OpVT.getVectorElementCount()) &&
1298          "Vector element counts must match in getZeroExtendInReg");
1299   assert(VT.bitsLE(OpVT) && "Not extending!");
1300   if (OpVT == VT)
1301     return Op;
1302   APInt Imm = APInt::getLowBitsSet(OpVT.getScalarSizeInBits(),
1303                                    VT.getScalarSizeInBits());
1304   return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1305 }
1306 
1307 SDValue SelectionDAG::getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT) {
1308   // Only unsigned pointer semantics are supported right now. In the future this
1309   // might delegate to TLI to check pointer signedness.
1310   return getZExtOrTrunc(Op, DL, VT);
1311 }
1312 
1313 SDValue SelectionDAG::getPtrExtendInReg(SDValue Op, const SDLoc &DL, EVT VT) {
1314   // Only unsigned pointer semantics are supported right now. In the future this
1315   // might delegate to TLI to check pointer signedness.
1316   return getZeroExtendInReg(Op, DL, VT);
1317 }
1318 
1319 /// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1320 SDValue SelectionDAG::getNOT(const SDLoc &DL, SDValue Val, EVT VT) {
1321   EVT EltVT = VT.getScalarType();
1322   SDValue NegOne =
1323     getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL, VT);
1324   return getNode(ISD::XOR, DL, VT, Val, NegOne);
1325 }
1326 
1327 SDValue SelectionDAG::getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT) {
1328   SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1329   return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1330 }
1331 
1332 SDValue SelectionDAG::getBoolConstant(bool V, const SDLoc &DL, EVT VT,
1333                                       EVT OpVT) {
1334   if (!V)
1335     return getConstant(0, DL, VT);
1336 
1337   switch (TLI->getBooleanContents(OpVT)) {
1338   case TargetLowering::ZeroOrOneBooleanContent:
1339   case TargetLowering::UndefinedBooleanContent:
1340     return getConstant(1, DL, VT);
1341   case TargetLowering::ZeroOrNegativeOneBooleanContent:
1342     return getAllOnesConstant(DL, VT);
1343   }
1344   llvm_unreachable("Unexpected boolean content enum!");
1345 }
1346 
1347 SDValue SelectionDAG::getConstant(uint64_t Val, const SDLoc &DL, EVT VT,
1348                                   bool isT, bool isO) {
1349   EVT EltVT = VT.getScalarType();
1350   assert((EltVT.getSizeInBits() >= 64 ||
1351           (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1352          "getConstant with a uint64_t value that doesn't fit in the type!");
1353   return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO);
1354 }
1355 
1356 SDValue SelectionDAG::getConstant(const APInt &Val, const SDLoc &DL, EVT VT,
1357                                   bool isT, bool isO) {
1358   return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1359 }
1360 
1361 SDValue SelectionDAG::getConstant(const ConstantInt &Val, const SDLoc &DL,
1362                                   EVT VT, bool isT, bool isO) {
1363   assert(VT.isInteger() && "Cannot create FP integer constant!");
1364 
1365   EVT EltVT = VT.getScalarType();
1366   const ConstantInt *Elt = &Val;
1367 
1368   // In some cases the vector type is legal but the element type is illegal and
1369   // needs to be promoted, for example v8i8 on ARM.  In this case, promote the
1370   // inserted value (the type does not need to match the vector element type).
1371   // Any extra bits introduced will be truncated away.
1372   if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1373                            TargetLowering::TypePromoteInteger) {
1374     EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1375     APInt NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1376     Elt = ConstantInt::get(*getContext(), NewVal);
1377   }
1378   // In other cases the element type is illegal and needs to be expanded, for
1379   // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1380   // the value into n parts and use a vector type with n-times the elements.
1381   // Then bitcast to the type requested.
1382   // Legalizing constants too early makes the DAGCombiner's job harder so we
1383   // only legalize if the DAG tells us we must produce legal types.
1384   else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1385            TLI->getTypeAction(*getContext(), EltVT) ==
1386                TargetLowering::TypeExpandInteger) {
1387     const APInt &NewVal = Elt->getValue();
1388     EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1389     unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1390 
1391     // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1392     if (VT.isScalableVector()) {
1393       assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1394              "Can only handle an even split!");
1395       unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1396 
1397       SmallVector<SDValue, 2> ScalarParts;
1398       for (unsigned i = 0; i != Parts; ++i)
1399         ScalarParts.push_back(getConstant(
1400             NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1401             ViaEltVT, isT, isO));
1402 
1403       return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1404     }
1405 
1406     unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1407     EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1408 
1409     // Check the temporary vector is the correct size. If this fails then
1410     // getTypeToTransformTo() probably returned a type whose size (in bits)
1411     // isn't a power-of-2 factor of the requested type size.
1412     assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1413 
1414     SmallVector<SDValue, 2> EltParts;
1415     for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1416       EltParts.push_back(getConstant(
1417           NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1418           ViaEltVT, isT, isO));
1419 
1420     // EltParts is currently in little endian order. If we actually want
1421     // big-endian order then reverse it now.
1422     if (getDataLayout().isBigEndian())
1423       std::reverse(EltParts.begin(), EltParts.end());
1424 
1425     // The elements must be reversed when the element order is different
1426     // to the endianness of the elements (because the BITCAST is itself a
1427     // vector shuffle in this situation). However, we do not need any code to
1428     // perform this reversal because getConstant() is producing a vector
1429     // splat.
1430     // This situation occurs in MIPS MSA.
1431 
1432     SmallVector<SDValue, 8> Ops;
1433     for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1434       llvm::append_range(Ops, EltParts);
1435 
1436     SDValue V =
1437         getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1438     return V;
1439   }
1440 
1441   assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1442          "APInt size does not match type size!");
1443   unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1444   FoldingSetNodeID ID;
1445   AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
1446   ID.AddPointer(Elt);
1447   ID.AddBoolean(isO);
1448   void *IP = nullptr;
1449   SDNode *N = nullptr;
1450   if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1451     if (!VT.isVector())
1452       return SDValue(N, 0);
1453 
1454   if (!N) {
1455     N = newSDNode<ConstantSDNode>(isT, isO, Elt, EltVT);
1456     CSEMap.InsertNode(N, IP);
1457     InsertNode(N);
1458     NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1459   }
1460 
1461   SDValue Result(N, 0);
1462   if (VT.isScalableVector())
1463     Result = getSplatVector(VT, DL, Result);
1464   else if (VT.isVector())
1465     Result = getSplatBuildVector(VT, DL, Result);
1466 
1467   return Result;
1468 }
1469 
1470 SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, const SDLoc &DL,
1471                                         bool isTarget) {
1472   return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1473 }
1474 
1475 SDValue SelectionDAG::getShiftAmountConstant(uint64_t Val, EVT VT,
1476                                              const SDLoc &DL, bool LegalTypes) {
1477   assert(VT.isInteger() && "Shift amount is not an integer type!");
1478   EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout(), LegalTypes);
1479   return getConstant(Val, DL, ShiftVT);
1480 }
1481 
1482 SDValue SelectionDAG::getVectorIdxConstant(uint64_t Val, const SDLoc &DL,
1483                                            bool isTarget) {
1484   return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1485 }
1486 
1487 SDValue SelectionDAG::getConstantFP(const APFloat &V, const SDLoc &DL, EVT VT,
1488                                     bool isTarget) {
1489   return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1490 }
1491 
1492 SDValue SelectionDAG::getConstantFP(const ConstantFP &V, const SDLoc &DL,
1493                                     EVT VT, bool isTarget) {
1494   assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1495 
1496   EVT EltVT = VT.getScalarType();
1497 
1498   // Do the map lookup using the actual bit pattern for the floating point
1499   // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1500   // we don't have issues with SNANs.
1501   unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1502   FoldingSetNodeID ID;
1503   AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
1504   ID.AddPointer(&V);
1505   void *IP = nullptr;
1506   SDNode *N = nullptr;
1507   if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1508     if (!VT.isVector())
1509       return SDValue(N, 0);
1510 
1511   if (!N) {
1512     N = newSDNode<ConstantFPSDNode>(isTarget, &V, EltVT);
1513     CSEMap.InsertNode(N, IP);
1514     InsertNode(N);
1515   }
1516 
1517   SDValue Result(N, 0);
1518   if (VT.isScalableVector())
1519     Result = getSplatVector(VT, DL, Result);
1520   else if (VT.isVector())
1521     Result = getSplatBuildVector(VT, DL, Result);
1522   NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1523   return Result;
1524 }
1525 
1526 SDValue SelectionDAG::getConstantFP(double Val, const SDLoc &DL, EVT VT,
1527                                     bool isTarget) {
1528   EVT EltVT = VT.getScalarType();
1529   if (EltVT == MVT::f32)
1530     return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1531   if (EltVT == MVT::f64)
1532     return getConstantFP(APFloat(Val), DL, VT, isTarget);
1533   if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1534       EltVT == MVT::f16 || EltVT == MVT::bf16) {
1535     bool Ignored;
1536     APFloat APF = APFloat(Val);
1537     APF.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
1538                 &Ignored);
1539     return getConstantFP(APF, DL, VT, isTarget);
1540   }
1541   llvm_unreachable("Unsupported type in getConstantFP");
1542 }
1543 
1544 SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, const SDLoc &DL,
1545                                        EVT VT, int64_t Offset, bool isTargetGA,
1546                                        unsigned TargetFlags) {
1547   assert((TargetFlags == 0 || isTargetGA) &&
1548          "Cannot set target flags on target-independent globals");
1549 
1550   // Truncate (with sign-extension) the offset value to the pointer size.
1551   unsigned BitWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
1552   if (BitWidth < 64)
1553     Offset = SignExtend64(Offset, BitWidth);
1554 
1555   unsigned Opc;
1556   if (GV->isThreadLocal())
1557     Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1558   else
1559     Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
1560 
1561   FoldingSetNodeID ID;
1562   AddNodeIDNode(ID, Opc, getVTList(VT), None);
1563   ID.AddPointer(GV);
1564   ID.AddInteger(Offset);
1565   ID.AddInteger(TargetFlags);
1566   void *IP = nullptr;
1567   if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1568     return SDValue(E, 0);
1569 
1570   auto *N = newSDNode<GlobalAddressSDNode>(
1571       Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VT, Offset, TargetFlags);
1572   CSEMap.InsertNode(N, IP);
1573     InsertNode(N);
1574   return SDValue(N, 0);
1575 }
1576 
1577 SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1578   unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1579   FoldingSetNodeID ID;
1580   AddNodeIDNode(ID, Opc, getVTList(VT), None);
1581   ID.AddInteger(FI);
1582   void *IP = nullptr;
1583   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1584     return SDValue(E, 0);
1585 
1586   auto *N = newSDNode<FrameIndexSDNode>(FI, VT, isTarget);
1587   CSEMap.InsertNode(N, IP);
1588   InsertNode(N);
1589   return SDValue(N, 0);
1590 }
1591 
1592 SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1593                                    unsigned TargetFlags) {
1594   assert((TargetFlags == 0 || isTarget) &&
1595          "Cannot set target flags on target-independent jump tables");
1596   unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1597   FoldingSetNodeID ID;
1598   AddNodeIDNode(ID, Opc, getVTList(VT), None);
1599   ID.AddInteger(JTI);
1600   ID.AddInteger(TargetFlags);
1601   void *IP = nullptr;
1602   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1603     return SDValue(E, 0);
1604 
1605   auto *N = newSDNode<JumpTableSDNode>(JTI, VT, isTarget, TargetFlags);
1606   CSEMap.InsertNode(N, IP);
1607   InsertNode(N);
1608   return SDValue(N, 0);
1609 }
1610 
1611 SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
1612                                       MaybeAlign Alignment, int Offset,
1613                                       bool isTarget, unsigned TargetFlags) {
1614   assert((TargetFlags == 0 || isTarget) &&
1615          "Cannot set target flags on target-independent globals");
1616   if (!Alignment)
1617     Alignment = shouldOptForSize()
1618                     ? getDataLayout().getABITypeAlign(C->getType())
1619                     : getDataLayout().getPrefTypeAlign(C->getType());
1620   unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1621   FoldingSetNodeID ID;
1622   AddNodeIDNode(ID, Opc, getVTList(VT), None);
1623   ID.AddInteger(Alignment->value());
1624   ID.AddInteger(Offset);
1625   ID.AddPointer(C);
1626   ID.AddInteger(TargetFlags);
1627   void *IP = nullptr;
1628   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1629     return SDValue(E, 0);
1630 
1631   auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VT, Offset, *Alignment,
1632                                           TargetFlags);
1633   CSEMap.InsertNode(N, IP);
1634   InsertNode(N);
1635   SDValue V = SDValue(N, 0);
1636   NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
1637   return V;
1638 }
1639 
1640 SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
1641                                       MaybeAlign Alignment, int Offset,
1642                                       bool isTarget, unsigned TargetFlags) {
1643   assert((TargetFlags == 0 || isTarget) &&
1644          "Cannot set target flags on target-independent globals");
1645   if (!Alignment)
1646     Alignment = getDataLayout().getPrefTypeAlign(C->getType());
1647   unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1648   FoldingSetNodeID ID;
1649   AddNodeIDNode(ID, Opc, getVTList(VT), None);
1650   ID.AddInteger(Alignment->value());
1651   ID.AddInteger(Offset);
1652   C->addSelectionDAGCSEId(ID);
1653   ID.AddInteger(TargetFlags);
1654   void *IP = nullptr;
1655   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1656     return SDValue(E, 0);
1657 
1658   auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VT, Offset, *Alignment,
1659                                           TargetFlags);
1660   CSEMap.InsertNode(N, IP);
1661   InsertNode(N);
1662   return SDValue(N, 0);
1663 }
1664 
1665 SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1666                                      unsigned TargetFlags) {
1667   FoldingSetNodeID ID;
1668   AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
1669   ID.AddInteger(Index);
1670   ID.AddInteger(Offset);
1671   ID.AddInteger(TargetFlags);
1672   void *IP = nullptr;
1673   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1674     return SDValue(E, 0);
1675 
1676   auto *N = newSDNode<TargetIndexSDNode>(Index, VT, Offset, TargetFlags);
1677   CSEMap.InsertNode(N, IP);
1678   InsertNode(N);
1679   return SDValue(N, 0);
1680 }
1681 
1682 SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
1683   FoldingSetNodeID ID;
1684   AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
1685   ID.AddPointer(MBB);
1686   void *IP = nullptr;
1687   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1688     return SDValue(E, 0);
1689 
1690   auto *N = newSDNode<BasicBlockSDNode>(MBB);
1691   CSEMap.InsertNode(N, IP);
1692   InsertNode(N);
1693   return SDValue(N, 0);
1694 }
1695 
1696 SDValue SelectionDAG::getValueType(EVT VT) {
1697   if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1698       ValueTypeNodes.size())
1699     ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
1700 
1701   SDNode *&N = VT.isExtended() ?
1702     ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
1703 
1704   if (N) return SDValue(N, 0);
1705   N = newSDNode<VTSDNode>(VT);
1706   InsertNode(N);
1707   return SDValue(N, 0);
1708 }
1709 
1710 SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
1711   SDNode *&N = ExternalSymbols[Sym];
1712   if (N) return SDValue(N, 0);
1713   N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, VT);
1714   InsertNode(N);
1715   return SDValue(N, 0);
1716 }
1717 
1718 SDValue SelectionDAG::getMCSymbol(MCSymbol *Sym, EVT VT) {
1719   SDNode *&N = MCSymbols[Sym];
1720   if (N)
1721     return SDValue(N, 0);
1722   N = newSDNode<MCSymbolSDNode>(Sym, VT);
1723   InsertNode(N);
1724   return SDValue(N, 0);
1725 }
1726 
1727 SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
1728                                               unsigned TargetFlags) {
1729   SDNode *&N =
1730       TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
1731   if (N) return SDValue(N, 0);
1732   N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, VT);
1733   InsertNode(N);
1734   return SDValue(N, 0);
1735 }
1736 
1737 SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
1738   if ((unsigned)Cond >= CondCodeNodes.size())
1739     CondCodeNodes.resize(Cond+1);
1740 
1741   if (!CondCodeNodes[Cond]) {
1742     auto *N = newSDNode<CondCodeSDNode>(Cond);
1743     CondCodeNodes[Cond] = N;
1744     InsertNode(N);
1745   }
1746 
1747   return SDValue(CondCodeNodes[Cond], 0);
1748 }
1749 
1750 SDValue SelectionDAG::getStepVector(const SDLoc &DL, EVT ResVT, SDValue Step) {
1751   if (ResVT.isScalableVector())
1752     return getNode(ISD::STEP_VECTOR, DL, ResVT, Step);
1753 
1754   EVT OpVT = Step.getValueType();
1755   APInt StepVal = cast<ConstantSDNode>(Step)->getAPIntValue();
1756   SmallVector<SDValue, 16> OpsStepConstants;
1757   for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
1758     OpsStepConstants.push_back(getConstant(StepVal * i, DL, OpVT));
1759   return getBuildVector(ResVT, DL, OpsStepConstants);
1760 }
1761 
1762 /// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
1763 /// point at N1 to point at N2 and indices that point at N2 to point at N1.
1764 static void commuteShuffle(SDValue &N1, SDValue &N2, MutableArrayRef<int> M) {
1765   std::swap(N1, N2);
1766   ShuffleVectorSDNode::commuteMask(M);
1767 }
1768 
1769 SDValue SelectionDAG::getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1,
1770                                        SDValue N2, ArrayRef<int> Mask) {
1771   assert(VT.getVectorNumElements() == Mask.size() &&
1772          "Must have the same number of vector elements as mask elements!");
1773   assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1774          "Invalid VECTOR_SHUFFLE");
1775 
1776   // Canonicalize shuffle undef, undef -> undef
1777   if (N1.isUndef() && N2.isUndef())
1778     return getUNDEF(VT);
1779 
1780   // Validate that all indices in Mask are within the range of the elements
1781   // input to the shuffle.
1782   int NElts = Mask.size();
1783   assert(llvm::all_of(Mask,
1784                       [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
1785          "Index out of range");
1786 
1787   // Copy the mask so we can do any needed cleanup.
1788   SmallVector<int, 8> MaskVec(Mask.begin(), Mask.end());
1789 
1790   // Canonicalize shuffle v, v -> v, undef
1791   if (N1 == N2) {
1792     N2 = getUNDEF(VT);
1793     for (int i = 0; i != NElts; ++i)
1794       if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
1795   }
1796 
1797   // Canonicalize shuffle undef, v -> v, undef.  Commute the shuffle mask.
1798   if (N1.isUndef())
1799     commuteShuffle(N1, N2, MaskVec);
1800 
1801   if (TLI->hasVectorBlend()) {
1802     // If shuffling a splat, try to blend the splat instead. We do this here so
1803     // that even when this arises during lowering we don't have to re-handle it.
1804     auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
1805       BitVector UndefElements;
1806       SDValue Splat = BV->getSplatValue(&UndefElements);
1807       if (!Splat)
1808         return;
1809 
1810       for (int i = 0; i < NElts; ++i) {
1811         if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
1812           continue;
1813 
1814         // If this input comes from undef, mark it as such.
1815         if (UndefElements[MaskVec[i] - Offset]) {
1816           MaskVec[i] = -1;
1817           continue;
1818         }
1819 
1820         // If we can blend a non-undef lane, use that instead.
1821         if (!UndefElements[i])
1822           MaskVec[i] = i + Offset;
1823       }
1824     };
1825     if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
1826       BlendSplat(N1BV, 0);
1827     if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
1828       BlendSplat(N2BV, NElts);
1829   }
1830 
1831   // Canonicalize all index into lhs, -> shuffle lhs, undef
1832   // Canonicalize all index into rhs, -> shuffle rhs, undef
1833   bool AllLHS = true, AllRHS = true;
1834   bool N2Undef = N2.isUndef();
1835   for (int i = 0; i != NElts; ++i) {
1836     if (MaskVec[i] >= NElts) {
1837       if (N2Undef)
1838         MaskVec[i] = -1;
1839       else
1840         AllLHS = false;
1841     } else if (MaskVec[i] >= 0) {
1842       AllRHS = false;
1843     }
1844   }
1845   if (AllLHS && AllRHS)
1846     return getUNDEF(VT);
1847   if (AllLHS && !N2Undef)
1848     N2 = getUNDEF(VT);
1849   if (AllRHS) {
1850     N1 = getUNDEF(VT);
1851     commuteShuffle(N1, N2, MaskVec);
1852   }
1853   // Reset our undef status after accounting for the mask.
1854   N2Undef = N2.isUndef();
1855   // Re-check whether both sides ended up undef.
1856   if (N1.isUndef() && N2Undef)
1857     return getUNDEF(VT);
1858 
1859   // If Identity shuffle return that node.
1860   bool Identity = true, AllSame = true;
1861   for (int i = 0; i != NElts; ++i) {
1862     if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
1863     if (MaskVec[i] != MaskVec[0]) AllSame = false;
1864   }
1865   if (Identity && NElts)
1866     return N1;
1867 
1868   // Shuffling a constant splat doesn't change the result.
1869   if (N2Undef) {
1870     SDValue V = N1;
1871 
1872     // Look through any bitcasts. We check that these don't change the number
1873     // (and size) of elements and just changes their types.
1874     while (V.getOpcode() == ISD::BITCAST)
1875       V = V->getOperand(0);
1876 
1877     // A splat should always show up as a build vector node.
1878     if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
1879       BitVector UndefElements;
1880       SDValue Splat = BV->getSplatValue(&UndefElements);
1881       // If this is a splat of an undef, shuffling it is also undef.
1882       if (Splat && Splat.isUndef())
1883         return getUNDEF(VT);
1884 
1885       bool SameNumElts =
1886           V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
1887 
1888       // We only have a splat which can skip shuffles if there is a splatted
1889       // value and no undef lanes rearranged by the shuffle.
1890       if (Splat && UndefElements.none()) {
1891         // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
1892         // number of elements match or the value splatted is a zero constant.
1893         if (SameNumElts)
1894           return N1;
1895         if (auto *C = dyn_cast<ConstantSDNode>(Splat))
1896           if (C->isNullValue())
1897             return N1;
1898       }
1899 
1900       // If the shuffle itself creates a splat, build the vector directly.
1901       if (AllSame && SameNumElts) {
1902         EVT BuildVT = BV->getValueType(0);
1903         const SDValue &Splatted = BV->getOperand(MaskVec[0]);
1904         SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
1905 
1906         // We may have jumped through bitcasts, so the type of the
1907         // BUILD_VECTOR may not match the type of the shuffle.
1908         if (BuildVT != VT)
1909           NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
1910         return NewBV;
1911       }
1912     }
1913   }
1914 
1915   FoldingSetNodeID ID;
1916   SDValue Ops[2] = { N1, N2 };
1917   AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
1918   for (int i = 0; i != NElts; ++i)
1919     ID.AddInteger(MaskVec[i]);
1920 
1921   void* IP = nullptr;
1922   if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
1923     return SDValue(E, 0);
1924 
1925   // Allocate the mask array for the node out of the BumpPtrAllocator, since
1926   // SDNode doesn't have access to it.  This memory will be "leaked" when
1927   // the node is deallocated, but recovered when the NodeAllocator is released.
1928   int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1929   llvm::copy(MaskVec, MaskAlloc);
1930 
1931   auto *N = newSDNode<ShuffleVectorSDNode>(VT, dl.getIROrder(),
1932                                            dl.getDebugLoc(), MaskAlloc);
1933   createOperands(N, Ops);
1934 
1935   CSEMap.InsertNode(N, IP);
1936   InsertNode(N);
1937   SDValue V = SDValue(N, 0);
1938   NewSDValueDbgMsg(V, "Creating new node: ", this);
1939   return V;
1940 }
1941 
1942 SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) {
1943   EVT VT = SV.getValueType(0);
1944   SmallVector<int, 8> MaskVec(SV.getMask().begin(), SV.getMask().end());
1945   ShuffleVectorSDNode::commuteMask(MaskVec);
1946 
1947   SDValue Op0 = SV.getOperand(0);
1948   SDValue Op1 = SV.getOperand(1);
1949   return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
1950 }
1951 
1952 SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
1953   FoldingSetNodeID ID;
1954   AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
1955   ID.AddInteger(RegNo);
1956   void *IP = nullptr;
1957   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1958     return SDValue(E, 0);
1959 
1960   auto *N = newSDNode<RegisterSDNode>(RegNo, VT);
1961   N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, DA);
1962   CSEMap.InsertNode(N, IP);
1963   InsertNode(N);
1964   return SDValue(N, 0);
1965 }
1966 
1967 SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1968   FoldingSetNodeID ID;
1969   AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
1970   ID.AddPointer(RegMask);
1971   void *IP = nullptr;
1972   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1973     return SDValue(E, 0);
1974 
1975   auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
1976   CSEMap.InsertNode(N, IP);
1977   InsertNode(N);
1978   return SDValue(N, 0);
1979 }
1980 
1981 SDValue SelectionDAG::getEHLabel(const SDLoc &dl, SDValue Root,
1982                                  MCSymbol *Label) {
1983   return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
1984 }
1985 
1986 SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
1987                                    SDValue Root, MCSymbol *Label) {
1988   FoldingSetNodeID ID;
1989   SDValue Ops[] = { Root };
1990   AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
1991   ID.AddPointer(Label);
1992   void *IP = nullptr;
1993   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1994     return SDValue(E, 0);
1995 
1996   auto *N =
1997       newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
1998   createOperands(N, Ops);
1999 
2000   CSEMap.InsertNode(N, IP);
2001   InsertNode(N);
2002   return SDValue(N, 0);
2003 }
2004 
2005 SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
2006                                       int64_t Offset, bool isTarget,
2007                                       unsigned TargetFlags) {
2008   unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2009 
2010   FoldingSetNodeID ID;
2011   AddNodeIDNode(ID, Opc, getVTList(VT), None);
2012   ID.AddPointer(BA);
2013   ID.AddInteger(Offset);
2014   ID.AddInteger(TargetFlags);
2015   void *IP = nullptr;
2016   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2017     return SDValue(E, 0);
2018 
2019   auto *N = newSDNode<BlockAddressSDNode>(Opc, VT, BA, Offset, TargetFlags);
2020   CSEMap.InsertNode(N, IP);
2021   InsertNode(N);
2022   return SDValue(N, 0);
2023 }
2024 
2025 SDValue SelectionDAG::getSrcValue(const Value *V) {
2026   FoldingSetNodeID ID;
2027   AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
2028   ID.AddPointer(V);
2029 
2030   void *IP = nullptr;
2031   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2032     return SDValue(E, 0);
2033 
2034   auto *N = newSDNode<SrcValueSDNode>(V);
2035   CSEMap.InsertNode(N, IP);
2036   InsertNode(N);
2037   return SDValue(N, 0);
2038 }
2039 
2040 SDValue SelectionDAG::getMDNode(const MDNode *MD) {
2041   FoldingSetNodeID ID;
2042   AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
2043   ID.AddPointer(MD);
2044 
2045   void *IP = nullptr;
2046   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2047     return SDValue(E, 0);
2048 
2049   auto *N = newSDNode<MDNodeSDNode>(MD);
2050   CSEMap.InsertNode(N, IP);
2051   InsertNode(N);
2052   return SDValue(N, 0);
2053 }
2054 
2055 SDValue SelectionDAG::getBitcast(EVT VT, SDValue V) {
2056   if (VT == V.getValueType())
2057     return V;
2058 
2059   return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2060 }
2061 
2062 SDValue SelectionDAG::getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr,
2063                                        unsigned SrcAS, unsigned DestAS) {
2064   SDValue Ops[] = {Ptr};
2065   FoldingSetNodeID ID;
2066   AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
2067   ID.AddInteger(SrcAS);
2068   ID.AddInteger(DestAS);
2069 
2070   void *IP = nullptr;
2071   if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2072     return SDValue(E, 0);
2073 
2074   auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2075                                            VT, SrcAS, DestAS);
2076   createOperands(N, Ops);
2077 
2078   CSEMap.InsertNode(N, IP);
2079   InsertNode(N);
2080   return SDValue(N, 0);
2081 }
2082 
2083 SDValue SelectionDAG::getFreeze(SDValue V) {
2084   return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2085 }
2086 
2087 /// getShiftAmountOperand - Return the specified value casted to
2088 /// the target's desired shift amount type.
2089 SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
2090   EVT OpTy = Op.getValueType();
2091   EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2092   if (OpTy == ShTy || OpTy.isVector()) return Op;
2093 
2094   return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2095 }
2096 
2097 SDValue SelectionDAG::expandVAArg(SDNode *Node) {
2098   SDLoc dl(Node);
2099   const TargetLowering &TLI = getTargetLoweringInfo();
2100   const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2101   EVT VT = Node->getValueType(0);
2102   SDValue Tmp1 = Node->getOperand(0);
2103   SDValue Tmp2 = Node->getOperand(1);
2104   const MaybeAlign MA(Node->getConstantOperandVal(3));
2105 
2106   SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2107                                Tmp2, MachinePointerInfo(V));
2108   SDValue VAList = VAListLoad;
2109 
2110   if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2111     VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2112                      getConstant(MA->value() - 1, dl, VAList.getValueType()));
2113 
2114     VAList =
2115         getNode(ISD::AND, dl, VAList.getValueType(), VAList,
2116                 getConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2117   }
2118 
2119   // Increment the pointer, VAList, to the next vaarg
2120   Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2121                  getConstant(getDataLayout().getTypeAllocSize(
2122                                                VT.getTypeForEVT(*getContext())),
2123                              dl, VAList.getValueType()));
2124   // Store the incremented VAList to the legalized pointer
2125   Tmp1 =
2126       getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2127   // Load the actual argument out of the pointer VAList
2128   return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2129 }
2130 
2131 SDValue SelectionDAG::expandVACopy(SDNode *Node) {
2132   SDLoc dl(Node);
2133   const TargetLowering &TLI = getTargetLoweringInfo();
2134   // This defaults to loading a pointer from the input and storing it to the
2135   // output, returning the chain.
2136   const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2137   const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2138   SDValue Tmp1 =
2139       getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2140               Node->getOperand(2), MachinePointerInfo(VS));
2141   return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2142                   MachinePointerInfo(VD));
2143 }
2144 
2145 Align SelectionDAG::getReducedAlign(EVT VT, bool UseABI) {
2146   const DataLayout &DL = getDataLayout();
2147   Type *Ty = VT.getTypeForEVT(*getContext());
2148   Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2149 
2150   if (TLI->isTypeLegal(VT) || !VT.isVector())
2151     return RedAlign;
2152 
2153   const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2154   const Align StackAlign = TFI->getStackAlign();
2155 
2156   // See if we can choose a smaller ABI alignment in cases where it's an
2157   // illegal vector type that will get broken down.
2158   if (RedAlign > StackAlign) {
2159     EVT IntermediateVT;
2160     MVT RegisterVT;
2161     unsigned NumIntermediates;
2162     TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2163                                 NumIntermediates, RegisterVT);
2164     Ty = IntermediateVT.getTypeForEVT(*getContext());
2165     Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2166     if (RedAlign2 < RedAlign)
2167       RedAlign = RedAlign2;
2168   }
2169 
2170   return RedAlign;
2171 }
2172 
2173 SDValue SelectionDAG::CreateStackTemporary(TypeSize Bytes, Align Alignment) {
2174   MachineFrameInfo &MFI = MF->getFrameInfo();
2175   const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2176   int StackID = 0;
2177   if (Bytes.isScalable())
2178     StackID = TFI->getStackIDForScalableVectors();
2179   // The stack id gives an indication of whether the object is scalable or
2180   // not, so it's safe to pass in the minimum size here.
2181   int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinSize(), Alignment,
2182                                        false, nullptr, StackID);
2183   return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2184 }
2185 
2186 SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
2187   Type *Ty = VT.getTypeForEVT(*getContext());
2188   Align StackAlign =
2189       std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2190   return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2191 }
2192 
2193 SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
2194   TypeSize VT1Size = VT1.getStoreSize();
2195   TypeSize VT2Size = VT2.getStoreSize();
2196   assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2197          "Don't know how to choose the maximum size when creating a stack "
2198          "temporary");
2199   TypeSize Bytes =
2200       VT1Size.getKnownMinSize() > VT2Size.getKnownMinSize() ? VT1Size : VT2Size;
2201 
2202   Type *Ty1 = VT1.getTypeForEVT(*getContext());
2203   Type *Ty2 = VT2.getTypeForEVT(*getContext());
2204   const DataLayout &DL = getDataLayout();
2205   Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2206   return CreateStackTemporary(Bytes, Align);
2207 }
2208 
2209 SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1, SDValue N2,
2210                                 ISD::CondCode Cond, const SDLoc &dl) {
2211   EVT OpVT = N1.getValueType();
2212 
2213   // These setcc operations always fold.
2214   switch (Cond) {
2215   default: break;
2216   case ISD::SETFALSE:
2217   case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2218   case ISD::SETTRUE:
2219   case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2220 
2221   case ISD::SETOEQ:
2222   case ISD::SETOGT:
2223   case ISD::SETOGE:
2224   case ISD::SETOLT:
2225   case ISD::SETOLE:
2226   case ISD::SETONE:
2227   case ISD::SETO:
2228   case ISD::SETUO:
2229   case ISD::SETUEQ:
2230   case ISD::SETUNE:
2231     assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2232     break;
2233   }
2234 
2235   if (OpVT.isInteger()) {
2236     // For EQ and NE, we can always pick a value for the undef to make the
2237     // predicate pass or fail, so we can return undef.
2238     // Matches behavior in llvm::ConstantFoldCompareInstruction.
2239     // icmp eq/ne X, undef -> undef.
2240     if ((N1.isUndef() || N2.isUndef()) &&
2241         (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2242       return getUNDEF(VT);
2243 
2244     // If both operands are undef, we can return undef for int comparison.
2245     // icmp undef, undef -> undef.
2246     if (N1.isUndef() && N2.isUndef())
2247       return getUNDEF(VT);
2248 
2249     // icmp X, X -> true/false
2250     // icmp X, undef -> true/false because undef could be X.
2251     if (N1 == N2)
2252       return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2253   }
2254 
2255   if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2)) {
2256     const APInt &C2 = N2C->getAPIntValue();
2257     if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1)) {
2258       const APInt &C1 = N1C->getAPIntValue();
2259 
2260       switch (Cond) {
2261       default: llvm_unreachable("Unknown integer setcc!");
2262       case ISD::SETEQ:  return getBoolConstant(C1 == C2, dl, VT, OpVT);
2263       case ISD::SETNE:  return getBoolConstant(C1 != C2, dl, VT, OpVT);
2264       case ISD::SETULT: return getBoolConstant(C1.ult(C2), dl, VT, OpVT);
2265       case ISD::SETUGT: return getBoolConstant(C1.ugt(C2), dl, VT, OpVT);
2266       case ISD::SETULE: return getBoolConstant(C1.ule(C2), dl, VT, OpVT);
2267       case ISD::SETUGE: return getBoolConstant(C1.uge(C2), dl, VT, OpVT);
2268       case ISD::SETLT:  return getBoolConstant(C1.slt(C2), dl, VT, OpVT);
2269       case ISD::SETGT:  return getBoolConstant(C1.sgt(C2), dl, VT, OpVT);
2270       case ISD::SETLE:  return getBoolConstant(C1.sle(C2), dl, VT, OpVT);
2271       case ISD::SETGE:  return getBoolConstant(C1.sge(C2), dl, VT, OpVT);
2272       }
2273     }
2274   }
2275 
2276   auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2277   auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2278 
2279   if (N1CFP && N2CFP) {
2280     APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2281     switch (Cond) {
2282     default: break;
2283     case ISD::SETEQ:  if (R==APFloat::cmpUnordered)
2284                         return getUNDEF(VT);
2285                       LLVM_FALLTHROUGH;
2286     case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2287                                              OpVT);
2288     case ISD::SETNE:  if (R==APFloat::cmpUnordered)
2289                         return getUNDEF(VT);
2290                       LLVM_FALLTHROUGH;
2291     case ISD::SETONE: return getBoolConstant(R==APFloat::cmpGreaterThan ||
2292                                              R==APFloat::cmpLessThan, dl, VT,
2293                                              OpVT);
2294     case ISD::SETLT:  if (R==APFloat::cmpUnordered)
2295                         return getUNDEF(VT);
2296                       LLVM_FALLTHROUGH;
2297     case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2298                                              OpVT);
2299     case ISD::SETGT:  if (R==APFloat::cmpUnordered)
2300                         return getUNDEF(VT);
2301                       LLVM_FALLTHROUGH;
2302     case ISD::SETOGT: return getBoolConstant(R==APFloat::cmpGreaterThan, dl,
2303                                              VT, OpVT);
2304     case ISD::SETLE:  if (R==APFloat::cmpUnordered)
2305                         return getUNDEF(VT);
2306                       LLVM_FALLTHROUGH;
2307     case ISD::SETOLE: return getBoolConstant(R==APFloat::cmpLessThan ||
2308                                              R==APFloat::cmpEqual, dl, VT,
2309                                              OpVT);
2310     case ISD::SETGE:  if (R==APFloat::cmpUnordered)
2311                         return getUNDEF(VT);
2312                       LLVM_FALLTHROUGH;
2313     case ISD::SETOGE: return getBoolConstant(R==APFloat::cmpGreaterThan ||
2314                                          R==APFloat::cmpEqual, dl, VT, OpVT);
2315     case ISD::SETO:   return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2316                                              OpVT);
2317     case ISD::SETUO:  return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2318                                              OpVT);
2319     case ISD::SETUEQ: return getBoolConstant(R==APFloat::cmpUnordered ||
2320                                              R==APFloat::cmpEqual, dl, VT,
2321                                              OpVT);
2322     case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2323                                              OpVT);
2324     case ISD::SETULT: return getBoolConstant(R==APFloat::cmpUnordered ||
2325                                              R==APFloat::cmpLessThan, dl, VT,
2326                                              OpVT);
2327     case ISD::SETUGT: return getBoolConstant(R==APFloat::cmpGreaterThan ||
2328                                              R==APFloat::cmpUnordered, dl, VT,
2329                                              OpVT);
2330     case ISD::SETULE: return getBoolConstant(R!=APFloat::cmpGreaterThan, dl,
2331                                              VT, OpVT);
2332     case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2333                                              OpVT);
2334     }
2335   } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2336     // Ensure that the constant occurs on the RHS.
2337     ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
2338     if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2339       return SDValue();
2340     return getSetCC(dl, VT, N2, N1, SwappedCond);
2341   } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2342              (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2343     // If an operand is known to be a nan (or undef that could be a nan), we can
2344     // fold it.
2345     // Choosing NaN for the undef will always make unordered comparison succeed
2346     // and ordered comparison fails.
2347     // Matches behavior in llvm::ConstantFoldCompareInstruction.
2348     switch (ISD::getUnorderedFlavor(Cond)) {
2349     default:
2350       llvm_unreachable("Unknown flavor!");
2351     case 0: // Known false.
2352       return getBoolConstant(false, dl, VT, OpVT);
2353     case 1: // Known true.
2354       return getBoolConstant(true, dl, VT, OpVT);
2355     case 2: // Undefined.
2356       return getUNDEF(VT);
2357     }
2358   }
2359 
2360   // Could not fold it.
2361   return SDValue();
2362 }
2363 
2364 /// See if the specified operand can be simplified with the knowledge that only
2365 /// the bits specified by DemandedBits are used.
2366 /// TODO: really we should be making this into the DAG equivalent of
2367 /// SimplifyMultipleUseDemandedBits and not generate any new nodes.
2368 SDValue SelectionDAG::GetDemandedBits(SDValue V, const APInt &DemandedBits) {
2369   EVT VT = V.getValueType();
2370 
2371   if (VT.isScalableVector())
2372     return SDValue();
2373 
2374   APInt DemandedElts = VT.isVector()
2375                            ? APInt::getAllOnesValue(VT.getVectorNumElements())
2376                            : APInt(1, 1);
2377   return GetDemandedBits(V, DemandedBits, DemandedElts);
2378 }
2379 
2380 /// See if the specified operand can be simplified with the knowledge that only
2381 /// the bits specified by DemandedBits are used in the elements specified by
2382 /// DemandedElts.
2383 /// TODO: really we should be making this into the DAG equivalent of
2384 /// SimplifyMultipleUseDemandedBits and not generate any new nodes.
2385 SDValue SelectionDAG::GetDemandedBits(SDValue V, const APInt &DemandedBits,
2386                                       const APInt &DemandedElts) {
2387   switch (V.getOpcode()) {
2388   default:
2389     return TLI->SimplifyMultipleUseDemandedBits(V, DemandedBits, DemandedElts,
2390                                                 *this, 0);
2391   case ISD::Constant: {
2392     const APInt &CVal = cast<ConstantSDNode>(V)->getAPIntValue();
2393     APInt NewVal = CVal & DemandedBits;
2394     if (NewVal != CVal)
2395       return getConstant(NewVal, SDLoc(V), V.getValueType());
2396     break;
2397   }
2398   case ISD::SRL:
2399     // Only look at single-use SRLs.
2400     if (!V.getNode()->hasOneUse())
2401       break;
2402     if (auto *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
2403       // See if we can recursively simplify the LHS.
2404       unsigned Amt = RHSC->getZExtValue();
2405 
2406       // Watch out for shift count overflow though.
2407       if (Amt >= DemandedBits.getBitWidth())
2408         break;
2409       APInt SrcDemandedBits = DemandedBits << Amt;
2410       if (SDValue SimplifyLHS =
2411               GetDemandedBits(V.getOperand(0), SrcDemandedBits))
2412         return getNode(ISD::SRL, SDLoc(V), V.getValueType(), SimplifyLHS,
2413                        V.getOperand(1));
2414     }
2415     break;
2416   }
2417   return SDValue();
2418 }
2419 
2420 /// SignBitIsZero - Return true if the sign bit of Op is known to be zero.  We
2421 /// use this predicate to simplify operations downstream.
2422 bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
2423   unsigned BitWidth = Op.getScalarValueSizeInBits();
2424   return MaskedValueIsZero(Op, APInt::getSignMask(BitWidth), Depth);
2425 }
2426 
2427 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero.  We use
2428 /// this predicate to simplify operations downstream.  Mask is known to be zero
2429 /// for bits that V cannot have.
2430 bool SelectionDAG::MaskedValueIsZero(SDValue V, const APInt &Mask,
2431                                      unsigned Depth) const {
2432   return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2433 }
2434 
2435 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2436 /// DemandedElts.  We use this predicate to simplify operations downstream.
2437 /// Mask is known to be zero for bits that V cannot have.
2438 bool SelectionDAG::MaskedValueIsZero(SDValue V, const APInt &Mask,
2439                                      const APInt &DemandedElts,
2440                                      unsigned Depth) const {
2441   return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2442 }
2443 
2444 /// Return true if the DemandedElts of the vector Op are all zero.  We
2445 /// use this predicate to simplify operations downstream.
2446 bool SelectionDAG::MaskedElementsAreZero(SDValue Op, const APInt &DemandedElts,
2447                                          unsigned Depth) const {
2448   unsigned BitWidth = Op.getScalarValueSizeInBits();
2449   APInt DemandedBits = APInt::getAllOnesValue(BitWidth);
2450   return MaskedValueIsZero(Op, DemandedBits, DemandedElts, Depth);
2451 }
2452 
2453 /// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2454 bool SelectionDAG::MaskedValueIsAllOnes(SDValue V, const APInt &Mask,
2455                                         unsigned Depth) const {
2456   return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2457 }
2458 
2459 /// isSplatValue - Return true if the vector V has the same value
2460 /// across all DemandedElts. For scalable vectors it does not make
2461 /// sense to specify which elements are demanded or undefined, therefore
2462 /// they are simply ignored.
2463 bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2464                                 APInt &UndefElts, unsigned Depth) {
2465   EVT VT = V.getValueType();
2466   assert(VT.isVector() && "Vector type expected");
2467 
2468   if (!VT.isScalableVector() && !DemandedElts)
2469     return false; // No demanded elts, better to assume we don't know anything.
2470 
2471   if (Depth >= MaxRecursionDepth)
2472     return false; // Limit search depth.
2473 
2474   // Deal with some common cases here that work for both fixed and scalable
2475   // vector types.
2476   switch (V.getOpcode()) {
2477   case ISD::SPLAT_VECTOR:
2478     UndefElts = V.getOperand(0).isUndef()
2479                     ? APInt::getAllOnesValue(DemandedElts.getBitWidth())
2480                     : APInt(DemandedElts.getBitWidth(), 0);
2481     return true;
2482   case ISD::ADD:
2483   case ISD::SUB:
2484   case ISD::AND:
2485   case ISD::XOR:
2486   case ISD::OR: {
2487     APInt UndefLHS, UndefRHS;
2488     SDValue LHS = V.getOperand(0);
2489     SDValue RHS = V.getOperand(1);
2490     if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2491         isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1)) {
2492       UndefElts = UndefLHS | UndefRHS;
2493       return true;
2494     }
2495     return false;
2496   }
2497   case ISD::ABS:
2498   case ISD::TRUNCATE:
2499   case ISD::SIGN_EXTEND:
2500   case ISD::ZERO_EXTEND:
2501     return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2502   }
2503 
2504   // We don't support other cases than those above for scalable vectors at
2505   // the moment.
2506   if (VT.isScalableVector())
2507     return false;
2508 
2509   unsigned NumElts = VT.getVectorNumElements();
2510   assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
2511   UndefElts = APInt::getNullValue(NumElts);
2512 
2513   switch (V.getOpcode()) {
2514   case ISD::BUILD_VECTOR: {
2515     SDValue Scl;
2516     for (unsigned i = 0; i != NumElts; ++i) {
2517       SDValue Op = V.getOperand(i);
2518       if (Op.isUndef()) {
2519         UndefElts.setBit(i);
2520         continue;
2521       }
2522       if (!DemandedElts[i])
2523         continue;
2524       if (Scl && Scl != Op)
2525         return false;
2526       Scl = Op;
2527     }
2528     return true;
2529   }
2530   case ISD::VECTOR_SHUFFLE: {
2531     // Check if this is a shuffle node doing a splat.
2532     // TODO: Do we need to handle shuffle(splat, undef, mask)?
2533     int SplatIndex = -1;
2534     ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
2535     for (int i = 0; i != (int)NumElts; ++i) {
2536       int M = Mask[i];
2537       if (M < 0) {
2538         UndefElts.setBit(i);
2539         continue;
2540       }
2541       if (!DemandedElts[i])
2542         continue;
2543       if (0 <= SplatIndex && SplatIndex != M)
2544         return false;
2545       SplatIndex = M;
2546     }
2547     return true;
2548   }
2549   case ISD::EXTRACT_SUBVECTOR: {
2550     // Offset the demanded elts by the subvector index.
2551     SDValue Src = V.getOperand(0);
2552     // We don't support scalable vectors at the moment.
2553     if (Src.getValueType().isScalableVector())
2554       return false;
2555     uint64_t Idx = V.getConstantOperandVal(1);
2556     unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2557     APInt UndefSrcElts;
2558     APInt DemandedSrcElts = DemandedElts.zextOrSelf(NumSrcElts).shl(Idx);
2559     if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2560       UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
2561       return true;
2562     }
2563     break;
2564   }
2565   }
2566 
2567   return false;
2568 }
2569 
2570 /// Helper wrapper to main isSplatValue function.
2571 bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) {
2572   EVT VT = V.getValueType();
2573   assert(VT.isVector() && "Vector type expected");
2574 
2575   APInt UndefElts;
2576   APInt DemandedElts;
2577 
2578   // For now we don't support this with scalable vectors.
2579   if (!VT.isScalableVector())
2580     DemandedElts = APInt::getAllOnesValue(VT.getVectorNumElements());
2581   return isSplatValue(V, DemandedElts, UndefElts) &&
2582          (AllowUndefs || !UndefElts);
2583 }
2584 
2585 SDValue SelectionDAG::getSplatSourceVector(SDValue V, int &SplatIdx) {
2586   V = peekThroughExtractSubvectors(V);
2587 
2588   EVT VT = V.getValueType();
2589   unsigned Opcode = V.getOpcode();
2590   switch (Opcode) {
2591   default: {
2592     APInt UndefElts;
2593     APInt DemandedElts;
2594 
2595     if (!VT.isScalableVector())
2596       DemandedElts = APInt::getAllOnesValue(VT.getVectorNumElements());
2597 
2598     if (isSplatValue(V, DemandedElts, UndefElts)) {
2599       if (VT.isScalableVector()) {
2600         // DemandedElts and UndefElts are ignored for scalable vectors, since
2601         // the only supported cases are SPLAT_VECTOR nodes.
2602         SplatIdx = 0;
2603       } else {
2604         // Handle case where all demanded elements are UNDEF.
2605         if (DemandedElts.isSubsetOf(UndefElts)) {
2606           SplatIdx = 0;
2607           return getUNDEF(VT);
2608         }
2609         SplatIdx = (UndefElts & DemandedElts).countTrailingOnes();
2610       }
2611       return V;
2612     }
2613     break;
2614   }
2615   case ISD::SPLAT_VECTOR:
2616     SplatIdx = 0;
2617     return V;
2618   case ISD::VECTOR_SHUFFLE: {
2619     if (VT.isScalableVector())
2620       return SDValue();
2621 
2622     // Check if this is a shuffle node doing a splat.
2623     // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
2624     // getTargetVShiftNode currently struggles without the splat source.
2625     auto *SVN = cast<ShuffleVectorSDNode>(V);
2626     if (!SVN->isSplat())
2627       break;
2628     int Idx = SVN->getSplatIndex();
2629     int NumElts = V.getValueType().getVectorNumElements();
2630     SplatIdx = Idx % NumElts;
2631     return V.getOperand(Idx / NumElts);
2632   }
2633   }
2634 
2635   return SDValue();
2636 }
2637 
2638 SDValue SelectionDAG::getSplatValue(SDValue V, bool LegalTypes) {
2639   int SplatIdx;
2640   if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
2641     EVT SVT = SrcVector.getValueType().getScalarType();
2642     EVT LegalSVT = SVT;
2643     if (LegalTypes && !TLI->isTypeLegal(SVT)) {
2644       if (!SVT.isInteger())
2645         return SDValue();
2646       LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
2647       if (LegalSVT.bitsLT(SVT))
2648         return SDValue();
2649     }
2650     return getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(V), LegalSVT, SrcVector,
2651                    getVectorIdxConstant(SplatIdx, SDLoc(V)));
2652   }
2653   return SDValue();
2654 }
2655 
2656 const APInt *
2657 SelectionDAG::getValidShiftAmountConstant(SDValue V,
2658                                           const APInt &DemandedElts) const {
2659   assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
2660           V.getOpcode() == ISD::SRA) &&
2661          "Unknown shift node");
2662   unsigned BitWidth = V.getScalarValueSizeInBits();
2663   if (ConstantSDNode *SA = isConstOrConstSplat(V.getOperand(1), DemandedElts)) {
2664     // Shifting more than the bitwidth is not valid.
2665     const APInt &ShAmt = SA->getAPIntValue();
2666     if (ShAmt.ult(BitWidth))
2667       return &ShAmt;
2668   }
2669   return nullptr;
2670 }
2671 
2672 const APInt *SelectionDAG::getValidMinimumShiftAmountConstant(
2673     SDValue V, const APInt &DemandedElts) const {
2674   assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
2675           V.getOpcode() == ISD::SRA) &&
2676          "Unknown shift node");
2677   if (const APInt *ValidAmt = getValidShiftAmountConstant(V, DemandedElts))
2678     return ValidAmt;
2679   unsigned BitWidth = V.getScalarValueSizeInBits();
2680   auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1));
2681   if (!BV)
2682     return nullptr;
2683   const APInt *MinShAmt = nullptr;
2684   for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
2685     if (!DemandedElts[i])
2686       continue;
2687     auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
2688     if (!SA)
2689       return nullptr;
2690     // Shifting more than the bitwidth is not valid.
2691     const APInt &ShAmt = SA->getAPIntValue();
2692     if (ShAmt.uge(BitWidth))
2693       return nullptr;
2694     if (MinShAmt && MinShAmt->ule(ShAmt))
2695       continue;
2696     MinShAmt = &ShAmt;
2697   }
2698   return MinShAmt;
2699 }
2700 
2701 const APInt *SelectionDAG::getValidMaximumShiftAmountConstant(
2702     SDValue V, const APInt &DemandedElts) const {
2703   assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
2704           V.getOpcode() == ISD::SRA) &&
2705          "Unknown shift node");
2706   if (const APInt *ValidAmt = getValidShiftAmountConstant(V, DemandedElts))
2707     return ValidAmt;
2708   unsigned BitWidth = V.getScalarValueSizeInBits();
2709   auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1));
2710   if (!BV)
2711     return nullptr;
2712   const APInt *MaxShAmt = nullptr;
2713   for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
2714     if (!DemandedElts[i])
2715       continue;
2716     auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
2717     if (!SA)
2718       return nullptr;
2719     // Shifting more than the bitwidth is not valid.
2720     const APInt &ShAmt = SA->getAPIntValue();
2721     if (ShAmt.uge(BitWidth))
2722       return nullptr;
2723     if (MaxShAmt && MaxShAmt->uge(ShAmt))
2724       continue;
2725     MaxShAmt = &ShAmt;
2726   }
2727   return MaxShAmt;
2728 }
2729 
2730 /// Determine which bits of Op are known to be either zero or one and return
2731 /// them in Known. For vectors, the known bits are those that are shared by
2732 /// every vector element.
2733 KnownBits SelectionDAG::computeKnownBits(SDValue Op, unsigned Depth) const {
2734   EVT VT = Op.getValueType();
2735 
2736   // TOOD: Until we have a plan for how to represent demanded elements for
2737   // scalable vectors, we can just bail out for now.
2738   if (Op.getValueType().isScalableVector()) {
2739     unsigned BitWidth = Op.getScalarValueSizeInBits();
2740     return KnownBits(BitWidth);
2741   }
2742 
2743   APInt DemandedElts = VT.isVector()
2744                            ? APInt::getAllOnesValue(VT.getVectorNumElements())
2745                            : APInt(1, 1);
2746   return computeKnownBits(Op, DemandedElts, Depth);
2747 }
2748 
2749 /// Determine which bits of Op are known to be either zero or one and return
2750 /// them in Known. The DemandedElts argument allows us to only collect the known
2751 /// bits that are shared by the requested vector elements.
2752 KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
2753                                          unsigned Depth) const {
2754   unsigned BitWidth = Op.getScalarValueSizeInBits();
2755 
2756   KnownBits Known(BitWidth);   // Don't know anything.
2757 
2758   // TOOD: Until we have a plan for how to represent demanded elements for
2759   // scalable vectors, we can just bail out for now.
2760   if (Op.getValueType().isScalableVector())
2761     return Known;
2762 
2763   if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
2764     // We know all of the bits for a constant!
2765     return KnownBits::makeConstant(C->getAPIntValue());
2766   }
2767   if (auto *C = dyn_cast<ConstantFPSDNode>(Op)) {
2768     // We know all of the bits for a constant fp!
2769     return KnownBits::makeConstant(C->getValueAPF().bitcastToAPInt());
2770   }
2771 
2772   if (Depth >= MaxRecursionDepth)
2773     return Known;  // Limit search depth.
2774 
2775   KnownBits Known2;
2776   unsigned NumElts = DemandedElts.getBitWidth();
2777   assert((!Op.getValueType().isVector() ||
2778           NumElts == Op.getValueType().getVectorNumElements()) &&
2779          "Unexpected vector size");
2780 
2781   if (!DemandedElts)
2782     return Known;  // No demanded elts, better to assume we don't know anything.
2783 
2784   unsigned Opcode = Op.getOpcode();
2785   switch (Opcode) {
2786   case ISD::BUILD_VECTOR:
2787     // Collect the known bits that are shared by every demanded vector element.
2788     Known.Zero.setAllBits(); Known.One.setAllBits();
2789     for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
2790       if (!DemandedElts[i])
2791         continue;
2792 
2793       SDValue SrcOp = Op.getOperand(i);
2794       Known2 = computeKnownBits(SrcOp, Depth + 1);
2795 
2796       // BUILD_VECTOR can implicitly truncate sources, we must handle this.
2797       if (SrcOp.getValueSizeInBits() != BitWidth) {
2798         assert(SrcOp.getValueSizeInBits() > BitWidth &&
2799                "Expected BUILD_VECTOR implicit truncation");
2800         Known2 = Known2.trunc(BitWidth);
2801       }
2802 
2803       // Known bits are the values that are shared by every demanded element.
2804       Known = KnownBits::commonBits(Known, Known2);
2805 
2806       // If we don't know any bits, early out.
2807       if (Known.isUnknown())
2808         break;
2809     }
2810     break;
2811   case ISD::VECTOR_SHUFFLE: {
2812     // Collect the known bits that are shared by every vector element referenced
2813     // by the shuffle.
2814     APInt DemandedLHS(NumElts, 0), DemandedRHS(NumElts, 0);
2815     Known.Zero.setAllBits(); Known.One.setAllBits();
2816     const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
2817     assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
2818     for (unsigned i = 0; i != NumElts; ++i) {
2819       if (!DemandedElts[i])
2820         continue;
2821 
2822       int M = SVN->getMaskElt(i);
2823       if (M < 0) {
2824         // For UNDEF elements, we don't know anything about the common state of
2825         // the shuffle result.
2826         Known.resetAll();
2827         DemandedLHS.clearAllBits();
2828         DemandedRHS.clearAllBits();
2829         break;
2830       }
2831 
2832       if ((unsigned)M < NumElts)
2833         DemandedLHS.setBit((unsigned)M % NumElts);
2834       else
2835         DemandedRHS.setBit((unsigned)M % NumElts);
2836     }
2837     // Known bits are the values that are shared by every demanded element.
2838     if (!!DemandedLHS) {
2839       SDValue LHS = Op.getOperand(0);
2840       Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
2841       Known = KnownBits::commonBits(Known, Known2);
2842     }
2843     // If we don't know any bits, early out.
2844     if (Known.isUnknown())
2845       break;
2846     if (!!DemandedRHS) {
2847       SDValue RHS = Op.getOperand(1);
2848       Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
2849       Known = KnownBits::commonBits(Known, Known2);
2850     }
2851     break;
2852   }
2853   case ISD::CONCAT_VECTORS: {
2854     // Split DemandedElts and test each of the demanded subvectors.
2855     Known.Zero.setAllBits(); Known.One.setAllBits();
2856     EVT SubVectorVT = Op.getOperand(0).getValueType();
2857     unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
2858     unsigned NumSubVectors = Op.getNumOperands();
2859     for (unsigned i = 0; i != NumSubVectors; ++i) {
2860       APInt DemandedSub =
2861           DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
2862       if (!!DemandedSub) {
2863         SDValue Sub = Op.getOperand(i);
2864         Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
2865         Known = KnownBits::commonBits(Known, Known2);
2866       }
2867       // If we don't know any bits, early out.
2868       if (Known.isUnknown())
2869         break;
2870     }
2871     break;
2872   }
2873   case ISD::INSERT_SUBVECTOR: {
2874     // Demand any elements from the subvector and the remainder from the src its
2875     // inserted into.
2876     SDValue Src = Op.getOperand(0);
2877     SDValue Sub = Op.getOperand(1);
2878     uint64_t Idx = Op.getConstantOperandVal(2);
2879     unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
2880     APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
2881     APInt DemandedSrcElts = DemandedElts;
2882     DemandedSrcElts.insertBits(APInt::getNullValue(NumSubElts), Idx);
2883 
2884     Known.One.setAllBits();
2885     Known.Zero.setAllBits();
2886     if (!!DemandedSubElts) {
2887       Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
2888       if (Known.isUnknown())
2889         break; // early-out.
2890     }
2891     if (!!DemandedSrcElts) {
2892       Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
2893       Known = KnownBits::commonBits(Known, Known2);
2894     }
2895     break;
2896   }
2897   case ISD::EXTRACT_SUBVECTOR: {
2898     // Offset the demanded elts by the subvector index.
2899     SDValue Src = Op.getOperand(0);
2900     // Bail until we can represent demanded elements for scalable vectors.
2901     if (Src.getValueType().isScalableVector())
2902       break;
2903     uint64_t Idx = Op.getConstantOperandVal(1);
2904     unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2905     APInt DemandedSrcElts = DemandedElts.zextOrSelf(NumSrcElts).shl(Idx);
2906     Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
2907     break;
2908   }
2909   case ISD::SCALAR_TO_VECTOR: {
2910     // We know about scalar_to_vector as much as we know about it source,
2911     // which becomes the first element of otherwise unknown vector.
2912     if (DemandedElts != 1)
2913       break;
2914 
2915     SDValue N0 = Op.getOperand(0);
2916     Known = computeKnownBits(N0, Depth + 1);
2917     if (N0.getValueSizeInBits() != BitWidth)
2918       Known = Known.trunc(BitWidth);
2919 
2920     break;
2921   }
2922   case ISD::BITCAST: {
2923     SDValue N0 = Op.getOperand(0);
2924     EVT SubVT = N0.getValueType();
2925     unsigned SubBitWidth = SubVT.getScalarSizeInBits();
2926 
2927     // Ignore bitcasts from unsupported types.
2928     if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
2929       break;
2930 
2931     // Fast handling of 'identity' bitcasts.
2932     if (BitWidth == SubBitWidth) {
2933       Known = computeKnownBits(N0, DemandedElts, Depth + 1);
2934       break;
2935     }
2936 
2937     bool IsLE = getDataLayout().isLittleEndian();
2938 
2939     // Bitcast 'small element' vector to 'large element' scalar/vector.
2940     if ((BitWidth % SubBitWidth) == 0) {
2941       assert(N0.getValueType().isVector() && "Expected bitcast from vector");
2942 
2943       // Collect known bits for the (larger) output by collecting the known
2944       // bits from each set of sub elements and shift these into place.
2945       // We need to separately call computeKnownBits for each set of
2946       // sub elements as the knownbits for each is likely to be different.
2947       unsigned SubScale = BitWidth / SubBitWidth;
2948       APInt SubDemandedElts(NumElts * SubScale, 0);
2949       for (unsigned i = 0; i != NumElts; ++i)
2950         if (DemandedElts[i])
2951           SubDemandedElts.setBit(i * SubScale);
2952 
2953       for (unsigned i = 0; i != SubScale; ++i) {
2954         Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
2955                          Depth + 1);
2956         unsigned Shifts = IsLE ? i : SubScale - 1 - i;
2957         Known.insertBits(Known2, SubBitWidth * Shifts);
2958       }
2959     }
2960 
2961     // Bitcast 'large element' scalar/vector to 'small element' vector.
2962     if ((SubBitWidth % BitWidth) == 0) {
2963       assert(Op.getValueType().isVector() && "Expected bitcast to vector");
2964 
2965       // Collect known bits for the (smaller) output by collecting the known
2966       // bits from the overlapping larger input elements and extracting the
2967       // sub sections we actually care about.
2968       unsigned SubScale = SubBitWidth / BitWidth;
2969       APInt SubDemandedElts(NumElts / SubScale, 0);
2970       for (unsigned i = 0; i != NumElts; ++i)
2971         if (DemandedElts[i])
2972           SubDemandedElts.setBit(i / SubScale);
2973 
2974       Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
2975 
2976       Known.Zero.setAllBits(); Known.One.setAllBits();
2977       for (unsigned i = 0; i != NumElts; ++i)
2978         if (DemandedElts[i]) {
2979           unsigned Shifts = IsLE ? i : NumElts - 1 - i;
2980           unsigned Offset = (Shifts % SubScale) * BitWidth;
2981           Known = KnownBits::commonBits(Known,
2982                                         Known2.extractBits(BitWidth, Offset));
2983           // If we don't know any bits, early out.
2984           if (Known.isUnknown())
2985             break;
2986         }
2987     }
2988     break;
2989   }
2990   case ISD::AND:
2991     Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
2992     Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
2993 
2994     Known &= Known2;
2995     break;
2996   case ISD::OR:
2997     Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
2998     Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
2999 
3000     Known |= Known2;
3001     break;
3002   case ISD::XOR:
3003     Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3004     Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3005 
3006     Known ^= Known2;
3007     break;
3008   case ISD::MUL: {
3009     Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3010     Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3011     Known = KnownBits::mul(Known, Known2);
3012     break;
3013   }
3014   case ISD::MULHU: {
3015     Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3016     Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3017     Known = KnownBits::mulhu(Known, Known2);
3018     break;
3019   }
3020   case ISD::MULHS: {
3021     Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3022     Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3023     Known = KnownBits::mulhs(Known, Known2);
3024     break;
3025   }
3026   case ISD::UMUL_LOHI: {
3027     assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3028     Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3029     Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3030     if (Op.getResNo() == 0)
3031       Known = KnownBits::mul(Known, Known2);
3032     else
3033       Known = KnownBits::mulhu(Known, Known2);
3034     break;
3035   }
3036   case ISD::SMUL_LOHI: {
3037     assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3038     Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3039     Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3040     if (Op.getResNo() == 0)
3041       Known = KnownBits::mul(Known, Known2);
3042     else
3043       Known = KnownBits::mulhs(Known, Known2);
3044     break;
3045   }
3046   case ISD::UDIV: {
3047     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3048     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3049     Known = KnownBits::udiv(Known, Known2);
3050     break;
3051   }
3052   case ISD::SELECT:
3053   case ISD::VSELECT:
3054     Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3055     // If we don't know any bits, early out.
3056     if (Known.isUnknown())
3057       break;
3058     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3059 
3060     // Only known if known in both the LHS and RHS.
3061     Known = KnownBits::commonBits(Known, Known2);
3062     break;
3063   case ISD::SELECT_CC:
3064     Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3065     // If we don't know any bits, early out.
3066     if (Known.isUnknown())
3067       break;
3068     Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3069 
3070     // Only known if known in both the LHS and RHS.
3071     Known = KnownBits::commonBits(Known, Known2);
3072     break;
3073   case ISD::SMULO:
3074   case ISD::UMULO:
3075     if (Op.getResNo() != 1)
3076       break;
3077     // The boolean result conforms to getBooleanContents.
3078     // If we know the result of a setcc has the top bits zero, use this info.
3079     // We know that we have an integer-based boolean since these operations
3080     // are only available for integer.
3081     if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3082             TargetLowering::ZeroOrOneBooleanContent &&
3083         BitWidth > 1)
3084       Known.Zero.setBitsFrom(1);
3085     break;
3086   case ISD::SETCC:
3087   case ISD::STRICT_FSETCC:
3088   case ISD::STRICT_FSETCCS: {
3089     unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3090     // If we know the result of a setcc has the top bits zero, use this info.
3091     if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3092             TargetLowering::ZeroOrOneBooleanContent &&
3093         BitWidth > 1)
3094       Known.Zero.setBitsFrom(1);
3095     break;
3096   }
3097   case ISD::SHL:
3098     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3099     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3100     Known = KnownBits::shl(Known, Known2);
3101 
3102     // Minimum shift low bits are known zero.
3103     if (const APInt *ShMinAmt =
3104             getValidMinimumShiftAmountConstant(Op, DemandedElts))
3105       Known.Zero.setLowBits(ShMinAmt->getZExtValue());
3106     break;
3107   case ISD::SRL:
3108     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3109     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3110     Known = KnownBits::lshr(Known, Known2);
3111 
3112     // Minimum shift high bits are known zero.
3113     if (const APInt *ShMinAmt =
3114             getValidMinimumShiftAmountConstant(Op, DemandedElts))
3115       Known.Zero.setHighBits(ShMinAmt->getZExtValue());
3116     break;
3117   case ISD::SRA:
3118     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3119     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3120     Known = KnownBits::ashr(Known, Known2);
3121     // TODO: Add minimum shift high known sign bits.
3122     break;
3123   case ISD::FSHL:
3124   case ISD::FSHR:
3125     if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3126       unsigned Amt = C->getAPIntValue().urem(BitWidth);
3127 
3128       // For fshl, 0-shift returns the 1st arg.
3129       // For fshr, 0-shift returns the 2nd arg.
3130       if (Amt == 0) {
3131         Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3132                                  DemandedElts, Depth + 1);
3133         break;
3134       }
3135 
3136       // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3137       // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3138       Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3139       Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3140       if (Opcode == ISD::FSHL) {
3141         Known.One <<= Amt;
3142         Known.Zero <<= Amt;
3143         Known2.One.lshrInPlace(BitWidth - Amt);
3144         Known2.Zero.lshrInPlace(BitWidth - Amt);
3145       } else {
3146         Known.One <<= BitWidth - Amt;
3147         Known.Zero <<= BitWidth - Amt;
3148         Known2.One.lshrInPlace(Amt);
3149         Known2.Zero.lshrInPlace(Amt);
3150       }
3151       Known.One |= Known2.One;
3152       Known.Zero |= Known2.Zero;
3153     }
3154     break;
3155   case ISD::SIGN_EXTEND_INREG: {
3156     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3157     EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3158     Known = Known.sextInReg(EVT.getScalarSizeInBits());
3159     break;
3160   }
3161   case ISD::CTTZ:
3162   case ISD::CTTZ_ZERO_UNDEF: {
3163     Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3164     // If we have a known 1, its position is our upper bound.
3165     unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3166     unsigned LowBits = Log2_32(PossibleTZ) + 1;
3167     Known.Zero.setBitsFrom(LowBits);
3168     break;
3169   }
3170   case ISD::CTLZ:
3171   case ISD::CTLZ_ZERO_UNDEF: {
3172     Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3173     // If we have a known 1, its position is our upper bound.
3174     unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3175     unsigned LowBits = Log2_32(PossibleLZ) + 1;
3176     Known.Zero.setBitsFrom(LowBits);
3177     break;
3178   }
3179   case ISD::CTPOP: {
3180     Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3181     // If we know some of the bits are zero, they can't be one.
3182     unsigned PossibleOnes = Known2.countMaxPopulation();
3183     Known.Zero.setBitsFrom(Log2_32(PossibleOnes) + 1);
3184     break;
3185   }
3186   case ISD::PARITY: {
3187     // Parity returns 0 everywhere but the LSB.
3188     Known.Zero.setBitsFrom(1);
3189     break;
3190   }
3191   case ISD::LOAD: {
3192     LoadSDNode *LD = cast<LoadSDNode>(Op);
3193     const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3194     if (ISD::isNON_EXTLoad(LD) && Cst) {
3195       // Determine any common known bits from the loaded constant pool value.
3196       Type *CstTy = Cst->getType();
3197       if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits()) {
3198         // If its a vector splat, then we can (quickly) reuse the scalar path.
3199         // NOTE: We assume all elements match and none are UNDEF.
3200         if (CstTy->isVectorTy()) {
3201           if (const Constant *Splat = Cst->getSplatValue()) {
3202             Cst = Splat;
3203             CstTy = Cst->getType();
3204           }
3205         }
3206         // TODO - do we need to handle different bitwidths?
3207         if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3208           // Iterate across all vector elements finding common known bits.
3209           Known.One.setAllBits();
3210           Known.Zero.setAllBits();
3211           for (unsigned i = 0; i != NumElts; ++i) {
3212             if (!DemandedElts[i])
3213               continue;
3214             if (Constant *Elt = Cst->getAggregateElement(i)) {
3215               if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3216                 const APInt &Value = CInt->getValue();
3217                 Known.One &= Value;
3218                 Known.Zero &= ~Value;
3219                 continue;
3220               }
3221               if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3222                 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3223                 Known.One &= Value;
3224                 Known.Zero &= ~Value;
3225                 continue;
3226               }
3227             }
3228             Known.One.clearAllBits();
3229             Known.Zero.clearAllBits();
3230             break;
3231           }
3232         } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3233           if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3234             Known = KnownBits::makeConstant(CInt->getValue());
3235           } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3236             Known =
3237                 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
3238           }
3239         }
3240       }
3241     } else if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
3242       // If this is a ZEXTLoad and we are looking at the loaded value.
3243       EVT VT = LD->getMemoryVT();
3244       unsigned MemBits = VT.getScalarSizeInBits();
3245       Known.Zero.setBitsFrom(MemBits);
3246     } else if (const MDNode *Ranges = LD->getRanges()) {
3247       if (LD->getExtensionType() == ISD::NON_EXTLOAD)
3248         computeKnownBitsFromRangeMetadata(*Ranges, Known);
3249     }
3250     break;
3251   }
3252   case ISD::ZERO_EXTEND_VECTOR_INREG: {
3253     EVT InVT = Op.getOperand(0).getValueType();
3254     APInt InDemandedElts = DemandedElts.zextOrSelf(InVT.getVectorNumElements());
3255     Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3256     Known = Known.zext(BitWidth);
3257     break;
3258   }
3259   case ISD::ZERO_EXTEND: {
3260     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3261     Known = Known.zext(BitWidth);
3262     break;
3263   }
3264   case ISD::SIGN_EXTEND_VECTOR_INREG: {
3265     EVT InVT = Op.getOperand(0).getValueType();
3266     APInt InDemandedElts = DemandedElts.zextOrSelf(InVT.getVectorNumElements());
3267     Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3268     // If the sign bit is known to be zero or one, then sext will extend
3269     // it to the top bits, else it will just zext.
3270     Known = Known.sext(BitWidth);
3271     break;
3272   }
3273   case ISD::SIGN_EXTEND: {
3274     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3275     // If the sign bit is known to be zero or one, then sext will extend
3276     // it to the top bits, else it will just zext.
3277     Known = Known.sext(BitWidth);
3278     break;
3279   }
3280   case ISD::ANY_EXTEND_VECTOR_INREG: {
3281     EVT InVT = Op.getOperand(0).getValueType();
3282     APInt InDemandedElts = DemandedElts.zextOrSelf(InVT.getVectorNumElements());
3283     Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3284     Known = Known.anyext(BitWidth);
3285     break;
3286   }
3287   case ISD::ANY_EXTEND: {
3288     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3289     Known = Known.anyext(BitWidth);
3290     break;
3291   }
3292   case ISD::TRUNCATE: {
3293     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3294     Known = Known.trunc(BitWidth);
3295     break;
3296   }
3297   case ISD::AssertZext: {
3298     EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3299     APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
3300     Known = computeKnownBits(Op.getOperand(0), Depth+1);
3301     Known.Zero |= (~InMask);
3302     Known.One  &= (~Known.Zero);
3303     break;
3304   }
3305   case ISD::AssertAlign: {
3306     unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
3307     assert(LogOfAlign != 0);
3308     // If a node is guaranteed to be aligned, set low zero bits accordingly as
3309     // well as clearing one bits.
3310     Known.Zero.setLowBits(LogOfAlign);
3311     Known.One.clearLowBits(LogOfAlign);
3312     break;
3313   }
3314   case ISD::FGETSIGN:
3315     // All bits are zero except the low bit.
3316     Known.Zero.setBitsFrom(1);
3317     break;
3318   case ISD::USUBO:
3319   case ISD::SSUBO:
3320     if (Op.getResNo() == 1) {
3321       // If we know the result of a setcc has the top bits zero, use this info.
3322       if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
3323               TargetLowering::ZeroOrOneBooleanContent &&
3324           BitWidth > 1)
3325         Known.Zero.setBitsFrom(1);
3326       break;
3327     }
3328     LLVM_FALLTHROUGH;
3329   case ISD::SUB:
3330   case ISD::SUBC: {
3331     assert(Op.getResNo() == 0 &&
3332            "We only compute knownbits for the difference here.");
3333 
3334     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3335     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3336     Known = KnownBits::computeForAddSub(/* Add */ false, /* NSW */ false,
3337                                         Known, Known2);
3338     break;
3339   }
3340   case ISD::UADDO:
3341   case ISD::SADDO:
3342   case ISD::ADDCARRY:
3343     if (Op.getResNo() == 1) {
3344       // If we know the result of a setcc has the top bits zero, use this info.
3345       if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
3346               TargetLowering::ZeroOrOneBooleanContent &&
3347           BitWidth > 1)
3348         Known.Zero.setBitsFrom(1);
3349       break;
3350     }
3351     LLVM_FALLTHROUGH;
3352   case ISD::ADD:
3353   case ISD::ADDC:
3354   case ISD::ADDE: {
3355     assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
3356 
3357     // With ADDE and ADDCARRY, a carry bit may be added in.
3358     KnownBits Carry(1);
3359     if (Opcode == ISD::ADDE)
3360       // Can't track carry from glue, set carry to unknown.
3361       Carry.resetAll();
3362     else if (Opcode == ISD::ADDCARRY)
3363       // TODO: Compute known bits for the carry operand. Not sure if it is worth
3364       // the trouble (how often will we find a known carry bit). And I haven't
3365       // tested this very much yet, but something like this might work:
3366       //   Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3367       //   Carry = Carry.zextOrTrunc(1, false);
3368       Carry.resetAll();
3369     else
3370       Carry.setAllZero();
3371 
3372     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3373     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3374     Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
3375     break;
3376   }
3377   case ISD::SREM: {
3378     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3379     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3380     Known = KnownBits::srem(Known, Known2);
3381     break;
3382   }
3383   case ISD::UREM: {
3384     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3385     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3386     Known = KnownBits::urem(Known, Known2);
3387     break;
3388   }
3389   case ISD::EXTRACT_ELEMENT: {
3390     Known = computeKnownBits(Op.getOperand(0), Depth+1);
3391     const unsigned Index = Op.getConstantOperandVal(1);
3392     const unsigned EltBitWidth = Op.getValueSizeInBits();
3393 
3394     // Remove low part of known bits mask
3395     Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
3396     Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
3397 
3398     // Remove high part of known bit mask
3399     Known = Known.trunc(EltBitWidth);
3400     break;
3401   }
3402   case ISD::EXTRACT_VECTOR_ELT: {
3403     SDValue InVec = Op.getOperand(0);
3404     SDValue EltNo = Op.getOperand(1);
3405     EVT VecVT = InVec.getValueType();
3406     // computeKnownBits not yet implemented for scalable vectors.
3407     if (VecVT.isScalableVector())
3408       break;
3409     const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
3410     const unsigned NumSrcElts = VecVT.getVectorNumElements();
3411 
3412     // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
3413     // anything about the extended bits.
3414     if (BitWidth > EltBitWidth)
3415       Known = Known.trunc(EltBitWidth);
3416 
3417     // If we know the element index, just demand that vector element, else for
3418     // an unknown element index, ignore DemandedElts and demand them all.
3419     APInt DemandedSrcElts = APInt::getAllOnesValue(NumSrcElts);
3420     auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
3421     if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
3422       DemandedSrcElts =
3423           APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
3424 
3425     Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
3426     if (BitWidth > EltBitWidth)
3427       Known = Known.anyext(BitWidth);
3428     break;
3429   }
3430   case ISD::INSERT_VECTOR_ELT: {
3431     // If we know the element index, split the demand between the
3432     // source vector and the inserted element, otherwise assume we need
3433     // the original demanded vector elements and the value.
3434     SDValue InVec = Op.getOperand(0);
3435     SDValue InVal = Op.getOperand(1);
3436     SDValue EltNo = Op.getOperand(2);
3437     bool DemandedVal = true;
3438     APInt DemandedVecElts = DemandedElts;
3439     auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
3440     if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
3441       unsigned EltIdx = CEltNo->getZExtValue();
3442       DemandedVal = !!DemandedElts[EltIdx];
3443       DemandedVecElts.clearBit(EltIdx);
3444     }
3445     Known.One.setAllBits();
3446     Known.Zero.setAllBits();
3447     if (DemandedVal) {
3448       Known2 = computeKnownBits(InVal, Depth + 1);
3449       Known = KnownBits::commonBits(Known, Known2.zextOrTrunc(BitWidth));
3450     }
3451     if (!!DemandedVecElts) {
3452       Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
3453       Known = KnownBits::commonBits(Known, Known2);
3454     }
3455     break;
3456   }
3457   case ISD::BITREVERSE: {
3458     Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3459     Known = Known2.reverseBits();
3460     break;
3461   }
3462   case ISD::BSWAP: {
3463     Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3464     Known = Known2.byteSwap();
3465     break;
3466   }
3467   case ISD::ABS: {
3468     Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3469     Known = Known2.abs();
3470     break;
3471   }
3472   case ISD::USUBSAT: {
3473     // The result of usubsat will never be larger than the LHS.
3474     Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3475     Known.Zero.setHighBits(Known2.countMinLeadingZeros());
3476     break;
3477   }
3478   case ISD::UMIN: {
3479     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3480     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3481     Known = KnownBits::umin(Known, Known2);
3482     break;
3483   }
3484   case ISD::UMAX: {
3485     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3486     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3487     Known = KnownBits::umax(Known, Known2);
3488     break;
3489   }
3490   case ISD::SMIN:
3491   case ISD::SMAX: {
3492     // If we have a clamp pattern, we know that the number of sign bits will be
3493     // the minimum of the clamp min/max range.
3494     bool IsMax = (Opcode == ISD::SMAX);
3495     ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
3496     if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
3497       if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
3498         CstHigh =
3499             isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
3500     if (CstLow && CstHigh) {
3501       if (!IsMax)
3502         std::swap(CstLow, CstHigh);
3503 
3504       const APInt &ValueLow = CstLow->getAPIntValue();
3505       const APInt &ValueHigh = CstHigh->getAPIntValue();
3506       if (ValueLow.sle(ValueHigh)) {
3507         unsigned LowSignBits = ValueLow.getNumSignBits();
3508         unsigned HighSignBits = ValueHigh.getNumSignBits();
3509         unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
3510         if (ValueLow.isNegative() && ValueHigh.isNegative()) {
3511           Known.One.setHighBits(MinSignBits);
3512           break;
3513         }
3514         if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
3515           Known.Zero.setHighBits(MinSignBits);
3516           break;
3517         }
3518       }
3519     }
3520 
3521     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3522     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3523     if (IsMax)
3524       Known = KnownBits::smax(Known, Known2);
3525     else
3526       Known = KnownBits::smin(Known, Known2);
3527     break;
3528   }
3529   case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
3530     if (Op.getResNo() == 1) {
3531       // The boolean result conforms to getBooleanContents.
3532       // If we know the result of a setcc has the top bits zero, use this info.
3533       // We know that we have an integer-based boolean since these operations
3534       // are only available for integer.
3535       if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3536               TargetLowering::ZeroOrOneBooleanContent &&
3537           BitWidth > 1)
3538         Known.Zero.setBitsFrom(1);
3539       break;
3540     }
3541     LLVM_FALLTHROUGH;
3542   case ISD::ATOMIC_CMP_SWAP:
3543   case ISD::ATOMIC_SWAP:
3544   case ISD::ATOMIC_LOAD_ADD:
3545   case ISD::ATOMIC_LOAD_SUB:
3546   case ISD::ATOMIC_LOAD_AND:
3547   case ISD::ATOMIC_LOAD_CLR:
3548   case ISD::ATOMIC_LOAD_OR:
3549   case ISD::ATOMIC_LOAD_XOR:
3550   case ISD::ATOMIC_LOAD_NAND:
3551   case ISD::ATOMIC_LOAD_MIN:
3552   case ISD::ATOMIC_LOAD_MAX:
3553   case ISD::ATOMIC_LOAD_UMIN:
3554   case ISD::ATOMIC_LOAD_UMAX:
3555   case ISD::ATOMIC_LOAD: {
3556     unsigned MemBits =
3557         cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
3558     // If we are looking at the loaded value.
3559     if (Op.getResNo() == 0) {
3560       if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
3561         Known.Zero.setBitsFrom(MemBits);
3562     }
3563     break;
3564   }
3565   case ISD::FrameIndex:
3566   case ISD::TargetFrameIndex:
3567     TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
3568                                        Known, getMachineFunction());
3569     break;
3570 
3571   default:
3572     if (Opcode < ISD::BUILTIN_OP_END)
3573       break;
3574     LLVM_FALLTHROUGH;
3575   case ISD::INTRINSIC_WO_CHAIN:
3576   case ISD::INTRINSIC_W_CHAIN:
3577   case ISD::INTRINSIC_VOID:
3578     // Allow the target to implement this method for its nodes.
3579     TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
3580     break;
3581   }
3582 
3583   assert(!Known.hasConflict() && "Bits known to be one AND zero?");
3584   return Known;
3585 }
3586 
3587 SelectionDAG::OverflowKind SelectionDAG::computeOverflowKind(SDValue N0,
3588                                                              SDValue N1) const {
3589   // X + 0 never overflow
3590   if (isNullConstant(N1))
3591     return OFK_Never;
3592 
3593   KnownBits N1Known = computeKnownBits(N1);
3594   if (N1Known.Zero.getBoolValue()) {
3595     KnownBits N0Known = computeKnownBits(N0);
3596 
3597     bool overflow;
3598     (void)N0Known.getMaxValue().uadd_ov(N1Known.getMaxValue(), overflow);
3599     if (!overflow)
3600       return OFK_Never;
3601   }
3602 
3603   // mulhi + 1 never overflow
3604   if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
3605       (N1Known.getMaxValue() & 0x01) == N1Known.getMaxValue())
3606     return OFK_Never;
3607 
3608   if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1) {
3609     KnownBits N0Known = computeKnownBits(N0);
3610 
3611     if ((N0Known.getMaxValue() & 0x01) == N0Known.getMaxValue())
3612       return OFK_Never;
3613   }
3614 
3615   return OFK_Sometime;
3616 }
3617 
3618 bool SelectionDAG::isKnownToBeAPowerOfTwo(SDValue Val) const {
3619   EVT OpVT = Val.getValueType();
3620   unsigned BitWidth = OpVT.getScalarSizeInBits();
3621 
3622   // Is the constant a known power of 2?
3623   if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(Val))
3624     return Const->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
3625 
3626   // A left-shift of a constant one will have exactly one bit set because
3627   // shifting the bit off the end is undefined.
3628   if (Val.getOpcode() == ISD::SHL) {
3629     auto *C = isConstOrConstSplat(Val.getOperand(0));
3630     if (C && C->getAPIntValue() == 1)
3631       return true;
3632   }
3633 
3634   // Similarly, a logical right-shift of a constant sign-bit will have exactly
3635   // one bit set.
3636   if (Val.getOpcode() == ISD::SRL) {
3637     auto *C = isConstOrConstSplat(Val.getOperand(0));
3638     if (C && C->getAPIntValue().isSignMask())
3639       return true;
3640   }
3641 
3642   // Are all operands of a build vector constant powers of two?
3643   if (Val.getOpcode() == ISD::BUILD_VECTOR)
3644     if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
3645           if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
3646             return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
3647           return false;
3648         }))
3649       return true;
3650 
3651   // More could be done here, though the above checks are enough
3652   // to handle some common cases.
3653 
3654   // Fall back to computeKnownBits to catch other known cases.
3655   KnownBits Known = computeKnownBits(Val);
3656   return (Known.countMaxPopulation() == 1) && (Known.countMinPopulation() == 1);
3657 }
3658 
3659 unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const {
3660   EVT VT = Op.getValueType();
3661 
3662   // TODO: Assume we don't know anything for now.
3663   if (VT.isScalableVector())
3664     return 1;
3665 
3666   APInt DemandedElts = VT.isVector()
3667                            ? APInt::getAllOnesValue(VT.getVectorNumElements())
3668                            : APInt(1, 1);
3669   return ComputeNumSignBits(Op, DemandedElts, Depth);
3670 }
3671 
3672 unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
3673                                           unsigned Depth) const {
3674   EVT VT = Op.getValueType();
3675   assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
3676   unsigned VTBits = VT.getScalarSizeInBits();
3677   unsigned NumElts = DemandedElts.getBitWidth();
3678   unsigned Tmp, Tmp2;
3679   unsigned FirstAnswer = 1;
3680 
3681   if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
3682     const APInt &Val = C->getAPIntValue();
3683     return Val.getNumSignBits();
3684   }
3685 
3686   if (Depth >= MaxRecursionDepth)
3687     return 1;  // Limit search depth.
3688 
3689   if (!DemandedElts || VT.isScalableVector())
3690     return 1;  // No demanded elts, better to assume we don't know anything.
3691 
3692   unsigned Opcode = Op.getOpcode();
3693   switch (Opcode) {
3694   default: break;
3695   case ISD::AssertSext:
3696     Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
3697     return VTBits-Tmp+1;
3698   case ISD::AssertZext:
3699     Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
3700     return VTBits-Tmp;
3701 
3702   case ISD::BUILD_VECTOR:
3703     Tmp = VTBits;
3704     for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
3705       if (!DemandedElts[i])
3706         continue;
3707 
3708       SDValue SrcOp = Op.getOperand(i);
3709       Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
3710 
3711       // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3712       if (SrcOp.getValueSizeInBits() != VTBits) {
3713         assert(SrcOp.getValueSizeInBits() > VTBits &&
3714                "Expected BUILD_VECTOR implicit truncation");
3715         unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
3716         Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
3717       }
3718       Tmp = std::min(Tmp, Tmp2);
3719     }
3720     return Tmp;
3721 
3722   case ISD::VECTOR_SHUFFLE: {
3723     // Collect the minimum number of sign bits that are shared by every vector
3724     // element referenced by the shuffle.
3725     APInt DemandedLHS(NumElts, 0), DemandedRHS(NumElts, 0);
3726     const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
3727     assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3728     for (unsigned i = 0; i != NumElts; ++i) {
3729       int M = SVN->getMaskElt(i);
3730       if (!DemandedElts[i])
3731         continue;
3732       // For UNDEF elements, we don't know anything about the common state of
3733       // the shuffle result.
3734       if (M < 0)
3735         return 1;
3736       if ((unsigned)M < NumElts)
3737         DemandedLHS.setBit((unsigned)M % NumElts);
3738       else
3739         DemandedRHS.setBit((unsigned)M % NumElts);
3740     }
3741     Tmp = std::numeric_limits<unsigned>::max();
3742     if (!!DemandedLHS)
3743       Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
3744     if (!!DemandedRHS) {
3745       Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
3746       Tmp = std::min(Tmp, Tmp2);
3747     }
3748     // If we don't know anything, early out and try computeKnownBits fall-back.
3749     if (Tmp == 1)
3750       break;
3751     assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
3752     return Tmp;
3753   }
3754 
3755   case ISD::BITCAST: {
3756     SDValue N0 = Op.getOperand(0);
3757     EVT SrcVT = N0.getValueType();
3758     unsigned SrcBits = SrcVT.getScalarSizeInBits();
3759 
3760     // Ignore bitcasts from unsupported types..
3761     if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
3762       break;
3763 
3764     // Fast handling of 'identity' bitcasts.
3765     if (VTBits == SrcBits)
3766       return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
3767 
3768     bool IsLE = getDataLayout().isLittleEndian();
3769 
3770     // Bitcast 'large element' scalar/vector to 'small element' vector.
3771     if ((SrcBits % VTBits) == 0) {
3772       assert(VT.isVector() && "Expected bitcast to vector");
3773 
3774       unsigned Scale = SrcBits / VTBits;
3775       APInt SrcDemandedElts(NumElts / Scale, 0);
3776       for (unsigned i = 0; i != NumElts; ++i)
3777         if (DemandedElts[i])
3778           SrcDemandedElts.setBit(i / Scale);
3779 
3780       // Fast case - sign splat can be simply split across the small elements.
3781       Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
3782       if (Tmp == SrcBits)
3783         return VTBits;
3784 
3785       // Slow case - determine how far the sign extends into each sub-element.
3786       Tmp2 = VTBits;
3787       for (unsigned i = 0; i != NumElts; ++i)
3788         if (DemandedElts[i]) {
3789           unsigned SubOffset = i % Scale;
3790           SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
3791           SubOffset = SubOffset * VTBits;
3792           if (Tmp <= SubOffset)
3793             return 1;
3794           Tmp2 = std::min(Tmp2, Tmp - SubOffset);
3795         }
3796       return Tmp2;
3797     }
3798     break;
3799   }
3800 
3801   case ISD::SIGN_EXTEND:
3802     Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
3803     return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
3804   case ISD::SIGN_EXTEND_INREG:
3805     // Max of the input and what this extends.
3806     Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
3807     Tmp = VTBits-Tmp+1;
3808     Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
3809     return std::max(Tmp, Tmp2);
3810   case ISD::SIGN_EXTEND_VECTOR_INREG: {
3811     SDValue Src = Op.getOperand(0);
3812     EVT SrcVT = Src.getValueType();
3813     APInt DemandedSrcElts = DemandedElts.zextOrSelf(SrcVT.getVectorNumElements());
3814     Tmp = VTBits - SrcVT.getScalarSizeInBits();
3815     return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
3816   }
3817   case ISD::SRA:
3818     Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3819     // SRA X, C -> adds C sign bits.
3820     if (const APInt *ShAmt =
3821             getValidMinimumShiftAmountConstant(Op, DemandedElts))
3822       Tmp = std::min<uint64_t>(Tmp + ShAmt->getZExtValue(), VTBits);
3823     return Tmp;
3824   case ISD::SHL:
3825     if (const APInt *ShAmt =
3826             getValidMaximumShiftAmountConstant(Op, DemandedElts)) {
3827       // shl destroys sign bits, ensure it doesn't shift out all sign bits.
3828       Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3829       if (ShAmt->ult(Tmp))
3830         return Tmp - ShAmt->getZExtValue();
3831     }
3832     break;
3833   case ISD::AND:
3834   case ISD::OR:
3835   case ISD::XOR:    // NOT is handled here.
3836     // Logical binary ops preserve the number of sign bits at the worst.
3837     Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
3838     if (Tmp != 1) {
3839       Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
3840       FirstAnswer = std::min(Tmp, Tmp2);
3841       // We computed what we know about the sign bits as our first
3842       // answer. Now proceed to the generic code that uses
3843       // computeKnownBits, and pick whichever answer is better.
3844     }
3845     break;
3846 
3847   case ISD::SELECT:
3848   case ISD::VSELECT:
3849     Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
3850     if (Tmp == 1) return 1;  // Early out.
3851     Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
3852     return std::min(Tmp, Tmp2);
3853   case ISD::SELECT_CC:
3854     Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
3855     if (Tmp == 1) return 1;  // Early out.
3856     Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
3857     return std::min(Tmp, Tmp2);
3858 
3859   case ISD::SMIN:
3860   case ISD::SMAX: {
3861     // If we have a clamp pattern, we know that the number of sign bits will be
3862     // the minimum of the clamp min/max range.
3863     bool IsMax = (Opcode == ISD::SMAX);
3864     ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
3865     if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
3866       if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
3867         CstHigh =
3868             isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
3869     if (CstLow && CstHigh) {
3870       if (!IsMax)
3871         std::swap(CstLow, CstHigh);
3872       if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
3873         Tmp = CstLow->getAPIntValue().getNumSignBits();
3874         Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
3875         return std::min(Tmp, Tmp2);
3876       }
3877     }
3878 
3879     // Fallback - just get the minimum number of sign bits of the operands.
3880     Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3881     if (Tmp == 1)
3882       return 1;  // Early out.
3883     Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3884     return std::min(Tmp, Tmp2);
3885   }
3886   case ISD::UMIN:
3887   case ISD::UMAX:
3888     Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3889     if (Tmp == 1)
3890       return 1;  // Early out.
3891     Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3892     return std::min(Tmp, Tmp2);
3893   case ISD::SADDO:
3894   case ISD::UADDO:
3895   case ISD::SSUBO:
3896   case ISD::USUBO:
3897   case ISD::SMULO:
3898   case ISD::UMULO:
3899     if (Op.getResNo() != 1)
3900       break;
3901     // The boolean result conforms to getBooleanContents.  Fall through.
3902     // If setcc returns 0/-1, all bits are sign bits.
3903     // We know that we have an integer-based boolean since these operations
3904     // are only available for integer.
3905     if (TLI->getBooleanContents(VT.isVector(), false) ==
3906         TargetLowering::ZeroOrNegativeOneBooleanContent)
3907       return VTBits;
3908     break;
3909   case ISD::SETCC:
3910   case ISD::STRICT_FSETCC:
3911   case ISD::STRICT_FSETCCS: {
3912     unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3913     // If setcc returns 0/-1, all bits are sign bits.
3914     if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3915         TargetLowering::ZeroOrNegativeOneBooleanContent)
3916       return VTBits;
3917     break;
3918   }
3919   case ISD::ROTL:
3920   case ISD::ROTR:
3921     Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3922 
3923     // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
3924     if (Tmp == VTBits)
3925       return VTBits;
3926 
3927     if (ConstantSDNode *C =
3928             isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3929       unsigned RotAmt = C->getAPIntValue().urem(VTBits);
3930 
3931       // Handle rotate right by N like a rotate left by 32-N.
3932       if (Opcode == ISD::ROTR)
3933         RotAmt = (VTBits - RotAmt) % VTBits;
3934 
3935       // If we aren't rotating out all of the known-in sign bits, return the
3936       // number that are left.  This handles rotl(sext(x), 1) for example.
3937       if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
3938     }
3939     break;
3940   case ISD::ADD:
3941   case ISD::ADDC:
3942     // Add can have at most one carry bit.  Thus we know that the output
3943     // is, at worst, one more bit than the inputs.
3944     Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3945     if (Tmp == 1) return 1; // Early out.
3946 
3947     // Special case decrementing a value (ADD X, -1):
3948     if (ConstantSDNode *CRHS =
3949             isConstOrConstSplat(Op.getOperand(1), DemandedElts))
3950       if (CRHS->isAllOnesValue()) {
3951         KnownBits Known =
3952             computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3953 
3954         // If the input is known to be 0 or 1, the output is 0/-1, which is all
3955         // sign bits set.
3956         if ((Known.Zero | 1).isAllOnesValue())
3957           return VTBits;
3958 
3959         // If we are subtracting one from a positive number, there is no carry
3960         // out of the result.
3961         if (Known.isNonNegative())
3962           return Tmp;
3963       }
3964 
3965     Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3966     if (Tmp2 == 1) return 1; // Early out.
3967     return std::min(Tmp, Tmp2) - 1;
3968   case ISD::SUB:
3969     Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3970     if (Tmp2 == 1) return 1; // Early out.
3971 
3972     // Handle NEG.
3973     if (ConstantSDNode *CLHS =
3974             isConstOrConstSplat(Op.getOperand(0), DemandedElts))
3975       if (CLHS->isNullValue()) {
3976         KnownBits Known =
3977             computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3978         // If the input is known to be 0 or 1, the output is 0/-1, which is all
3979         // sign bits set.
3980         if ((Known.Zero | 1).isAllOnesValue())
3981           return VTBits;
3982 
3983         // If the input is known to be positive (the sign bit is known clear),
3984         // the output of the NEG has the same number of sign bits as the input.
3985         if (Known.isNonNegative())
3986           return Tmp2;
3987 
3988         // Otherwise, we treat this like a SUB.
3989       }
3990 
3991     // Sub can have at most one carry bit.  Thus we know that the output
3992     // is, at worst, one more bit than the inputs.
3993     Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3994     if (Tmp == 1) return 1; // Early out.
3995     return std::min(Tmp, Tmp2) - 1;
3996   case ISD::MUL: {
3997     // The output of the Mul can be at most twice the valid bits in the inputs.
3998     unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
3999     if (SignBitsOp0 == 1)
4000       break;
4001     unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
4002     if (SignBitsOp1 == 1)
4003       break;
4004     unsigned OutValidBits =
4005         (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
4006     return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
4007   }
4008   case ISD::SREM:
4009     // The sign bit is the LHS's sign bit, except when the result of the
4010     // remainder is zero. The magnitude of the result should be less than or
4011     // equal to the magnitude of the LHS. Therefore, the result should have
4012     // at least as many sign bits as the left hand side.
4013     return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4014   case ISD::TRUNCATE: {
4015     // Check if the sign bits of source go down as far as the truncated value.
4016     unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
4017     unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4018     if (NumSrcSignBits > (NumSrcBits - VTBits))
4019       return NumSrcSignBits - (NumSrcBits - VTBits);
4020     break;
4021   }
4022   case ISD::EXTRACT_ELEMENT: {
4023     const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
4024     const int BitWidth = Op.getValueSizeInBits();
4025     const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
4026 
4027     // Get reverse index (starting from 1), Op1 value indexes elements from
4028     // little end. Sign starts at big end.
4029     const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
4030 
4031     // If the sign portion ends in our element the subtraction gives correct
4032     // result. Otherwise it gives either negative or > bitwidth result
4033     return std::max(std::min(KnownSign - rIndex * BitWidth, BitWidth), 0);
4034   }
4035   case ISD::INSERT_VECTOR_ELT: {
4036     // If we know the element index, split the demand between the
4037     // source vector and the inserted element, otherwise assume we need
4038     // the original demanded vector elements and the value.
4039     SDValue InVec = Op.getOperand(0);
4040     SDValue InVal = Op.getOperand(1);
4041     SDValue EltNo = Op.getOperand(2);
4042     bool DemandedVal = true;
4043     APInt DemandedVecElts = DemandedElts;
4044     auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4045     if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4046       unsigned EltIdx = CEltNo->getZExtValue();
4047       DemandedVal = !!DemandedElts[EltIdx];
4048       DemandedVecElts.clearBit(EltIdx);
4049     }
4050     Tmp = std::numeric_limits<unsigned>::max();
4051     if (DemandedVal) {
4052       // TODO - handle implicit truncation of inserted elements.
4053       if (InVal.getScalarValueSizeInBits() != VTBits)
4054         break;
4055       Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
4056       Tmp = std::min(Tmp, Tmp2);
4057     }
4058     if (!!DemandedVecElts) {
4059       Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
4060       Tmp = std::min(Tmp, Tmp2);
4061     }
4062     assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4063     return Tmp;
4064   }
4065   case ISD::EXTRACT_VECTOR_ELT: {
4066     SDValue InVec = Op.getOperand(0);
4067     SDValue EltNo = Op.getOperand(1);
4068     EVT VecVT = InVec.getValueType();
4069     // ComputeNumSignBits not yet implemented for scalable vectors.
4070     if (VecVT.isScalableVector())
4071       break;
4072     const unsigned BitWidth = Op.getValueSizeInBits();
4073     const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
4074     const unsigned NumSrcElts = VecVT.getVectorNumElements();
4075 
4076     // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
4077     // anything about sign bits. But if the sizes match we can derive knowledge
4078     // about sign bits from the vector operand.
4079     if (BitWidth != EltBitWidth)
4080       break;
4081 
4082     // If we know the element index, just demand that vector element, else for
4083     // an unknown element index, ignore DemandedElts and demand them all.
4084     APInt DemandedSrcElts = APInt::getAllOnesValue(NumSrcElts);
4085     auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4086     if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4087       DemandedSrcElts =
4088           APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4089 
4090     return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
4091   }
4092   case ISD::EXTRACT_SUBVECTOR: {
4093     // Offset the demanded elts by the subvector index.
4094     SDValue Src = Op.getOperand(0);
4095     // Bail until we can represent demanded elements for scalable vectors.
4096     if (Src.getValueType().isScalableVector())
4097       break;
4098     uint64_t Idx = Op.getConstantOperandVal(1);
4099     unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
4100     APInt DemandedSrcElts = DemandedElts.zextOrSelf(NumSrcElts).shl(Idx);
4101     return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
4102   }
4103   case ISD::CONCAT_VECTORS: {
4104     // Determine the minimum number of sign bits across all demanded
4105     // elts of the input vectors. Early out if the result is already 1.
4106     Tmp = std::numeric_limits<unsigned>::max();
4107     EVT SubVectorVT = Op.getOperand(0).getValueType();
4108     unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
4109     unsigned NumSubVectors = Op.getNumOperands();
4110     for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
4111       APInt DemandedSub =
4112           DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
4113       if (!DemandedSub)
4114         continue;
4115       Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
4116       Tmp = std::min(Tmp, Tmp2);
4117     }
4118     assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4119     return Tmp;
4120   }
4121   case ISD::INSERT_SUBVECTOR: {
4122     // Demand any elements from the subvector and the remainder from the src its
4123     // inserted into.
4124     SDValue Src = Op.getOperand(0);
4125     SDValue Sub = Op.getOperand(1);
4126     uint64_t Idx = Op.getConstantOperandVal(2);
4127     unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
4128     APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
4129     APInt DemandedSrcElts = DemandedElts;
4130     DemandedSrcElts.insertBits(APInt::getNullValue(NumSubElts), Idx);
4131 
4132     Tmp = std::numeric_limits<unsigned>::max();
4133     if (!!DemandedSubElts) {
4134       Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
4135       if (Tmp == 1)
4136         return 1; // early-out
4137     }
4138     if (!!DemandedSrcElts) {
4139       Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
4140       Tmp = std::min(Tmp, Tmp2);
4141     }
4142     assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4143     return Tmp;
4144   }
4145   case ISD::ATOMIC_CMP_SWAP:
4146   case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
4147   case ISD::ATOMIC_SWAP:
4148   case ISD::ATOMIC_LOAD_ADD:
4149   case ISD::ATOMIC_LOAD_SUB:
4150   case ISD::ATOMIC_LOAD_AND:
4151   case ISD::ATOMIC_LOAD_CLR:
4152   case ISD::ATOMIC_LOAD_OR:
4153   case ISD::ATOMIC_LOAD_XOR:
4154   case ISD::ATOMIC_LOAD_NAND:
4155   case ISD::ATOMIC_LOAD_MIN:
4156   case ISD::ATOMIC_LOAD_MAX:
4157   case ISD::ATOMIC_LOAD_UMIN:
4158   case ISD::ATOMIC_LOAD_UMAX:
4159   case ISD::ATOMIC_LOAD: {
4160     Tmp = cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
4161     // If we are looking at the loaded value.
4162     if (Op.getResNo() == 0) {
4163       if (Tmp == VTBits)
4164         return 1; // early-out
4165       if (TLI->getExtendForAtomicOps() == ISD::SIGN_EXTEND)
4166         return VTBits - Tmp + 1;
4167       if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
4168         return VTBits - Tmp;
4169     }
4170     break;
4171   }
4172   }
4173 
4174   // If we are looking at the loaded value of the SDNode.
4175   if (Op.getResNo() == 0) {
4176     // Handle LOADX separately here. EXTLOAD case will fallthrough.
4177     if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
4178       unsigned ExtType = LD->getExtensionType();
4179       switch (ExtType) {
4180       default: break;
4181       case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
4182         Tmp = LD->getMemoryVT().getScalarSizeInBits();
4183         return VTBits - Tmp + 1;
4184       case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
4185         Tmp = LD->getMemoryVT().getScalarSizeInBits();
4186         return VTBits - Tmp;
4187       case ISD::NON_EXTLOAD:
4188         if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
4189           // We only need to handle vectors - computeKnownBits should handle
4190           // scalar cases.
4191           Type *CstTy = Cst->getType();
4192           if (CstTy->isVectorTy() &&
4193               (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits()) {
4194             Tmp = VTBits;
4195             for (unsigned i = 0; i != NumElts; ++i) {
4196               if (!DemandedElts[i])
4197                 continue;
4198               if (Constant *Elt = Cst->getAggregateElement(i)) {
4199                 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
4200                   const APInt &Value = CInt->getValue();
4201                   Tmp = std::min(Tmp, Value.getNumSignBits());
4202                   continue;
4203                 }
4204                 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
4205                   APInt Value = CFP->getValueAPF().bitcastToAPInt();
4206                   Tmp = std::min(Tmp, Value.getNumSignBits());
4207                   continue;
4208                 }
4209               }
4210               // Unknown type. Conservatively assume no bits match sign bit.
4211               return 1;
4212             }
4213             return Tmp;
4214           }
4215         }
4216         break;
4217       }
4218     }
4219   }
4220 
4221   // Allow the target to implement this method for its nodes.
4222   if (Opcode >= ISD::BUILTIN_OP_END ||
4223       Opcode == ISD::INTRINSIC_WO_CHAIN ||
4224       Opcode == ISD::INTRINSIC_W_CHAIN ||
4225       Opcode == ISD::INTRINSIC_VOID) {
4226     unsigned NumBits =
4227         TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
4228     if (NumBits > 1)
4229       FirstAnswer = std::max(FirstAnswer, NumBits);
4230   }
4231 
4232   // Finally, if we can prove that the top bits of the result are 0's or 1's,
4233   // use this information.
4234   KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
4235 
4236   APInt Mask;
4237   if (Known.isNonNegative()) {        // sign bit is 0
4238     Mask = Known.Zero;
4239   } else if (Known.isNegative()) {  // sign bit is 1;
4240     Mask = Known.One;
4241   } else {
4242     // Nothing known.
4243     return FirstAnswer;
4244   }
4245 
4246   // Okay, we know that the sign bit in Mask is set.  Use CLO to determine
4247   // the number of identical bits in the top of the input value.
4248   Mask <<= Mask.getBitWidth()-VTBits;
4249   return std::max(FirstAnswer, Mask.countLeadingOnes());
4250 }
4251 
4252 bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
4253   if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
4254       !isa<ConstantSDNode>(Op.getOperand(1)))
4255     return false;
4256 
4257   if (Op.getOpcode() == ISD::OR &&
4258       !MaskedValueIsZero(Op.getOperand(0), Op.getConstantOperandAPInt(1)))
4259     return false;
4260 
4261   return true;
4262 }
4263 
4264 bool SelectionDAG::isKnownNeverNaN(SDValue Op, bool SNaN, unsigned Depth) const {
4265   // If we're told that NaNs won't happen, assume they won't.
4266   if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
4267     return true;
4268 
4269   if (Depth >= MaxRecursionDepth)
4270     return false; // Limit search depth.
4271 
4272   // TODO: Handle vectors.
4273   // If the value is a constant, we can obviously see if it is a NaN or not.
4274   if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) {
4275     return !C->getValueAPF().isNaN() ||
4276            (SNaN && !C->getValueAPF().isSignaling());
4277   }
4278 
4279   unsigned Opcode = Op.getOpcode();
4280   switch (Opcode) {
4281   case ISD::FADD:
4282   case ISD::FSUB:
4283   case ISD::FMUL:
4284   case ISD::FDIV:
4285   case ISD::FREM:
4286   case ISD::FSIN:
4287   case ISD::FCOS: {
4288     if (SNaN)
4289       return true;
4290     // TODO: Need isKnownNeverInfinity
4291     return false;
4292   }
4293   case ISD::FCANONICALIZE:
4294   case ISD::FEXP:
4295   case ISD::FEXP2:
4296   case ISD::FTRUNC:
4297   case ISD::FFLOOR:
4298   case ISD::FCEIL:
4299   case ISD::FROUND:
4300   case ISD::FROUNDEVEN:
4301   case ISD::FRINT:
4302   case ISD::FNEARBYINT: {
4303     if (SNaN)
4304       return true;
4305     return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
4306   }
4307   case ISD::FABS:
4308   case ISD::FNEG:
4309   case ISD::FCOPYSIGN: {
4310     return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
4311   }
4312   case ISD::SELECT:
4313     return isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) &&
4314            isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1);
4315   case ISD::FP_EXTEND:
4316   case ISD::FP_ROUND: {
4317     if (SNaN)
4318       return true;
4319     return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
4320   }
4321   case ISD::SINT_TO_FP:
4322   case ISD::UINT_TO_FP:
4323     return true;
4324   case ISD::FMA:
4325   case ISD::FMAD: {
4326     if (SNaN)
4327       return true;
4328     return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) &&
4329            isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) &&
4330            isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1);
4331   }
4332   case ISD::FSQRT: // Need is known positive
4333   case ISD::FLOG:
4334   case ISD::FLOG2:
4335   case ISD::FLOG10:
4336   case ISD::FPOWI:
4337   case ISD::FPOW: {
4338     if (SNaN)
4339       return true;
4340     // TODO: Refine on operand
4341     return false;
4342   }
4343   case ISD::FMINNUM:
4344   case ISD::FMAXNUM: {
4345     // Only one needs to be known not-nan, since it will be returned if the
4346     // other ends up being one.
4347     return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) ||
4348            isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
4349   }
4350   case ISD::FMINNUM_IEEE:
4351   case ISD::FMAXNUM_IEEE: {
4352     if (SNaN)
4353       return true;
4354     // This can return a NaN if either operand is an sNaN, or if both operands
4355     // are NaN.
4356     return (isKnownNeverNaN(Op.getOperand(0), false, Depth + 1) &&
4357             isKnownNeverSNaN(Op.getOperand(1), Depth + 1)) ||
4358            (isKnownNeverNaN(Op.getOperand(1), false, Depth + 1) &&
4359             isKnownNeverSNaN(Op.getOperand(0), Depth + 1));
4360   }
4361   case ISD::FMINIMUM:
4362   case ISD::FMAXIMUM: {
4363     // TODO: Does this quiet or return the origina NaN as-is?
4364     return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) &&
4365            isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
4366   }
4367   case ISD::EXTRACT_VECTOR_ELT: {
4368     return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
4369   }
4370   default:
4371     if (Opcode >= ISD::BUILTIN_OP_END ||
4372         Opcode == ISD::INTRINSIC_WO_CHAIN ||
4373         Opcode == ISD::INTRINSIC_W_CHAIN ||
4374         Opcode == ISD::INTRINSIC_VOID) {
4375       return TLI->isKnownNeverNaNForTargetNode(Op, *this, SNaN, Depth);
4376     }
4377 
4378     return false;
4379   }
4380 }
4381 
4382 bool SelectionDAG::isKnownNeverZeroFloat(SDValue Op) const {
4383   assert(Op.getValueType().isFloatingPoint() &&
4384          "Floating point type expected");
4385 
4386   // If the value is a constant, we can obviously see if it is a zero or not.
4387   // TODO: Add BuildVector support.
4388   if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
4389     return !C->isZero();
4390   return false;
4391 }
4392 
4393 bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
4394   assert(!Op.getValueType().isFloatingPoint() &&
4395          "Floating point types unsupported - use isKnownNeverZeroFloat");
4396 
4397   // If the value is a constant, we can obviously see if it is a zero or not.
4398   if (ISD::matchUnaryPredicate(
4399           Op, [](ConstantSDNode *C) { return !C->isNullValue(); }))
4400     return true;
4401 
4402   // TODO: Recognize more cases here.
4403   switch (Op.getOpcode()) {
4404   default: break;
4405   case ISD::OR:
4406     if (isKnownNeverZero(Op.getOperand(1)) ||
4407         isKnownNeverZero(Op.getOperand(0)))
4408       return true;
4409     break;
4410   }
4411 
4412   return false;
4413 }
4414 
4415 bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
4416   // Check the obvious case.
4417   if (A == B) return true;
4418 
4419   // For for negative and positive zero.
4420   if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
4421     if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
4422       if (CA->isZero() && CB->isZero()) return true;
4423 
4424   // Otherwise they may not be equal.
4425   return false;
4426 }
4427 
4428 // FIXME: unify with llvm::haveNoCommonBitsSet.
4429 // FIXME: could also handle masked merge pattern (X & ~M) op (Y & M)
4430 bool SelectionDAG::haveNoCommonBitsSet(SDValue A, SDValue B) const {
4431   assert(A.getValueType() == B.getValueType() &&
4432          "Values must have the same type");
4433   return KnownBits::haveNoCommonBitsSet(computeKnownBits(A),
4434                                         computeKnownBits(B));
4435 }
4436 
4437 static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
4438                                SelectionDAG &DAG) {
4439   if (cast<ConstantSDNode>(Step)->isNullValue())
4440     return DAG.getConstant(0, DL, VT);
4441 
4442   return SDValue();
4443 }
4444 
4445 static SDValue FoldBUILD_VECTOR(const SDLoc &DL, EVT VT,
4446                                 ArrayRef<SDValue> Ops,
4447                                 SelectionDAG &DAG) {
4448   int NumOps = Ops.size();
4449   assert(NumOps != 0 && "Can't build an empty vector!");
4450   assert(!VT.isScalableVector() &&
4451          "BUILD_VECTOR cannot be used with scalable types");
4452   assert(VT.getVectorNumElements() == (unsigned)NumOps &&
4453          "Incorrect element count in BUILD_VECTOR!");
4454 
4455   // BUILD_VECTOR of UNDEFs is UNDEF.
4456   if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
4457     return DAG.getUNDEF(VT);
4458 
4459   // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
4460   SDValue IdentitySrc;
4461   bool IsIdentity = true;
4462   for (int i = 0; i != NumOps; ++i) {
4463     if (Ops[i].getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
4464         Ops[i].getOperand(0).getValueType() != VT ||
4465         (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
4466         !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
4467         cast<ConstantSDNode>(Ops[i].getOperand(1))->getAPIntValue() != i) {
4468       IsIdentity = false;
4469       break;
4470     }
4471     IdentitySrc = Ops[i].getOperand(0);
4472   }
4473   if (IsIdentity)
4474     return IdentitySrc;
4475 
4476   return SDValue();
4477 }
4478 
4479 /// Try to simplify vector concatenation to an input value, undef, or build
4480 /// vector.
4481 static SDValue foldCONCAT_VECTORS(const SDLoc &DL, EVT VT,
4482                                   ArrayRef<SDValue> Ops,
4483                                   SelectionDAG &DAG) {
4484   assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
4485   assert(llvm::all_of(Ops,
4486                       [Ops](SDValue Op) {
4487                         return Ops[0].getValueType() == Op.getValueType();
4488                       }) &&
4489          "Concatenation of vectors with inconsistent value types!");
4490   assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
4491              VT.getVectorElementCount() &&
4492          "Incorrect element count in vector concatenation!");
4493 
4494   if (Ops.size() == 1)
4495     return Ops[0];
4496 
4497   // Concat of UNDEFs is UNDEF.
4498   if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
4499     return DAG.getUNDEF(VT);
4500 
4501   // Scan the operands and look for extract operations from a single source
4502   // that correspond to insertion at the same location via this concatenation:
4503   // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
4504   SDValue IdentitySrc;
4505   bool IsIdentity = true;
4506   for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
4507     SDValue Op = Ops[i];
4508     unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
4509     if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
4510         Op.getOperand(0).getValueType() != VT ||
4511         (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
4512         Op.getConstantOperandVal(1) != IdentityIndex) {
4513       IsIdentity = false;
4514       break;
4515     }
4516     assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
4517            "Unexpected identity source vector for concat of extracts");
4518     IdentitySrc = Op.getOperand(0);
4519   }
4520   if (IsIdentity) {
4521     assert(IdentitySrc && "Failed to set source vector of extracts");
4522     return IdentitySrc;
4523   }
4524 
4525   // The code below this point is only designed to work for fixed width
4526   // vectors, so we bail out for now.
4527   if (VT.isScalableVector())
4528     return SDValue();
4529 
4530   // A CONCAT_VECTOR with all UNDEF/BUILD_VECTOR operands can be
4531   // simplified to one big BUILD_VECTOR.
4532   // FIXME: Add support for SCALAR_TO_VECTOR as well.
4533   EVT SVT = VT.getScalarType();
4534   SmallVector<SDValue, 16> Elts;
4535   for (SDValue Op : Ops) {
4536     EVT OpVT = Op.getValueType();
4537     if (Op.isUndef())
4538       Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
4539     else if (Op.getOpcode() == ISD::BUILD_VECTOR)
4540       Elts.append(Op->op_begin(), Op->op_end());
4541     else
4542       return SDValue();
4543   }
4544 
4545   // BUILD_VECTOR requires all inputs to be of the same type, find the
4546   // maximum type and extend them all.
4547   for (SDValue Op : Elts)
4548     SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
4549 
4550   if (SVT.bitsGT(VT.getScalarType())) {
4551     for (SDValue &Op : Elts) {
4552       if (Op.isUndef())
4553         Op = DAG.getUNDEF(SVT);
4554       else
4555         Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
4556                  ? DAG.getZExtOrTrunc(Op, DL, SVT)
4557                  : DAG.getSExtOrTrunc(Op, DL, SVT);
4558     }
4559   }
4560 
4561   SDValue V = DAG.getBuildVector(VT, DL, Elts);
4562   NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
4563   return V;
4564 }
4565 
4566 /// Gets or creates the specified node.
4567 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
4568   FoldingSetNodeID ID;
4569   AddNodeIDNode(ID, Opcode, getVTList(VT), None);
4570   void *IP = nullptr;
4571   if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
4572     return SDValue(E, 0);
4573 
4574   auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4575                               getVTList(VT));
4576   CSEMap.InsertNode(N, IP);
4577 
4578   InsertNode(N);
4579   SDValue V = SDValue(N, 0);
4580   NewSDValueDbgMsg(V, "Creating new node: ", this);
4581   return V;
4582 }
4583 
4584 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
4585                               SDValue Operand) {
4586   SDNodeFlags Flags;
4587   if (Inserter)
4588     Flags = Inserter->getFlags();
4589   return getNode(Opcode, DL, VT, Operand, Flags);
4590 }
4591 
4592 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
4593                               SDValue Operand, const SDNodeFlags Flags) {
4594   assert(Operand.getOpcode() != ISD::DELETED_NODE &&
4595          "Operand is DELETED_NODE!");
4596   // Constant fold unary operations with an integer constant operand. Even
4597   // opaque constant will be folded, because the folding of unary operations
4598   // doesn't create new constants with different values. Nevertheless, the
4599   // opaque flag is preserved during folding to prevent future folding with
4600   // other constants.
4601   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand)) {
4602     const APInt &Val = C->getAPIntValue();
4603     switch (Opcode) {
4604     default: break;
4605     case ISD::SIGN_EXTEND:
4606       return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
4607                          C->isTargetOpcode(), C->isOpaque());
4608     case ISD::TRUNCATE:
4609       if (C->isOpaque())
4610         break;
4611       LLVM_FALLTHROUGH;
4612     case ISD::ANY_EXTEND:
4613     case ISD::ZERO_EXTEND:
4614       return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
4615                          C->isTargetOpcode(), C->isOpaque());
4616     case ISD::UINT_TO_FP:
4617     case ISD::SINT_TO_FP: {
4618       APFloat apf(EVTToAPFloatSemantics(VT),
4619                   APInt::getNullValue(VT.getSizeInBits()));
4620       (void)apf.convertFromAPInt(Val,
4621                                  Opcode==ISD::SINT_TO_FP,
4622                                  APFloat::rmNearestTiesToEven);
4623       return getConstantFP(apf, DL, VT);
4624     }
4625     case ISD::BITCAST:
4626       if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
4627         return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
4628       if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
4629         return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
4630       if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
4631         return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
4632       if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
4633         return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
4634       break;
4635     case ISD::ABS:
4636       return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
4637                          C->isOpaque());
4638     case ISD::BITREVERSE:
4639       return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
4640                          C->isOpaque());
4641     case ISD::BSWAP:
4642       return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
4643                          C->isOpaque());
4644     case ISD::CTPOP:
4645       return getConstant(Val.countPopulation(), DL, VT, C->isTargetOpcode(),
4646                          C->isOpaque());
4647     case ISD::CTLZ:
4648     case ISD::CTLZ_ZERO_UNDEF:
4649       return getConstant(Val.countLeadingZeros(), DL, VT, C->isTargetOpcode(),
4650                          C->isOpaque());
4651     case ISD::CTTZ:
4652     case ISD::CTTZ_ZERO_UNDEF:
4653       return getConstant(Val.countTrailingZeros(), DL, VT, C->isTargetOpcode(),
4654                          C->isOpaque());
4655     case ISD::FP16_TO_FP: {
4656       bool Ignored;
4657       APFloat FPV(APFloat::IEEEhalf(),
4658                   (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
4659 
4660       // This can return overflow, underflow, or inexact; we don't care.
4661       // FIXME need to be more flexible about rounding mode.
4662       (void)FPV.convert(EVTToAPFloatSemantics(VT),
4663                         APFloat::rmNearestTiesToEven, &Ignored);
4664       return getConstantFP(FPV, DL, VT);
4665     }
4666     case ISD::STEP_VECTOR: {
4667       if (SDValue V = FoldSTEP_VECTOR(DL, VT, Operand, *this))
4668         return V;
4669       break;
4670     }
4671     }
4672   }
4673 
4674   // Constant fold unary operations with a floating point constant operand.
4675   if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand)) {
4676     APFloat V = C->getValueAPF();    // make copy
4677     switch (Opcode) {
4678     case ISD::FNEG:
4679       V.changeSign();
4680       return getConstantFP(V, DL, VT);
4681     case ISD::FABS:
4682       V.clearSign();
4683       return getConstantFP(V, DL, VT);
4684     case ISD::FCEIL: {
4685       APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
4686       if (fs == APFloat::opOK || fs == APFloat::opInexact)
4687         return getConstantFP(V, DL, VT);
4688       break;
4689     }
4690     case ISD::FTRUNC: {
4691       APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
4692       if (fs == APFloat::opOK || fs == APFloat::opInexact)
4693         return getConstantFP(V, DL, VT);
4694       break;
4695     }
4696     case ISD::FFLOOR: {
4697       APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
4698       if (fs == APFloat::opOK || fs == APFloat::opInexact)
4699         return getConstantFP(V, DL, VT);
4700       break;
4701     }
4702     case ISD::FP_EXTEND: {
4703       bool ignored;
4704       // This can return overflow, underflow, or inexact; we don't care.
4705       // FIXME need to be more flexible about rounding mode.
4706       (void)V.convert(EVTToAPFloatSemantics(VT),
4707                       APFloat::rmNearestTiesToEven, &ignored);
4708       return getConstantFP(V, DL, VT);
4709     }
4710     case ISD::FP_TO_SINT:
4711     case ISD::FP_TO_UINT: {
4712       bool ignored;
4713       APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
4714       // FIXME need to be more flexible about rounding mode.
4715       APFloat::opStatus s =
4716           V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
4717       if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
4718         break;
4719       return getConstant(IntVal, DL, VT);
4720     }
4721     case ISD::BITCAST:
4722       if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
4723         return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
4724       if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
4725         return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
4726       if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
4727         return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
4728       if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
4729         return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
4730       break;
4731     case ISD::FP_TO_FP16: {
4732       bool Ignored;
4733       // This can return overflow, underflow, or inexact; we don't care.
4734       // FIXME need to be more flexible about rounding mode.
4735       (void)V.convert(APFloat::IEEEhalf(),
4736                       APFloat::rmNearestTiesToEven, &Ignored);
4737       return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
4738     }
4739     }
4740   }
4741 
4742   // Constant fold unary operations with a vector integer or float operand.
4743   switch (Opcode) {
4744   default:
4745     // FIXME: Entirely reasonable to perform folding of other unary
4746     // operations here as the need arises.
4747     break;
4748   case ISD::FNEG:
4749   case ISD::FABS:
4750   case ISD::FCEIL:
4751   case ISD::FTRUNC:
4752   case ISD::FFLOOR:
4753   case ISD::FP_EXTEND:
4754   case ISD::FP_TO_SINT:
4755   case ISD::FP_TO_UINT:
4756   case ISD::TRUNCATE:
4757   case ISD::ANY_EXTEND:
4758   case ISD::ZERO_EXTEND:
4759   case ISD::SIGN_EXTEND:
4760   case ISD::UINT_TO_FP:
4761   case ISD::SINT_TO_FP:
4762   case ISD::ABS:
4763   case ISD::BITREVERSE:
4764   case ISD::BSWAP:
4765   case ISD::CTLZ:
4766   case ISD::CTLZ_ZERO_UNDEF:
4767   case ISD::CTTZ:
4768   case ISD::CTTZ_ZERO_UNDEF:
4769   case ISD::CTPOP: {
4770     SDValue Ops = {Operand};
4771     if (SDValue Fold = FoldConstantVectorArithmetic(Opcode, DL, VT, Ops))
4772       return Fold;
4773   }
4774   }
4775 
4776   unsigned OpOpcode = Operand.getNode()->getOpcode();
4777   switch (Opcode) {
4778   case ISD::STEP_VECTOR:
4779     assert(VT.isScalableVector() &&
4780            "STEP_VECTOR can only be used with scalable types");
4781     assert(VT.getScalarSizeInBits() >= 8 &&
4782            "STEP_VECTOR can only be used with vectors of integers that are at "
4783            "least 8 bits wide");
4784     assert(isa<ConstantSDNode>(Operand) &&
4785            cast<ConstantSDNode>(Operand)->getAPIntValue().isSignedIntN(
4786                VT.getScalarSizeInBits()) &&
4787            "Expected STEP_VECTOR integer constant to fit in "
4788            "the vector element type");
4789     break;
4790   case ISD::FREEZE:
4791     assert(VT == Operand.getValueType() && "Unexpected VT!");
4792     break;
4793   case ISD::TokenFactor:
4794   case ISD::MERGE_VALUES:
4795   case ISD::CONCAT_VECTORS:
4796     return Operand;         // Factor, merge or concat of one node?  No need.
4797   case ISD::BUILD_VECTOR: {
4798     // Attempt to simplify BUILD_VECTOR.
4799     SDValue Ops[] = {Operand};
4800     if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
4801       return V;
4802     break;
4803   }
4804   case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
4805   case ISD::FP_EXTEND:
4806     assert(VT.isFloatingPoint() &&
4807            Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
4808     if (Operand.getValueType() == VT) return Operand;  // noop conversion.
4809     assert((!VT.isVector() ||
4810             VT.getVectorElementCount() ==
4811             Operand.getValueType().getVectorElementCount()) &&
4812            "Vector element count mismatch!");
4813     assert(Operand.getValueType().bitsLT(VT) &&
4814            "Invalid fpext node, dst < src!");
4815     if (Operand.isUndef())
4816       return getUNDEF(VT);
4817     break;
4818   case ISD::FP_TO_SINT:
4819   case ISD::FP_TO_UINT:
4820     if (Operand.isUndef())
4821       return getUNDEF(VT);
4822     break;
4823   case ISD::SINT_TO_FP:
4824   case ISD::UINT_TO_FP:
4825     // [us]itofp(undef) = 0, because the result value is bounded.
4826     if (Operand.isUndef())
4827       return getConstantFP(0.0, DL, VT);
4828     break;
4829   case ISD::SIGN_EXTEND:
4830     assert(VT.isInteger() && Operand.getValueType().isInteger() &&
4831            "Invalid SIGN_EXTEND!");
4832     assert(VT.isVector() == Operand.getValueType().isVector() &&
4833            "SIGN_EXTEND result type type should be vector iff the operand "
4834            "type is vector!");
4835     if (Operand.getValueType() == VT) return Operand;   // noop extension
4836     assert((!VT.isVector() ||
4837             VT.getVectorElementCount() ==
4838                 Operand.getValueType().getVectorElementCount()) &&
4839            "Vector element count mismatch!");
4840     assert(Operand.getValueType().bitsLT(VT) &&
4841            "Invalid sext node, dst < src!");
4842     if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
4843       return getNode(OpOpcode, DL, VT, Operand.getOperand(0));
4844     if (OpOpcode == ISD::UNDEF)
4845       // sext(undef) = 0, because the top bits will all be the same.
4846       return getConstant(0, DL, VT);
4847     break;
4848   case ISD::ZERO_EXTEND:
4849     assert(VT.isInteger() && Operand.getValueType().isInteger() &&
4850            "Invalid ZERO_EXTEND!");
4851     assert(VT.isVector() == Operand.getValueType().isVector() &&
4852            "ZERO_EXTEND result type type should be vector iff the operand "
4853            "type is vector!");
4854     if (Operand.getValueType() == VT) return Operand;   // noop extension
4855     assert((!VT.isVector() ||
4856             VT.getVectorElementCount() ==
4857                 Operand.getValueType().getVectorElementCount()) &&
4858            "Vector element count mismatch!");
4859     assert(Operand.getValueType().bitsLT(VT) &&
4860            "Invalid zext node, dst < src!");
4861     if (OpOpcode == ISD::ZERO_EXTEND)   // (zext (zext x)) -> (zext x)
4862       return getNode(ISD::ZERO_EXTEND, DL, VT, Operand.getOperand(0));
4863     if (OpOpcode == ISD::UNDEF)
4864       // zext(undef) = 0, because the top bits will be zero.
4865       return getConstant(0, DL, VT);
4866     break;
4867   case ISD::ANY_EXTEND:
4868     assert(VT.isInteger() && Operand.getValueType().isInteger() &&
4869            "Invalid ANY_EXTEND!");
4870     assert(VT.isVector() == Operand.getValueType().isVector() &&
4871            "ANY_EXTEND result type type should be vector iff the operand "
4872            "type is vector!");
4873     if (Operand.getValueType() == VT) return Operand;   // noop extension
4874     assert((!VT.isVector() ||
4875             VT.getVectorElementCount() ==
4876                 Operand.getValueType().getVectorElementCount()) &&
4877            "Vector element count mismatch!");
4878     assert(Operand.getValueType().bitsLT(VT) &&
4879            "Invalid anyext node, dst < src!");
4880 
4881     if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
4882         OpOpcode == ISD::ANY_EXTEND)
4883       // (ext (zext x)) -> (zext x)  and  (ext (sext x)) -> (sext x)
4884       return getNode(OpOpcode, DL, VT, Operand.getOperand(0));
4885     if (OpOpcode == ISD::UNDEF)
4886       return getUNDEF(VT);
4887 
4888     // (ext (trunc x)) -> x
4889     if (OpOpcode == ISD::TRUNCATE) {
4890       SDValue OpOp = Operand.getOperand(0);
4891       if (OpOp.getValueType() == VT) {
4892         transferDbgValues(Operand, OpOp);
4893         return OpOp;
4894       }
4895     }
4896     break;
4897   case ISD::TRUNCATE:
4898     assert(VT.isInteger() && Operand.getValueType().isInteger() &&
4899            "Invalid TRUNCATE!");
4900     assert(VT.isVector() == Operand.getValueType().isVector() &&
4901            "TRUNCATE result type type should be vector iff the operand "
4902            "type is vector!");
4903     if (Operand.getValueType() == VT) return Operand;   // noop truncate
4904     assert((!VT.isVector() ||
4905             VT.getVectorElementCount() ==
4906                 Operand.getValueType().getVectorElementCount()) &&
4907            "Vector element count mismatch!");
4908     assert(Operand.getValueType().bitsGT(VT) &&
4909            "Invalid truncate node, src < dst!");
4910     if (OpOpcode == ISD::TRUNCATE)
4911       return getNode(ISD::TRUNCATE, DL, VT, Operand.getOperand(0));
4912     if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
4913         OpOpcode == ISD::ANY_EXTEND) {
4914       // If the source is smaller than the dest, we still need an extend.
4915       if (Operand.getOperand(0).getValueType().getScalarType()
4916             .bitsLT(VT.getScalarType()))
4917         return getNode(OpOpcode, DL, VT, Operand.getOperand(0));
4918       if (Operand.getOperand(0).getValueType().bitsGT(VT))
4919         return getNode(ISD::TRUNCATE, DL, VT, Operand.getOperand(0));
4920       return Operand.getOperand(0);
4921     }
4922     if (OpOpcode == ISD::UNDEF)
4923       return getUNDEF(VT);
4924     break;
4925   case ISD::ANY_EXTEND_VECTOR_INREG:
4926   case ISD::ZERO_EXTEND_VECTOR_INREG:
4927   case ISD::SIGN_EXTEND_VECTOR_INREG:
4928     assert(VT.isVector() && "This DAG node is restricted to vector types.");
4929     assert(Operand.getValueType().bitsLE(VT) &&
4930            "The input must be the same size or smaller than the result.");
4931     assert(VT.getVectorMinNumElements() <
4932                Operand.getValueType().getVectorMinNumElements() &&
4933            "The destination vector type must have fewer lanes than the input.");
4934     break;
4935   case ISD::ABS:
4936     assert(VT.isInteger() && VT == Operand.getValueType() &&
4937            "Invalid ABS!");
4938     if (OpOpcode == ISD::UNDEF)
4939       return getUNDEF(VT);
4940     break;
4941   case ISD::BSWAP:
4942     assert(VT.isInteger() && VT == Operand.getValueType() &&
4943            "Invalid BSWAP!");
4944     assert((VT.getScalarSizeInBits() % 16 == 0) &&
4945            "BSWAP types must be a multiple of 16 bits!");
4946     if (OpOpcode == ISD::UNDEF)
4947       return getUNDEF(VT);
4948     break;
4949   case ISD::BITREVERSE:
4950     assert(VT.isInteger() && VT == Operand.getValueType() &&
4951            "Invalid BITREVERSE!");
4952     if (OpOpcode == ISD::UNDEF)
4953       return getUNDEF(VT);
4954     break;
4955   case ISD::BITCAST:
4956     // Basic sanity checking.
4957     assert(VT.getSizeInBits() == Operand.getValueSizeInBits() &&
4958            "Cannot BITCAST between types of different sizes!");
4959     if (VT == Operand.getValueType()) return Operand;  // noop conversion.
4960     if (OpOpcode == ISD::BITCAST)  // bitconv(bitconv(x)) -> bitconv(x)
4961       return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
4962     if (OpOpcode == ISD::UNDEF)
4963       return getUNDEF(VT);
4964     break;
4965   case ISD::SCALAR_TO_VECTOR:
4966     assert(VT.isVector() && !Operand.getValueType().isVector() &&
4967            (VT.getVectorElementType() == Operand.getValueType() ||
4968             (VT.getVectorElementType().isInteger() &&
4969              Operand.getValueType().isInteger() &&
4970              VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
4971            "Illegal SCALAR_TO_VECTOR node!");
4972     if (OpOpcode == ISD::UNDEF)
4973       return getUNDEF(VT);
4974     // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
4975     if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
4976         isa<ConstantSDNode>(Operand.getOperand(1)) &&
4977         Operand.getConstantOperandVal(1) == 0 &&
4978         Operand.getOperand(0).getValueType() == VT)
4979       return Operand.getOperand(0);
4980     break;
4981   case ISD::FNEG:
4982     // Negation of an unknown bag of bits is still completely undefined.
4983     if (OpOpcode == ISD::UNDEF)
4984       return getUNDEF(VT);
4985 
4986     if (OpOpcode == ISD::FNEG)  // --X -> X
4987       return Operand.getOperand(0);
4988     break;
4989   case ISD::FABS:
4990     if (OpOpcode == ISD::FNEG)  // abs(-X) -> abs(X)
4991       return getNode(ISD::FABS, DL, VT, Operand.getOperand(0));
4992     break;
4993   case ISD::VSCALE:
4994     assert(VT == Operand.getValueType() && "Unexpected VT!");
4995     break;
4996   case ISD::CTPOP:
4997     if (Operand.getValueType().getScalarType() == MVT::i1)
4998       return Operand;
4999     break;
5000   case ISD::CTLZ:
5001   case ISD::CTTZ:
5002     if (Operand.getValueType().getScalarType() == MVT::i1)
5003       return getNOT(DL, Operand, Operand.getValueType());
5004     break;
5005   case ISD::VECREDUCE_SMIN:
5006   case ISD::VECREDUCE_UMAX:
5007     if (Operand.getValueType().getScalarType() == MVT::i1)
5008       return getNode(ISD::VECREDUCE_OR, DL, VT, Operand);
5009     break;
5010   case ISD::VECREDUCE_SMAX:
5011   case ISD::VECREDUCE_UMIN:
5012     if (Operand.getValueType().getScalarType() == MVT::i1)
5013       return getNode(ISD::VECREDUCE_AND, DL, VT, Operand);
5014     break;
5015   }
5016 
5017   SDNode *N;
5018   SDVTList VTs = getVTList(VT);
5019   SDValue Ops[] = {Operand};
5020   if (VT != MVT::Glue) { // Don't CSE flag producing nodes
5021     FoldingSetNodeID ID;
5022     AddNodeIDNode(ID, Opcode, VTs, Ops);
5023     void *IP = nullptr;
5024     if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
5025       E->intersectFlagsWith(Flags);
5026       return SDValue(E, 0);
5027     }
5028 
5029     N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
5030     N->setFlags(Flags);
5031     createOperands(N, Ops);
5032     CSEMap.InsertNode(N, IP);
5033   } else {
5034     N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
5035     createOperands(N, Ops);
5036   }
5037 
5038   InsertNode(N);
5039   SDValue V = SDValue(N, 0);
5040   NewSDValueDbgMsg(V, "Creating new node: ", this);
5041   return V;
5042 }
5043 
5044 static llvm::Optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
5045                                        const APInt &C2) {
5046   switch (Opcode) {
5047   case ISD::ADD:  return C1 + C2;
5048   case ISD::SUB:  return C1 - C2;
5049   case ISD::MUL:  return C1 * C2;
5050   case ISD::AND:  return C1 & C2;
5051   case ISD::OR:   return C1 | C2;
5052   case ISD::XOR:  return C1 ^ C2;
5053   case ISD::SHL:  return C1 << C2;
5054   case ISD::SRL:  return C1.lshr(C2);
5055   case ISD::SRA:  return C1.ashr(C2);
5056   case ISD::ROTL: return C1.rotl(C2);
5057   case ISD::ROTR: return C1.rotr(C2);
5058   case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
5059   case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
5060   case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
5061   case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
5062   case ISD::SADDSAT: return C1.sadd_sat(C2);
5063   case ISD::UADDSAT: return C1.uadd_sat(C2);
5064   case ISD::SSUBSAT: return C1.ssub_sat(C2);
5065   case ISD::USUBSAT: return C1.usub_sat(C2);
5066   case ISD::UDIV:
5067     if (!C2.getBoolValue())
5068       break;
5069     return C1.udiv(C2);
5070   case ISD::UREM:
5071     if (!C2.getBoolValue())
5072       break;
5073     return C1.urem(C2);
5074   case ISD::SDIV:
5075     if (!C2.getBoolValue())
5076       break;
5077     return C1.sdiv(C2);
5078   case ISD::SREM:
5079     if (!C2.getBoolValue())
5080       break;
5081     return C1.srem(C2);
5082   case ISD::MULHS: {
5083     unsigned FullWidth = C1.getBitWidth() * 2;
5084     APInt C1Ext = C1.sext(FullWidth);
5085     APInt C2Ext = C2.sext(FullWidth);
5086     return (C1Ext * C2Ext).extractBits(C1.getBitWidth(), C1.getBitWidth());
5087   }
5088   case ISD::MULHU: {
5089     unsigned FullWidth = C1.getBitWidth() * 2;
5090     APInt C1Ext = C1.zext(FullWidth);
5091     APInt C2Ext = C2.zext(FullWidth);
5092     return (C1Ext * C2Ext).extractBits(C1.getBitWidth(), C1.getBitWidth());
5093   }
5094   }
5095   return llvm::None;
5096 }
5097 
5098 SDValue SelectionDAG::FoldSymbolOffset(unsigned Opcode, EVT VT,
5099                                        const GlobalAddressSDNode *GA,
5100                                        const SDNode *N2) {
5101   if (GA->getOpcode() != ISD::GlobalAddress)
5102     return SDValue();
5103   if (!TLI->isOffsetFoldingLegal(GA))
5104     return SDValue();
5105   auto *C2 = dyn_cast<ConstantSDNode>(N2);
5106   if (!C2)
5107     return SDValue();
5108   int64_t Offset = C2->getSExtValue();
5109   switch (Opcode) {
5110   case ISD::ADD: break;
5111   case ISD::SUB: Offset = -uint64_t(Offset); break;
5112   default: return SDValue();
5113   }
5114   return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
5115                           GA->getOffset() + uint64_t(Offset));
5116 }
5117 
5118 bool SelectionDAG::isUndef(unsigned Opcode, ArrayRef<SDValue> Ops) {
5119   switch (Opcode) {
5120   case ISD::SDIV:
5121   case ISD::UDIV:
5122   case ISD::SREM:
5123   case ISD::UREM: {
5124     // If a divisor is zero/undef or any element of a divisor vector is
5125     // zero/undef, the whole op is undef.
5126     assert(Ops.size() == 2 && "Div/rem should have 2 operands");
5127     SDValue Divisor = Ops[1];
5128     if (Divisor.isUndef() || isNullConstant(Divisor))
5129       return true;
5130 
5131     return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
5132            llvm::any_of(Divisor->op_values(),
5133                         [](SDValue V) { return V.isUndef() ||
5134                                         isNullConstant(V); });
5135     // TODO: Handle signed overflow.
5136   }
5137   // TODO: Handle oversized shifts.
5138   default:
5139     return false;
5140   }
5141 }
5142 
5143 SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL,
5144                                              EVT VT, ArrayRef<SDValue> Ops) {
5145   // If the opcode is a target-specific ISD node, there's nothing we can
5146   // do here and the operand rules may not line up with the below, so
5147   // bail early.
5148   // We can't create a scalar CONCAT_VECTORS so skip it. It will break
5149   // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
5150   // foldCONCAT_VECTORS in getNode before this is called.
5151   if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
5152     return SDValue();
5153 
5154   // For now, the array Ops should only contain two values.
5155   // This enforcement will be removed once this function is merged with
5156   // FoldConstantVectorArithmetic
5157   if (Ops.size() != 2)
5158     return SDValue();
5159 
5160   if (isUndef(Opcode, Ops))
5161     return getUNDEF(VT);
5162 
5163   SDNode *N1 = Ops[0].getNode();
5164   SDNode *N2 = Ops[1].getNode();
5165 
5166   // Handle the case of two scalars.
5167   if (auto *C1 = dyn_cast<ConstantSDNode>(N1)) {
5168     if (auto *C2 = dyn_cast<ConstantSDNode>(N2)) {
5169       if (C1->isOpaque() || C2->isOpaque())
5170         return SDValue();
5171 
5172       Optional<APInt> FoldAttempt =
5173           FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
5174       if (!FoldAttempt)
5175         return SDValue();
5176 
5177       SDValue Folded = getConstant(FoldAttempt.getValue(), DL, VT);
5178       assert((!Folded || !VT.isVector()) &&
5179              "Can't fold vectors ops with scalar operands");
5180       return Folded;
5181     }
5182   }
5183 
5184   // fold (add Sym, c) -> Sym+c
5185   if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N1))
5186     return FoldSymbolOffset(Opcode, VT, GA, N2);
5187   if (TLI->isCommutativeBinOp(Opcode))
5188     if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N2))
5189       return FoldSymbolOffset(Opcode, VT, GA, N1);
5190 
5191   // For fixed width vectors, extract each constant element and fold them
5192   // individually. Either input may be an undef value.
5193   bool IsBVOrSV1 = N1->getOpcode() == ISD::BUILD_VECTOR ||
5194                    N1->getOpcode() == ISD::SPLAT_VECTOR;
5195   if (!IsBVOrSV1 && !N1->isUndef())
5196     return SDValue();
5197   bool IsBVOrSV2 = N2->getOpcode() == ISD::BUILD_VECTOR ||
5198                    N2->getOpcode() == ISD::SPLAT_VECTOR;
5199   if (!IsBVOrSV2 && !N2->isUndef())
5200     return SDValue();
5201   // If both operands are undef, that's handled the same way as scalars.
5202   if (!IsBVOrSV1 && !IsBVOrSV2)
5203     return SDValue();
5204 
5205   EVT SVT = VT.getScalarType();
5206   EVT LegalSVT = SVT;
5207   if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
5208     LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
5209     if (LegalSVT.bitsLT(SVT))
5210       return SDValue();
5211   }
5212 
5213   SmallVector<SDValue, 4> Outputs;
5214   unsigned NumOps = 0;
5215   if (IsBVOrSV1)
5216     NumOps = std::max(NumOps, N1->getNumOperands());
5217   if (IsBVOrSV2)
5218     NumOps = std::max(NumOps, N2->getNumOperands());
5219   assert(NumOps != 0 && "Expected non-zero operands");
5220   // Scalable vectors should only be SPLAT_VECTOR or UNDEF here. We only need
5221   // one iteration for that.
5222   assert((!VT.isScalableVector() || NumOps == 1) &&
5223          "Scalable vector should only have one scalar");
5224 
5225   for (unsigned I = 0; I != NumOps; ++I) {
5226     // We can have a fixed length SPLAT_VECTOR and a BUILD_VECTOR so we need
5227     // to use operand 0 of the SPLAT_VECTOR for each fixed element.
5228     SDValue V1;
5229     if (N1->getOpcode() == ISD::BUILD_VECTOR)
5230       V1 = N1->getOperand(I);
5231     else if (N1->getOpcode() == ISD::SPLAT_VECTOR)
5232       V1 = N1->getOperand(0);
5233     else
5234       V1 = getUNDEF(SVT);
5235 
5236     SDValue V2;
5237     if (N2->getOpcode() == ISD::BUILD_VECTOR)
5238       V2 = N2->getOperand(I);
5239     else if (N2->getOpcode() == ISD::SPLAT_VECTOR)
5240       V2 = N2->getOperand(0);
5241     else
5242       V2 = getUNDEF(SVT);
5243 
5244     if (SVT.isInteger()) {
5245       if (V1.getValueType().bitsGT(SVT))
5246         V1 = getNode(ISD::TRUNCATE, DL, SVT, V1);
5247       if (V2.getValueType().bitsGT(SVT))
5248         V2 = getNode(ISD::TRUNCATE, DL, SVT, V2);
5249     }
5250 
5251     if (V1.getValueType() != SVT || V2.getValueType() != SVT)
5252       return SDValue();
5253 
5254     // Fold one vector element.
5255     SDValue ScalarResult = getNode(Opcode, DL, SVT, V1, V2);
5256     if (LegalSVT != SVT)
5257       ScalarResult = getNode(ISD::SIGN_EXTEND, DL, LegalSVT, ScalarResult);
5258 
5259     // Scalar folding only succeeded if the result is a constant or UNDEF.
5260     if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
5261         ScalarResult.getOpcode() != ISD::ConstantFP)
5262       return SDValue();
5263     Outputs.push_back(ScalarResult);
5264   }
5265 
5266   if (N1->getOpcode() == ISD::BUILD_VECTOR ||
5267       N2->getOpcode() == ISD::BUILD_VECTOR) {
5268     assert(VT.getVectorNumElements() == Outputs.size() &&
5269            "Vector size mismatch!");
5270 
5271     // Build a big vector out of the scalar elements we generated.
5272     return getBuildVector(VT, SDLoc(), Outputs);
5273   }
5274 
5275   assert((N1->getOpcode() == ISD::SPLAT_VECTOR ||
5276           N2->getOpcode() == ISD::SPLAT_VECTOR) &&
5277          "One operand should be a splat vector");
5278 
5279   assert(Outputs.size() == 1 && "Vector size mismatch!");
5280   return getSplatVector(VT, SDLoc(), Outputs[0]);
5281 }
5282 
5283 // TODO: Merge with FoldConstantArithmetic
5284 SDValue SelectionDAG::FoldConstantVectorArithmetic(unsigned Opcode,
5285                                                    const SDLoc &DL, EVT VT,
5286                                                    ArrayRef<SDValue> Ops,
5287                                                    const SDNodeFlags Flags) {
5288   // If the opcode is a target-specific ISD node, there's nothing we can
5289   // do here and the operand rules may not line up with the below, so
5290   // bail early.
5291   if (Opcode >= ISD::BUILTIN_OP_END)
5292     return SDValue();
5293 
5294   if (isUndef(Opcode, Ops))
5295     return getUNDEF(VT);
5296 
5297   // We can only fold vectors - maybe merge with FoldConstantArithmetic someday?
5298   if (!VT.isVector())
5299     return SDValue();
5300 
5301   ElementCount NumElts = VT.getVectorElementCount();
5302 
5303   auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
5304     return !Op.getValueType().isVector() ||
5305            Op.getValueType().getVectorElementCount() == NumElts;
5306   };
5307 
5308   auto IsConstantBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
5309     APInt SplatVal;
5310     BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Op);
5311     return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
5312            (BV && BV->isConstant()) ||
5313            (Op.getOpcode() == ISD::SPLAT_VECTOR &&
5314             ISD::isConstantSplatVector(Op.getNode(), SplatVal));
5315   };
5316 
5317   // All operands must be vector types with the same number of elements as
5318   // the result type and must be either UNDEF or a build vector of constant
5319   // or UNDEF scalars.
5320   if (!llvm::all_of(Ops, IsConstantBuildVectorSplatVectorOrUndef) ||
5321       !llvm::all_of(Ops, IsScalarOrSameVectorSize))
5322     return SDValue();
5323 
5324   // If we are comparing vectors, then the result needs to be a i1 boolean
5325   // that is then sign-extended back to the legal result type.
5326   EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
5327 
5328   // Find legal integer scalar type for constant promotion and
5329   // ensure that its scalar size is at least as large as source.
5330   EVT LegalSVT = VT.getScalarType();
5331   if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
5332     LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
5333     if (LegalSVT.bitsLT(VT.getScalarType()))
5334       return SDValue();
5335   }
5336 
5337   // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
5338   // only have one operand to check. For fixed-length vector types we may have
5339   // a combination of BUILD_VECTOR and SPLAT_VECTOR.
5340   unsigned NumOperands = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
5341 
5342   // Constant fold each scalar lane separately.
5343   SmallVector<SDValue, 4> ScalarResults;
5344   for (unsigned I = 0; I != NumOperands; I++) {
5345     SmallVector<SDValue, 4> ScalarOps;
5346     for (SDValue Op : Ops) {
5347       EVT InSVT = Op.getValueType().getScalarType();
5348       if (Op.getOpcode() != ISD::BUILD_VECTOR &&
5349           Op.getOpcode() != ISD::SPLAT_VECTOR) {
5350         // We've checked that this is UNDEF or a constant of some kind.
5351         if (Op.isUndef())
5352           ScalarOps.push_back(getUNDEF(InSVT));
5353         else
5354           ScalarOps.push_back(Op);
5355         continue;
5356       }
5357 
5358       SDValue ScalarOp =
5359           Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
5360       EVT ScalarVT = ScalarOp.getValueType();
5361 
5362       // Build vector (integer) scalar operands may need implicit
5363       // truncation - do this before constant folding.
5364       if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT))
5365         ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
5366 
5367       ScalarOps.push_back(ScalarOp);
5368     }
5369 
5370     // Constant fold the scalar operands.
5371     SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
5372 
5373     // Legalize the (integer) scalar constant if necessary.
5374     if (LegalSVT != SVT)
5375       ScalarResult = getNode(ISD::SIGN_EXTEND, DL, LegalSVT, ScalarResult);
5376 
5377     // Scalar folding only succeeded if the result is a constant or UNDEF.
5378     if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
5379         ScalarResult.getOpcode() != ISD::ConstantFP)
5380       return SDValue();
5381     ScalarResults.push_back(ScalarResult);
5382   }
5383 
5384   SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
5385                                    : getBuildVector(VT, DL, ScalarResults);
5386   NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
5387   return V;
5388 }
5389 
5390 SDValue SelectionDAG::foldConstantFPMath(unsigned Opcode, const SDLoc &DL,
5391                                          EVT VT, SDValue N1, SDValue N2) {
5392   // TODO: We don't do any constant folding for strict FP opcodes here, but we
5393   //       should. That will require dealing with a potentially non-default
5394   //       rounding mode, checking the "opStatus" return value from the APFloat
5395   //       math calculations, and possibly other variations.
5396   auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
5397   auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
5398   if (N1CFP && N2CFP) {
5399     APFloat C1 = N1CFP->getValueAPF(), C2 = N2CFP->getValueAPF();
5400     switch (Opcode) {
5401     case ISD::FADD:
5402       C1.add(C2, APFloat::rmNearestTiesToEven);
5403       return getConstantFP(C1, DL, VT);
5404     case ISD::FSUB:
5405       C1.subtract(C2, APFloat::rmNearestTiesToEven);
5406       return getConstantFP(C1, DL, VT);
5407     case ISD::FMUL:
5408       C1.multiply(C2, APFloat::rmNearestTiesToEven);
5409       return getConstantFP(C1, DL, VT);
5410     case ISD::FDIV:
5411       C1.divide(C2, APFloat::rmNearestTiesToEven);
5412       return getConstantFP(C1, DL, VT);
5413     case ISD::FREM:
5414       C1.mod(C2);
5415       return getConstantFP(C1, DL, VT);
5416     case ISD::FCOPYSIGN:
5417       C1.copySign(C2);
5418       return getConstantFP(C1, DL, VT);
5419     default: break;
5420     }
5421   }
5422   if (N1CFP && Opcode == ISD::FP_ROUND) {
5423     APFloat C1 = N1CFP->getValueAPF();    // make copy
5424     bool Unused;
5425     // This can return overflow, underflow, or inexact; we don't care.
5426     // FIXME need to be more flexible about rounding mode.
5427     (void) C1.convert(EVTToAPFloatSemantics(VT), APFloat::rmNearestTiesToEven,
5428                       &Unused);
5429     return getConstantFP(C1, DL, VT);
5430   }
5431 
5432   switch (Opcode) {
5433   case ISD::FSUB:
5434     // -0.0 - undef --> undef (consistent with "fneg undef")
5435     if (N1CFP && N1CFP->getValueAPF().isNegZero() && N2.isUndef())
5436       return getUNDEF(VT);
5437     LLVM_FALLTHROUGH;
5438 
5439   case ISD::FADD:
5440   case ISD::FMUL:
5441   case ISD::FDIV:
5442   case ISD::FREM:
5443     // If both operands are undef, the result is undef. If 1 operand is undef,
5444     // the result is NaN. This should match the behavior of the IR optimizer.
5445     if (N1.isUndef() && N2.isUndef())
5446       return getUNDEF(VT);
5447     if (N1.isUndef() || N2.isUndef())
5448       return getConstantFP(APFloat::getNaN(EVTToAPFloatSemantics(VT)), DL, VT);
5449   }
5450   return SDValue();
5451 }
5452 
5453 SDValue SelectionDAG::getAssertAlign(const SDLoc &DL, SDValue Val, Align A) {
5454   assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
5455 
5456   // There's no need to assert on a byte-aligned pointer. All pointers are at
5457   // least byte aligned.
5458   if (A == Align(1))
5459     return Val;
5460 
5461   FoldingSetNodeID ID;
5462   AddNodeIDNode(ID, ISD::AssertAlign, getVTList(Val.getValueType()), {Val});
5463   ID.AddInteger(A.value());
5464 
5465   void *IP = nullptr;
5466   if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
5467     return SDValue(E, 0);
5468 
5469   auto *N = newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(),
5470                                          Val.getValueType(), A);
5471   createOperands(N, {Val});
5472 
5473   CSEMap.InsertNode(N, IP);
5474   InsertNode(N);
5475 
5476   SDValue V(N, 0);
5477   NewSDValueDbgMsg(V, "Creating new node: ", this);
5478   return V;
5479 }
5480 
5481 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5482                               SDValue N1, SDValue N2) {
5483   SDNodeFlags Flags;
5484   if (Inserter)
5485     Flags = Inserter->getFlags();
5486   return getNode(Opcode, DL, VT, N1, N2, Flags);
5487 }
5488 
5489 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5490                               SDValue N1, SDValue N2, const SDNodeFlags Flags) {
5491   assert(N1.getOpcode() != ISD::DELETED_NODE &&
5492          N2.getOpcode() != ISD::DELETED_NODE &&
5493          "Operand is DELETED_NODE!");
5494   ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5495   ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
5496   ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
5497   ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
5498 
5499   // Canonicalize constant to RHS if commutative.
5500   if (TLI->isCommutativeBinOp(Opcode)) {
5501     if (N1C && !N2C) {
5502       std::swap(N1C, N2C);
5503       std::swap(N1, N2);
5504     } else if (N1CFP && !N2CFP) {
5505       std::swap(N1CFP, N2CFP);
5506       std::swap(N1, N2);
5507     }
5508   }
5509 
5510   switch (Opcode) {
5511   default: break;
5512   case ISD::TokenFactor:
5513     assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
5514            N2.getValueType() == MVT::Other && "Invalid token factor!");
5515     // Fold trivial token factors.
5516     if (N1.getOpcode() == ISD::EntryToken) return N2;
5517     if (N2.getOpcode() == ISD::EntryToken) return N1;
5518     if (N1 == N2) return N1;
5519     break;
5520   case ISD::BUILD_VECTOR: {
5521     // Attempt to simplify BUILD_VECTOR.
5522     SDValue Ops[] = {N1, N2};
5523     if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
5524       return V;
5525     break;
5526   }
5527   case ISD::CONCAT_VECTORS: {
5528     SDValue Ops[] = {N1, N2};
5529     if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
5530       return V;
5531     break;
5532   }
5533   case ISD::AND:
5534     assert(VT.isInteger() && "This operator does not apply to FP types!");
5535     assert(N1.getValueType() == N2.getValueType() &&
5536            N1.getValueType() == VT && "Binary operator types must match!");
5537     // (X & 0) -> 0.  This commonly occurs when legalizing i64 values, so it's
5538     // worth handling here.
5539     if (N2C && N2C->isNullValue())
5540       return N2;
5541     if (N2C && N2C->isAllOnesValue())  // X & -1 -> X
5542       return N1;
5543     break;
5544   case ISD::OR:
5545   case ISD::XOR:
5546   case ISD::ADD:
5547   case ISD::SUB:
5548     assert(VT.isInteger() && "This operator does not apply to FP types!");
5549     assert(N1.getValueType() == N2.getValueType() &&
5550            N1.getValueType() == VT && "Binary operator types must match!");
5551     // (X ^|+- 0) -> X.  This commonly occurs when legalizing i64 values, so
5552     // it's worth handling here.
5553     if (N2C && N2C->isNullValue())
5554       return N1;
5555     if ((Opcode == ISD::ADD || Opcode == ISD::SUB) && VT.isVector() &&
5556         VT.getVectorElementType() == MVT::i1)
5557       return getNode(ISD::XOR, DL, VT, N1, N2);
5558     break;
5559   case ISD::MUL:
5560     assert(VT.isInteger() && "This operator does not apply to FP types!");
5561     assert(N1.getValueType() == N2.getValueType() &&
5562            N1.getValueType() == VT && "Binary operator types must match!");
5563     if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
5564       return getNode(ISD::AND, DL, VT, N1, N2);
5565     if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
5566       const APInt &MulImm = N1->getConstantOperandAPInt(0);
5567       const APInt &N2CImm = N2C->getAPIntValue();
5568       return getVScale(DL, VT, MulImm * N2CImm);
5569     }
5570     break;
5571   case ISD::UDIV:
5572   case ISD::UREM:
5573   case ISD::MULHU:
5574   case ISD::MULHS:
5575   case ISD::SDIV:
5576   case ISD::SREM:
5577   case ISD::SADDSAT:
5578   case ISD::SSUBSAT:
5579   case ISD::UADDSAT:
5580   case ISD::USUBSAT:
5581     assert(VT.isInteger() && "This operator does not apply to FP types!");
5582     assert(N1.getValueType() == N2.getValueType() &&
5583            N1.getValueType() == VT && "Binary operator types must match!");
5584     if (VT.isVector() && VT.getVectorElementType() == MVT::i1) {
5585       // fold (add_sat x, y) -> (or x, y) for bool types.
5586       if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
5587         return getNode(ISD::OR, DL, VT, N1, N2);
5588       // fold (sub_sat x, y) -> (and x, ~y) for bool types.
5589       if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
5590         return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
5591     }
5592     break;
5593   case ISD::SMIN:
5594   case ISD::UMAX:
5595     assert(VT.isInteger() && "This operator does not apply to FP types!");
5596     assert(N1.getValueType() == N2.getValueType() &&
5597            N1.getValueType() == VT && "Binary operator types must match!");
5598     if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
5599       return getNode(ISD::OR, DL, VT, N1, N2);
5600     break;
5601   case ISD::SMAX:
5602   case ISD::UMIN:
5603     assert(VT.isInteger() && "This operator does not apply to FP types!");
5604     assert(N1.getValueType() == N2.getValueType() &&
5605            N1.getValueType() == VT && "Binary operator types must match!");
5606     if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
5607       return getNode(ISD::AND, DL, VT, N1, N2);
5608     break;
5609   case ISD::FADD:
5610   case ISD::FSUB:
5611   case ISD::FMUL:
5612   case ISD::FDIV:
5613   case ISD::FREM:
5614     assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
5615     assert(N1.getValueType() == N2.getValueType() &&
5616            N1.getValueType() == VT && "Binary operator types must match!");
5617     if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
5618       return V;
5619     break;
5620   case ISD::FCOPYSIGN:   // N1 and result must match.  N1/N2 need not match.
5621     assert(N1.getValueType() == VT &&
5622            N1.getValueType().isFloatingPoint() &&
5623            N2.getValueType().isFloatingPoint() &&
5624            "Invalid FCOPYSIGN!");
5625     break;
5626   case ISD::SHL:
5627     if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
5628       const APInt &MulImm = N1->getConstantOperandAPInt(0);
5629       const APInt &ShiftImm = N2C->getAPIntValue();
5630       return getVScale(DL, VT, MulImm << ShiftImm);
5631     }
5632     LLVM_FALLTHROUGH;
5633   case ISD::SRA:
5634   case ISD::SRL:
5635     if (SDValue V = simplifyShift(N1, N2))
5636       return V;
5637     LLVM_FALLTHROUGH;
5638   case ISD::ROTL:
5639   case ISD::ROTR:
5640     assert(VT == N1.getValueType() &&
5641            "Shift operators return type must be the same as their first arg");
5642     assert(VT.isInteger() && N2.getValueType().isInteger() &&
5643            "Shifts only work on integers");
5644     assert((!VT.isVector() || VT == N2.getValueType()) &&
5645            "Vector shift amounts must be in the same as their first arg");
5646     // Verify that the shift amount VT is big enough to hold valid shift
5647     // amounts.  This catches things like trying to shift an i1024 value by an
5648     // i8, which is easy to fall into in generic code that uses
5649     // TLI.getShiftAmount().
5650     assert(N2.getValueType().getScalarSizeInBits() >=
5651                Log2_32_Ceil(VT.getScalarSizeInBits()) &&
5652            "Invalid use of small shift amount with oversized value!");
5653 
5654     // Always fold shifts of i1 values so the code generator doesn't need to
5655     // handle them.  Since we know the size of the shift has to be less than the
5656     // size of the value, the shift/rotate count is guaranteed to be zero.
5657     if (VT == MVT::i1)
5658       return N1;
5659     if (N2C && N2C->isNullValue())
5660       return N1;
5661     break;
5662   case ISD::FP_ROUND:
5663     assert(VT.isFloatingPoint() &&
5664            N1.getValueType().isFloatingPoint() &&
5665            VT.bitsLE(N1.getValueType()) &&
5666            N2C && (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
5667            "Invalid FP_ROUND!");
5668     if (N1.getValueType() == VT) return N1;  // noop conversion.
5669     break;
5670   case ISD::AssertSext:
5671   case ISD::AssertZext: {
5672     EVT EVT = cast<VTSDNode>(N2)->getVT();
5673     assert(VT == N1.getValueType() && "Not an inreg extend!");
5674     assert(VT.isInteger() && EVT.isInteger() &&
5675            "Cannot *_EXTEND_INREG FP types");
5676     assert(!EVT.isVector() &&
5677            "AssertSExt/AssertZExt type should be the vector element type "
5678            "rather than the vector type!");
5679     assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
5680     if (VT.getScalarType() == EVT) return N1; // noop assertion.
5681     break;
5682   }
5683   case ISD::SIGN_EXTEND_INREG: {
5684     EVT EVT = cast<VTSDNode>(N2)->getVT();
5685     assert(VT == N1.getValueType() && "Not an inreg extend!");
5686     assert(VT.isInteger() && EVT.isInteger() &&
5687            "Cannot *_EXTEND_INREG FP types");
5688     assert(EVT.isVector() == VT.isVector() &&
5689            "SIGN_EXTEND_INREG type should be vector iff the operand "
5690            "type is vector!");
5691     assert((!EVT.isVector() ||
5692             EVT.getVectorElementCount() == VT.getVectorElementCount()) &&
5693            "Vector element counts must match in SIGN_EXTEND_INREG");
5694     assert(EVT.bitsLE(VT) && "Not extending!");
5695     if (EVT == VT) return N1;  // Not actually extending
5696 
5697     auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
5698       unsigned FromBits = EVT.getScalarSizeInBits();
5699       Val <<= Val.getBitWidth() - FromBits;
5700       Val.ashrInPlace(Val.getBitWidth() - FromBits);
5701       return getConstant(Val, DL, ConstantVT);
5702     };
5703 
5704     if (N1C) {
5705       const APInt &Val = N1C->getAPIntValue();
5706       return SignExtendInReg(Val, VT);
5707     }
5708 
5709     if (ISD::isBuildVectorOfConstantSDNodes(N1.getNode())) {
5710       SmallVector<SDValue, 8> Ops;
5711       llvm::EVT OpVT = N1.getOperand(0).getValueType();
5712       for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
5713         SDValue Op = N1.getOperand(i);
5714         if (Op.isUndef()) {
5715           Ops.push_back(getUNDEF(OpVT));
5716           continue;
5717         }
5718         ConstantSDNode *C = cast<ConstantSDNode>(Op);
5719         APInt Val = C->getAPIntValue();
5720         Ops.push_back(SignExtendInReg(Val, OpVT));
5721       }
5722       return getBuildVector(VT, DL, Ops);
5723     }
5724     break;
5725   }
5726   case ISD::FP_TO_SINT_SAT:
5727   case ISD::FP_TO_UINT_SAT: {
5728     assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
5729            N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
5730     assert(N1.getValueType().isVector() == VT.isVector() &&
5731            "FP_TO_*INT_SAT type should be vector iff the operand type is "
5732            "vector!");
5733     assert((!VT.isVector() || VT.getVectorNumElements() ==
5734                                   N1.getValueType().getVectorNumElements()) &&
5735            "Vector element counts must match in FP_TO_*INT_SAT");
5736     assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
5737            "Type to saturate to must be a scalar.");
5738     assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
5739            "Not extending!");
5740     break;
5741   }
5742   case ISD::EXTRACT_VECTOR_ELT:
5743     assert(VT.getSizeInBits() >= N1.getValueType().getScalarSizeInBits() &&
5744            "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
5745              element type of the vector.");
5746 
5747     // Extract from an undefined value or using an undefined index is undefined.
5748     if (N1.isUndef() || N2.isUndef())
5749       return getUNDEF(VT);
5750 
5751     // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
5752     // vectors. For scalable vectors we will provide appropriate support for
5753     // dealing with arbitrary indices.
5754     if (N2C && N1.getValueType().isFixedLengthVector() &&
5755         N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
5756       return getUNDEF(VT);
5757 
5758     // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
5759     // expanding copies of large vectors from registers. This only works for
5760     // fixed length vectors, since we need to know the exact number of
5761     // elements.
5762     if (N2C && N1.getOperand(0).getValueType().isFixedLengthVector() &&
5763         N1.getOpcode() == ISD::CONCAT_VECTORS && N1.getNumOperands() > 0) {
5764       unsigned Factor =
5765         N1.getOperand(0).getValueType().getVectorNumElements();
5766       return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
5767                      N1.getOperand(N2C->getZExtValue() / Factor),
5768                      getVectorIdxConstant(N2C->getZExtValue() % Factor, DL));
5769     }
5770 
5771     // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
5772     // lowering is expanding large vector constants.
5773     if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
5774                 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
5775       assert((N1.getOpcode() != ISD::BUILD_VECTOR ||
5776               N1.getValueType().isFixedLengthVector()) &&
5777              "BUILD_VECTOR used for scalable vectors");
5778       unsigned Index =
5779           N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
5780       SDValue Elt = N1.getOperand(Index);
5781 
5782       if (VT != Elt.getValueType())
5783         // If the vector element type is not legal, the BUILD_VECTOR operands
5784         // are promoted and implicitly truncated, and the result implicitly
5785         // extended. Make that explicit here.
5786         Elt = getAnyExtOrTrunc(Elt, DL, VT);
5787 
5788       return Elt;
5789     }
5790 
5791     // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
5792     // operations are lowered to scalars.
5793     if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
5794       // If the indices are the same, return the inserted element else
5795       // if the indices are known different, extract the element from
5796       // the original vector.
5797       SDValue N1Op2 = N1.getOperand(2);
5798       ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2);
5799 
5800       if (N1Op2C && N2C) {
5801         if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
5802           if (VT == N1.getOperand(1).getValueType())
5803             return N1.getOperand(1);
5804           return getSExtOrTrunc(N1.getOperand(1), DL, VT);
5805         }
5806         return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
5807       }
5808     }
5809 
5810     // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
5811     // when vector types are scalarized and v1iX is legal.
5812     // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
5813     // Here we are completely ignoring the extract element index (N2),
5814     // which is fine for fixed width vectors, since any index other than 0
5815     // is undefined anyway. However, this cannot be ignored for scalable
5816     // vectors - in theory we could support this, but we don't want to do this
5817     // without a profitability check.
5818     if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
5819         N1.getValueType().isFixedLengthVector() &&
5820         N1.getValueType().getVectorNumElements() == 1) {
5821       return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
5822                      N1.getOperand(1));
5823     }
5824     break;
5825   case ISD::EXTRACT_ELEMENT:
5826     assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
5827     assert(!N1.getValueType().isVector() && !VT.isVector() &&
5828            (N1.getValueType().isInteger() == VT.isInteger()) &&
5829            N1.getValueType() != VT &&
5830            "Wrong types for EXTRACT_ELEMENT!");
5831 
5832     // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
5833     // 64-bit integers into 32-bit parts.  Instead of building the extract of
5834     // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
5835     if (N1.getOpcode() == ISD::BUILD_PAIR)
5836       return N1.getOperand(N2C->getZExtValue());
5837 
5838     // EXTRACT_ELEMENT of a constant int is also very common.
5839     if (N1C) {
5840       unsigned ElementSize = VT.getSizeInBits();
5841       unsigned Shift = ElementSize * N2C->getZExtValue();
5842       const APInt &Val = N1C->getAPIntValue();
5843       return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
5844     }
5845     break;
5846   case ISD::EXTRACT_SUBVECTOR: {
5847     EVT N1VT = N1.getValueType();
5848     assert(VT.isVector() && N1VT.isVector() &&
5849            "Extract subvector VTs must be vectors!");
5850     assert(VT.getVectorElementType() == N1VT.getVectorElementType() &&
5851            "Extract subvector VTs must have the same element type!");
5852     assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
5853            "Cannot extract a scalable vector from a fixed length vector!");
5854     assert((VT.isScalableVector() != N1VT.isScalableVector() ||
5855             VT.getVectorMinNumElements() <= N1VT.getVectorMinNumElements()) &&
5856            "Extract subvector must be from larger vector to smaller vector!");
5857     assert(N2C && "Extract subvector index must be a constant");
5858     assert((VT.isScalableVector() != N1VT.isScalableVector() ||
5859             (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
5860                 N1VT.getVectorMinNumElements()) &&
5861            "Extract subvector overflow!");
5862     assert(N2C->getAPIntValue().getBitWidth() ==
5863                TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
5864            "Constant index for EXTRACT_SUBVECTOR has an invalid size");
5865 
5866     // Trivial extraction.
5867     if (VT == N1VT)
5868       return N1;
5869 
5870     // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
5871     if (N1.isUndef())
5872       return getUNDEF(VT);
5873 
5874     // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
5875     // the concat have the same type as the extract.
5876     if (N1.getOpcode() == ISD::CONCAT_VECTORS && N1.getNumOperands() > 0 &&
5877         VT == N1.getOperand(0).getValueType()) {
5878       unsigned Factor = VT.getVectorMinNumElements();
5879       return N1.getOperand(N2C->getZExtValue() / Factor);
5880     }
5881 
5882     // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
5883     // during shuffle legalization.
5884     if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
5885         VT == N1.getOperand(1).getValueType())
5886       return N1.getOperand(1);
5887     break;
5888   }
5889   }
5890 
5891   // Perform trivial constant folding.
5892   if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}))
5893     return SV;
5894 
5895   if (SDValue V = foldConstantFPMath(Opcode, DL, VT, N1, N2))
5896     return V;
5897 
5898   // Canonicalize an UNDEF to the RHS, even over a constant.
5899   if (N1.isUndef()) {
5900     if (TLI->isCommutativeBinOp(Opcode)) {
5901       std::swap(N1, N2);
5902     } else {
5903       switch (Opcode) {
5904       case ISD::SIGN_EXTEND_INREG:
5905       case ISD::SUB:
5906         return getUNDEF(VT);     // fold op(undef, arg2) -> undef
5907       case ISD::UDIV:
5908       case ISD::SDIV:
5909       case ISD::UREM:
5910       case ISD::SREM:
5911       case ISD::SSUBSAT:
5912       case ISD::USUBSAT:
5913         return getConstant(0, DL, VT);    // fold op(undef, arg2) -> 0
5914       }
5915     }
5916   }
5917 
5918   // Fold a bunch of operators when the RHS is undef.
5919   if (N2.isUndef()) {
5920     switch (Opcode) {
5921     case ISD::XOR:
5922       if (N1.isUndef())
5923         // Handle undef ^ undef -> 0 special case. This is a common
5924         // idiom (misuse).
5925         return getConstant(0, DL, VT);
5926       LLVM_FALLTHROUGH;
5927     case ISD::ADD:
5928     case ISD::SUB:
5929     case ISD::UDIV:
5930     case ISD::SDIV:
5931     case ISD::UREM:
5932     case ISD::SREM:
5933       return getUNDEF(VT);       // fold op(arg1, undef) -> undef
5934     case ISD::MUL:
5935     case ISD::AND:
5936     case ISD::SSUBSAT:
5937     case ISD::USUBSAT:
5938       return getConstant(0, DL, VT);  // fold op(arg1, undef) -> 0
5939     case ISD::OR:
5940     case ISD::SADDSAT:
5941     case ISD::UADDSAT:
5942       return getAllOnesConstant(DL, VT);
5943     }
5944   }
5945 
5946   // Memoize this node if possible.
5947   SDNode *N;
5948   SDVTList VTs = getVTList(VT);
5949   SDValue Ops[] = {N1, N2};
5950   if (VT != MVT::Glue) {
5951     FoldingSetNodeID ID;
5952     AddNodeIDNode(ID, Opcode, VTs, Ops);
5953     void *IP = nullptr;
5954     if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
5955       E->intersectFlagsWith(Flags);
5956       return SDValue(E, 0);
5957     }
5958 
5959     N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
5960     N->setFlags(Flags);
5961     createOperands(N, Ops);
5962     CSEMap.InsertNode(N, IP);
5963   } else {
5964     N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
5965     createOperands(N, Ops);
5966   }
5967 
5968   InsertNode(N);
5969   SDValue V = SDValue(N, 0);
5970   NewSDValueDbgMsg(V, "Creating new node: ", this);
5971   return V;
5972 }
5973 
5974 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5975                               SDValue N1, SDValue N2, SDValue N3) {
5976   SDNodeFlags Flags;
5977   if (Inserter)
5978     Flags = Inserter->getFlags();
5979   return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
5980 }
5981 
5982 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5983                               SDValue N1, SDValue N2, SDValue N3,
5984                               const SDNodeFlags Flags) {
5985   assert(N1.getOpcode() != ISD::DELETED_NODE &&
5986          N2.getOpcode() != ISD::DELETED_NODE &&
5987          N3.getOpcode() != ISD::DELETED_NODE &&
5988          "Operand is DELETED_NODE!");
5989   // Perform various simplifications.
5990   switch (Opcode) {
5991   case ISD::FMA: {
5992     assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
5993     assert(N1.getValueType() == VT && N2.getValueType() == VT &&
5994            N3.getValueType() == VT && "FMA types must match!");
5995     ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
5996     ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
5997     ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
5998     if (N1CFP && N2CFP && N3CFP) {
5999       APFloat  V1 = N1CFP->getValueAPF();
6000       const APFloat &V2 = N2CFP->getValueAPF();
6001       const APFloat &V3 = N3CFP->getValueAPF();
6002       V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
6003       return getConstantFP(V1, DL, VT);
6004     }
6005     break;
6006   }
6007   case ISD::BUILD_VECTOR: {
6008     // Attempt to simplify BUILD_VECTOR.
6009     SDValue Ops[] = {N1, N2, N3};
6010     if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6011       return V;
6012     break;
6013   }
6014   case ISD::CONCAT_VECTORS: {
6015     SDValue Ops[] = {N1, N2, N3};
6016     if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
6017       return V;
6018     break;
6019   }
6020   case ISD::SETCC: {
6021     assert(VT.isInteger() && "SETCC result type must be an integer!");
6022     assert(N1.getValueType() == N2.getValueType() &&
6023            "SETCC operands must have the same type!");
6024     assert(VT.isVector() == N1.getValueType().isVector() &&
6025            "SETCC type should be vector iff the operand type is vector!");
6026     assert((!VT.isVector() || VT.getVectorElementCount() ==
6027                                   N1.getValueType().getVectorElementCount()) &&
6028            "SETCC vector element counts must match!");
6029     // Use FoldSetCC to simplify SETCC's.
6030     if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
6031       return V;
6032     // Vector constant folding.
6033     SDValue Ops[] = {N1, N2, N3};
6034     if (SDValue V = FoldConstantVectorArithmetic(Opcode, DL, VT, Ops)) {
6035       NewSDValueDbgMsg(V, "New node vector constant folding: ", this);
6036       return V;
6037     }
6038     break;
6039   }
6040   case ISD::SELECT:
6041   case ISD::VSELECT:
6042     if (SDValue V = simplifySelect(N1, N2, N3))
6043       return V;
6044     break;
6045   case ISD::VECTOR_SHUFFLE:
6046     llvm_unreachable("should use getVectorShuffle constructor!");
6047   case ISD::INSERT_VECTOR_ELT: {
6048     ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3);
6049     // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
6050     // for scalable vectors where we will generate appropriate code to
6051     // deal with out-of-bounds cases correctly.
6052     if (N3C && N1.getValueType().isFixedLengthVector() &&
6053         N3C->getZExtValue() >= N1.getValueType().getVectorNumElements())
6054       return getUNDEF(VT);
6055 
6056     // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
6057     if (N3.isUndef())
6058       return getUNDEF(VT);
6059 
6060     // If the inserted element is an UNDEF, just use the input vector.
6061     if (N2.isUndef())
6062       return N1;
6063 
6064     break;
6065   }
6066   case ISD::INSERT_SUBVECTOR: {
6067     // Inserting undef into undef is still undef.
6068     if (N1.isUndef() && N2.isUndef())
6069       return getUNDEF(VT);
6070 
6071     EVT N2VT = N2.getValueType();
6072     assert(VT == N1.getValueType() &&
6073            "Dest and insert subvector source types must match!");
6074     assert(VT.isVector() && N2VT.isVector() &&
6075            "Insert subvector VTs must be vectors!");
6076     assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
6077            "Cannot insert a scalable vector into a fixed length vector!");
6078     assert((VT.isScalableVector() != N2VT.isScalableVector() ||
6079             VT.getVectorMinNumElements() >= N2VT.getVectorMinNumElements()) &&
6080            "Insert subvector must be from smaller vector to larger vector!");
6081     assert(isa<ConstantSDNode>(N3) &&
6082            "Insert subvector index must be constant");
6083     assert((VT.isScalableVector() != N2VT.isScalableVector() ||
6084             (N2VT.getVectorMinNumElements() +
6085              cast<ConstantSDNode>(N3)->getZExtValue()) <=
6086                 VT.getVectorMinNumElements()) &&
6087            "Insert subvector overflow!");
6088     assert(cast<ConstantSDNode>(N3)->getAPIntValue().getBitWidth() ==
6089                TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
6090            "Constant index for INSERT_SUBVECTOR has an invalid size");
6091 
6092     // Trivial insertion.
6093     if (VT == N2VT)
6094       return N2;
6095 
6096     // If this is an insert of an extracted vector into an undef vector, we
6097     // can just use the input to the extract.
6098     if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
6099         N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT)
6100       return N2.getOperand(0);
6101     break;
6102   }
6103   case ISD::BITCAST:
6104     // Fold bit_convert nodes from a type to themselves.
6105     if (N1.getValueType() == VT)
6106       return N1;
6107     break;
6108   }
6109 
6110   // Memoize node if it doesn't produce a flag.
6111   SDNode *N;
6112   SDVTList VTs = getVTList(VT);
6113   SDValue Ops[] = {N1, N2, N3};
6114   if (VT != MVT::Glue) {
6115     FoldingSetNodeID ID;
6116     AddNodeIDNode(ID, Opcode, VTs, Ops);
6117     void *IP = nullptr;
6118     if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6119       E->intersectFlagsWith(Flags);
6120       return SDValue(E, 0);
6121     }
6122 
6123     N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6124     N->setFlags(Flags);
6125     createOperands(N, Ops);
6126     CSEMap.InsertNode(N, IP);
6127   } else {
6128     N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6129     createOperands(N, Ops);
6130   }
6131 
6132   InsertNode(N);
6133   SDValue V = SDValue(N, 0);
6134   NewSDValueDbgMsg(V, "Creating new node: ", this);
6135   return V;
6136 }
6137 
6138 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6139                               SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
6140   SDValue Ops[] = { N1, N2, N3, N4 };
6141   return getNode(Opcode, DL, VT, Ops);
6142 }
6143 
6144 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6145                               SDValue N1, SDValue N2, SDValue N3, SDValue N4,
6146                               SDValue N5) {
6147   SDValue Ops[] = { N1, N2, N3, N4, N5 };
6148   return getNode(Opcode, DL, VT, Ops);
6149 }
6150 
6151 /// getStackArgumentTokenFactor - Compute a TokenFactor to force all
6152 /// the incoming stack arguments to be loaded from the stack.
6153 SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
6154   SmallVector<SDValue, 8> ArgChains;
6155 
6156   // Include the original chain at the beginning of the list. When this is
6157   // used by target LowerCall hooks, this helps legalize find the
6158   // CALLSEQ_BEGIN node.
6159   ArgChains.push_back(Chain);
6160 
6161   // Add a chain value for each stack argument.
6162   for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
6163        UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
6164     if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
6165       if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
6166         if (FI->getIndex() < 0)
6167           ArgChains.push_back(SDValue(L, 1));
6168 
6169   // Build a tokenfactor for all the chains.
6170   return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
6171 }
6172 
6173 /// getMemsetValue - Vectorized representation of the memset value
6174 /// operand.
6175 static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
6176                               const SDLoc &dl) {
6177   assert(!Value.isUndef());
6178 
6179   unsigned NumBits = VT.getScalarSizeInBits();
6180   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
6181     assert(C->getAPIntValue().getBitWidth() == 8);
6182     APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
6183     if (VT.isInteger()) {
6184       bool IsOpaque = VT.getSizeInBits() > 64 ||
6185           !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
6186       return DAG.getConstant(Val, dl, VT, false, IsOpaque);
6187     }
6188     return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), dl,
6189                              VT);
6190   }
6191 
6192   assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
6193   EVT IntVT = VT.getScalarType();
6194   if (!IntVT.isInteger())
6195     IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
6196 
6197   Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
6198   if (NumBits > 8) {
6199     // Use a multiplication with 0x010101... to extend the input to the
6200     // required length.
6201     APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
6202     Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
6203                         DAG.getConstant(Magic, dl, IntVT));
6204   }
6205 
6206   if (VT != Value.getValueType() && !VT.isInteger())
6207     Value = DAG.getBitcast(VT.getScalarType(), Value);
6208   if (VT != Value.getValueType())
6209     Value = DAG.getSplatBuildVector(VT, dl, Value);
6210 
6211   return Value;
6212 }
6213 
6214 /// getMemsetStringVal - Similar to getMemsetValue. Except this is only
6215 /// used when a memcpy is turned into a memset when the source is a constant
6216 /// string ptr.
6217 static SDValue getMemsetStringVal(EVT VT, const SDLoc &dl, SelectionDAG &DAG,
6218                                   const TargetLowering &TLI,
6219                                   const ConstantDataArraySlice &Slice) {
6220   // Handle vector with all elements zero.
6221   if (Slice.Array == nullptr) {
6222     if (VT.isInteger())
6223       return DAG.getConstant(0, dl, VT);
6224     if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
6225       return DAG.getConstantFP(0.0, dl, VT);
6226     if (VT.isVector()) {
6227       unsigned NumElts = VT.getVectorNumElements();
6228       MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
6229       return DAG.getNode(ISD::BITCAST, dl, VT,
6230                          DAG.getConstant(0, dl,
6231                                          EVT::getVectorVT(*DAG.getContext(),
6232                                                           EltVT, NumElts)));
6233     }
6234     llvm_unreachable("Expected type!");
6235   }
6236 
6237   assert(!VT.isVector() && "Can't handle vector type here!");
6238   unsigned NumVTBits = VT.getSizeInBits();
6239   unsigned NumVTBytes = NumVTBits / 8;
6240   unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
6241 
6242   APInt Val(NumVTBits, 0);
6243   if (DAG.getDataLayout().isLittleEndian()) {
6244     for (unsigned i = 0; i != NumBytes; ++i)
6245       Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
6246   } else {
6247     for (unsigned i = 0; i != NumBytes; ++i)
6248       Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
6249   }
6250 
6251   // If the "cost" of materializing the integer immediate is less than the cost
6252   // of a load, then it is cost effective to turn the load into the immediate.
6253   Type *Ty = VT.getTypeForEVT(*DAG.getContext());
6254   if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
6255     return DAG.getConstant(Val, dl, VT);
6256   return SDValue(nullptr, 0);
6257 }
6258 
6259 SDValue SelectionDAG::getMemBasePlusOffset(SDValue Base, TypeSize Offset,
6260                                            const SDLoc &DL,
6261                                            const SDNodeFlags Flags) {
6262   EVT VT = Base.getValueType();
6263   SDValue Index;
6264 
6265   if (Offset.isScalable())
6266     Index = getVScale(DL, Base.getValueType(),
6267                       APInt(Base.getValueSizeInBits().getFixedSize(),
6268                             Offset.getKnownMinSize()));
6269   else
6270     Index = getConstant(Offset.getFixedSize(), DL, VT);
6271 
6272   return getMemBasePlusOffset(Base, Index, DL, Flags);
6273 }
6274 
6275 SDValue SelectionDAG::getMemBasePlusOffset(SDValue Ptr, SDValue Offset,
6276                                            const SDLoc &DL,
6277                                            const SDNodeFlags Flags) {
6278   assert(Offset.getValueType().isInteger());
6279   EVT BasePtrVT = Ptr.getValueType();
6280   return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, Flags);
6281 }
6282 
6283 /// Returns true if memcpy source is constant data.
6284 static bool isMemSrcFromConstant(SDValue Src, ConstantDataArraySlice &Slice) {
6285   uint64_t SrcDelta = 0;
6286   GlobalAddressSDNode *G = nullptr;
6287   if (Src.getOpcode() == ISD::GlobalAddress)
6288     G = cast<GlobalAddressSDNode>(Src);
6289   else if (Src.getOpcode() == ISD::ADD &&
6290            Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
6291            Src.getOperand(1).getOpcode() == ISD::Constant) {
6292     G = cast<GlobalAddressSDNode>(Src.getOperand(0));
6293     SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
6294   }
6295   if (!G)
6296     return false;
6297 
6298   return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
6299                                   SrcDelta + G->getOffset());
6300 }
6301 
6302 static bool shouldLowerMemFuncForSize(const MachineFunction &MF,
6303                                       SelectionDAG &DAG) {
6304   // On Darwin, -Os means optimize for size without hurting performance, so
6305   // only really optimize for size when -Oz (MinSize) is used.
6306   if (MF.getTarget().getTargetTriple().isOSDarwin())
6307     return MF.getFunction().hasMinSize();
6308   return DAG.shouldOptForSize();
6309 }
6310 
6311 static void chainLoadsAndStoresForMemcpy(SelectionDAG &DAG, const SDLoc &dl,
6312                           SmallVector<SDValue, 32> &OutChains, unsigned From,
6313                           unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
6314                           SmallVector<SDValue, 16> &OutStoreChains) {
6315   assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
6316   assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
6317   SmallVector<SDValue, 16> GluedLoadChains;
6318   for (unsigned i = From; i < To; ++i) {
6319     OutChains.push_back(OutLoadChains[i]);
6320     GluedLoadChains.push_back(OutLoadChains[i]);
6321   }
6322 
6323   // Chain for all loads.
6324   SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
6325                                   GluedLoadChains);
6326 
6327   for (unsigned i = From; i < To; ++i) {
6328     StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
6329     SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
6330                                   ST->getBasePtr(), ST->getMemoryVT(),
6331                                   ST->getMemOperand());
6332     OutChains.push_back(NewStore);
6333   }
6334 }
6335 
6336 static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl,
6337                                        SDValue Chain, SDValue Dst, SDValue Src,
6338                                        uint64_t Size, Align Alignment,
6339                                        bool isVol, bool AlwaysInline,
6340                                        MachinePointerInfo DstPtrInfo,
6341                                        MachinePointerInfo SrcPtrInfo,
6342                                        const AAMDNodes &AAInfo) {
6343   // Turn a memcpy of undef to nop.
6344   // FIXME: We need to honor volatile even is Src is undef.
6345   if (Src.isUndef())
6346     return Chain;
6347 
6348   // Expand memcpy to a series of load and store ops if the size operand falls
6349   // below a certain threshold.
6350   // TODO: In the AlwaysInline case, if the size is big then generate a loop
6351   // rather than maybe a humongous number of loads and stores.
6352   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6353   const DataLayout &DL = DAG.getDataLayout();
6354   LLVMContext &C = *DAG.getContext();
6355   std::vector<EVT> MemOps;
6356   bool DstAlignCanChange = false;
6357   MachineFunction &MF = DAG.getMachineFunction();
6358   MachineFrameInfo &MFI = MF.getFrameInfo();
6359   bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
6360   FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
6361   if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
6362     DstAlignCanChange = true;
6363   MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
6364   if (!SrcAlign || Alignment > *SrcAlign)
6365     SrcAlign = Alignment;
6366   assert(SrcAlign && "SrcAlign must be set");
6367   ConstantDataArraySlice Slice;
6368   // If marked as volatile, perform a copy even when marked as constant.
6369   bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
6370   bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
6371   unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
6372   const MemOp Op = isZeroConstant
6373                        ? MemOp::Set(Size, DstAlignCanChange, Alignment,
6374                                     /*IsZeroMemset*/ true, isVol)
6375                        : MemOp::Copy(Size, DstAlignCanChange, Alignment,
6376                                      *SrcAlign, isVol, CopyFromConstant);
6377   if (!TLI.findOptimalMemOpLowering(
6378           MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
6379           SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
6380     return SDValue();
6381 
6382   if (DstAlignCanChange) {
6383     Type *Ty = MemOps[0].getTypeForEVT(C);
6384     Align NewAlign = DL.getABITypeAlign(Ty);
6385 
6386     // Don't promote to an alignment that would require dynamic stack
6387     // realignment.
6388     const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
6389     if (!TRI->hasStackRealignment(MF))
6390       while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
6391         NewAlign = NewAlign / 2;
6392 
6393     if (NewAlign > Alignment) {
6394       // Give the stack frame object a larger alignment if needed.
6395       if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
6396         MFI.setObjectAlignment(FI->getIndex(), NewAlign);
6397       Alignment = NewAlign;
6398     }
6399   }
6400 
6401   // Prepare AAInfo for loads/stores after lowering this memcpy.
6402   AAMDNodes NewAAInfo = AAInfo;
6403   NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
6404 
6405   MachineMemOperand::Flags MMOFlags =
6406       isVol ? MachineMemOperand::MOVolatile : MachineMemOperand::MONone;
6407   SmallVector<SDValue, 16> OutLoadChains;
6408   SmallVector<SDValue, 16> OutStoreChains;
6409   SmallVector<SDValue, 32> OutChains;
6410   unsigned NumMemOps = MemOps.size();
6411   uint64_t SrcOff = 0, DstOff = 0;
6412   for (unsigned i = 0; i != NumMemOps; ++i) {
6413     EVT VT = MemOps[i];
6414     unsigned VTSize = VT.getSizeInBits() / 8;
6415     SDValue Value, Store;
6416 
6417     if (VTSize > Size) {
6418       // Issuing an unaligned load / store pair  that overlaps with the previous
6419       // pair. Adjust the offset accordingly.
6420       assert(i == NumMemOps-1 && i != 0);
6421       SrcOff -= VTSize - Size;
6422       DstOff -= VTSize - Size;
6423     }
6424 
6425     if (CopyFromConstant &&
6426         (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
6427       // It's unlikely a store of a vector immediate can be done in a single
6428       // instruction. It would require a load from a constantpool first.
6429       // We only handle zero vectors here.
6430       // FIXME: Handle other cases where store of vector immediate is done in
6431       // a single instruction.
6432       ConstantDataArraySlice SubSlice;
6433       if (SrcOff < Slice.Length) {
6434         SubSlice = Slice;
6435         SubSlice.move(SrcOff);
6436       } else {
6437         // This is an out-of-bounds access and hence UB. Pretend we read zero.
6438         SubSlice.Array = nullptr;
6439         SubSlice.Offset = 0;
6440         SubSlice.Length = VTSize;
6441       }
6442       Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
6443       if (Value.getNode()) {
6444         Store = DAG.getStore(
6445             Chain, dl, Value,
6446             DAG.getMemBasePlusOffset(Dst, TypeSize::Fixed(DstOff), dl),
6447             DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
6448         OutChains.push_back(Store);
6449       }
6450     }
6451 
6452     if (!Store.getNode()) {
6453       // The type might not be legal for the target.  This should only happen
6454       // if the type is smaller than a legal type, as on PPC, so the right
6455       // thing to do is generate a LoadExt/StoreTrunc pair.  These simplify
6456       // to Load/Store if NVT==VT.
6457       // FIXME does the case above also need this?
6458       EVT NVT = TLI.getTypeToTransformTo(C, VT);
6459       assert(NVT.bitsGE(VT));
6460 
6461       bool isDereferenceable =
6462         SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
6463       MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
6464       if (isDereferenceable)
6465         SrcMMOFlags |= MachineMemOperand::MODereferenceable;
6466 
6467       Value = DAG.getExtLoad(
6468           ISD::EXTLOAD, dl, NVT, Chain,
6469           DAG.getMemBasePlusOffset(Src, TypeSize::Fixed(SrcOff), dl),
6470           SrcPtrInfo.getWithOffset(SrcOff), VT,
6471           commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
6472       OutLoadChains.push_back(Value.getValue(1));
6473 
6474       Store = DAG.getTruncStore(
6475           Chain, dl, Value,
6476           DAG.getMemBasePlusOffset(Dst, TypeSize::Fixed(DstOff), dl),
6477           DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
6478       OutStoreChains.push_back(Store);
6479     }
6480     SrcOff += VTSize;
6481     DstOff += VTSize;
6482     Size -= VTSize;
6483   }
6484 
6485   unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
6486                                 TLI.getMaxGluedStoresPerMemcpy() : MaxLdStGlue;
6487   unsigned NumLdStInMemcpy = OutStoreChains.size();
6488 
6489   if (NumLdStInMemcpy) {
6490     // It may be that memcpy might be converted to memset if it's memcpy
6491     // of constants. In such a case, we won't have loads and stores, but
6492     // just stores. In the absence of loads, there is nothing to gang up.
6493     if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
6494       // If target does not care, just leave as it.
6495       for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
6496         OutChains.push_back(OutLoadChains[i]);
6497         OutChains.push_back(OutStoreChains[i]);
6498       }
6499     } else {
6500       // Ld/St less than/equal limit set by target.
6501       if (NumLdStInMemcpy <= GluedLdStLimit) {
6502           chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
6503                                         NumLdStInMemcpy, OutLoadChains,
6504                                         OutStoreChains);
6505       } else {
6506         unsigned NumberLdChain =  NumLdStInMemcpy / GluedLdStLimit;
6507         unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
6508         unsigned GlueIter = 0;
6509 
6510         for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
6511           unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
6512           unsigned IndexTo   = NumLdStInMemcpy - GlueIter;
6513 
6514           chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
6515                                        OutLoadChains, OutStoreChains);
6516           GlueIter += GluedLdStLimit;
6517         }
6518 
6519         // Residual ld/st.
6520         if (RemainingLdStInMemcpy) {
6521           chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
6522                                         RemainingLdStInMemcpy, OutLoadChains,
6523                                         OutStoreChains);
6524         }
6525       }
6526     }
6527   }
6528   return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
6529 }
6530 
6531 static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl,
6532                                         SDValue Chain, SDValue Dst, SDValue Src,
6533                                         uint64_t Size, Align Alignment,
6534                                         bool isVol, bool AlwaysInline,
6535                                         MachinePointerInfo DstPtrInfo,
6536                                         MachinePointerInfo SrcPtrInfo,
6537                                         const AAMDNodes &AAInfo) {
6538   // Turn a memmove of undef to nop.
6539   // FIXME: We need to honor volatile even is Src is undef.
6540   if (Src.isUndef())
6541     return Chain;
6542 
6543   // Expand memmove to a series of load and store ops if the size operand falls
6544   // below a certain threshold.
6545   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6546   const DataLayout &DL = DAG.getDataLayout();
6547   LLVMContext &C = *DAG.getContext();
6548   std::vector<EVT> MemOps;
6549   bool DstAlignCanChange = false;
6550   MachineFunction &MF = DAG.getMachineFunction();
6551   MachineFrameInfo &MFI = MF.getFrameInfo();
6552   bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
6553   FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
6554   if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
6555     DstAlignCanChange = true;
6556   MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
6557   if (!SrcAlign || Alignment > *SrcAlign)
6558     SrcAlign = Alignment;
6559   assert(SrcAlign && "SrcAlign must be set");
6560   unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
6561   if (!TLI.findOptimalMemOpLowering(
6562           MemOps, Limit,
6563           MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
6564                       /*IsVolatile*/ true),
6565           DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
6566           MF.getFunction().getAttributes()))
6567     return SDValue();
6568 
6569   if (DstAlignCanChange) {
6570     Type *Ty = MemOps[0].getTypeForEVT(C);
6571     Align NewAlign = DL.getABITypeAlign(Ty);
6572     if (NewAlign > Alignment) {
6573       // Give the stack frame object a larger alignment if needed.
6574       if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
6575         MFI.setObjectAlignment(FI->getIndex(), NewAlign);
6576       Alignment = NewAlign;
6577     }
6578   }
6579 
6580   // Prepare AAInfo for loads/stores after lowering this memmove.
6581   AAMDNodes NewAAInfo = AAInfo;
6582   NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
6583 
6584   MachineMemOperand::Flags MMOFlags =
6585       isVol ? MachineMemOperand::MOVolatile : MachineMemOperand::MONone;
6586   uint64_t SrcOff = 0, DstOff = 0;
6587   SmallVector<SDValue, 8> LoadValues;
6588   SmallVector<SDValue, 8> LoadChains;
6589   SmallVector<SDValue, 8> OutChains;
6590   unsigned NumMemOps = MemOps.size();
6591   for (unsigned i = 0; i < NumMemOps; i++) {
6592     EVT VT = MemOps[i];
6593     unsigned VTSize = VT.getSizeInBits() / 8;
6594     SDValue Value;
6595 
6596     bool isDereferenceable =
6597       SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
6598     MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
6599     if (isDereferenceable)
6600       SrcMMOFlags |= MachineMemOperand::MODereferenceable;
6601 
6602     Value = DAG.getLoad(
6603         VT, dl, Chain,
6604         DAG.getMemBasePlusOffset(Src, TypeSize::Fixed(SrcOff), dl),
6605         SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
6606     LoadValues.push_back(Value);
6607     LoadChains.push_back(Value.getValue(1));
6608     SrcOff += VTSize;
6609   }
6610   Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
6611   OutChains.clear();
6612   for (unsigned i = 0; i < NumMemOps; i++) {
6613     EVT VT = MemOps[i];
6614     unsigned VTSize = VT.getSizeInBits() / 8;
6615     SDValue Store;
6616 
6617     Store = DAG.getStore(
6618         Chain, dl, LoadValues[i],
6619         DAG.getMemBasePlusOffset(Dst, TypeSize::Fixed(DstOff), dl),
6620         DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
6621     OutChains.push_back(Store);
6622     DstOff += VTSize;
6623   }
6624 
6625   return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
6626 }
6627 
6628 /// Lower the call to 'memset' intrinsic function into a series of store
6629 /// operations.
6630 ///
6631 /// \param DAG Selection DAG where lowered code is placed.
6632 /// \param dl Link to corresponding IR location.
6633 /// \param Chain Control flow dependency.
6634 /// \param Dst Pointer to destination memory location.
6635 /// \param Src Value of byte to write into the memory.
6636 /// \param Size Number of bytes to write.
6637 /// \param Alignment Alignment of the destination in bytes.
6638 /// \param isVol True if destination is volatile.
6639 /// \param DstPtrInfo IR information on the memory pointer.
6640 /// \returns New head in the control flow, if lowering was successful, empty
6641 /// SDValue otherwise.
6642 ///
6643 /// The function tries to replace 'llvm.memset' intrinsic with several store
6644 /// operations and value calculation code. This is usually profitable for small
6645 /// memory size.
6646 static SDValue getMemsetStores(SelectionDAG &DAG, const SDLoc &dl,
6647                                SDValue Chain, SDValue Dst, SDValue Src,
6648                                uint64_t Size, Align Alignment, bool isVol,
6649                                MachinePointerInfo DstPtrInfo,
6650                                const AAMDNodes &AAInfo) {
6651   // Turn a memset of undef to nop.
6652   // FIXME: We need to honor volatile even is Src is undef.
6653   if (Src.isUndef())
6654     return Chain;
6655 
6656   // Expand memset to a series of load/store ops if the size operand
6657   // falls below a certain threshold.
6658   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6659   std::vector<EVT> MemOps;
6660   bool DstAlignCanChange = false;
6661   MachineFunction &MF = DAG.getMachineFunction();
6662   MachineFrameInfo &MFI = MF.getFrameInfo();
6663   bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
6664   FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
6665   if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
6666     DstAlignCanChange = true;
6667   bool IsZeroVal =
6668     isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
6669   if (!TLI.findOptimalMemOpLowering(
6670           MemOps, TLI.getMaxStoresPerMemset(OptSize),
6671           MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
6672           DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
6673     return SDValue();
6674 
6675   if (DstAlignCanChange) {
6676     Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
6677     Align NewAlign = DAG.getDataLayout().getABITypeAlign(Ty);
6678     if (NewAlign > Alignment) {
6679       // Give the stack frame object a larger alignment if needed.
6680       if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
6681         MFI.setObjectAlignment(FI->getIndex(), NewAlign);
6682       Alignment = NewAlign;
6683     }
6684   }
6685 
6686   SmallVector<SDValue, 8> OutChains;
6687   uint64_t DstOff = 0;
6688   unsigned NumMemOps = MemOps.size();
6689 
6690   // Find the largest store and generate the bit pattern for it.
6691   EVT LargestVT = MemOps[0];
6692   for (unsigned i = 1; i < NumMemOps; i++)
6693     if (MemOps[i].bitsGT(LargestVT))
6694       LargestVT = MemOps[i];
6695   SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
6696 
6697   // Prepare AAInfo for loads/stores after lowering this memset.
6698   AAMDNodes NewAAInfo = AAInfo;
6699   NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
6700 
6701   for (unsigned i = 0; i < NumMemOps; i++) {
6702     EVT VT = MemOps[i];
6703     unsigned VTSize = VT.getSizeInBits() / 8;
6704     if (VTSize > Size) {
6705       // Issuing an unaligned load / store pair  that overlaps with the previous
6706       // pair. Adjust the offset accordingly.
6707       assert(i == NumMemOps-1 && i != 0);
6708       DstOff -= VTSize - Size;
6709     }
6710 
6711     // If this store is smaller than the largest store see whether we can get
6712     // the smaller value for free with a truncate.
6713     SDValue Value = MemSetValue;
6714     if (VT.bitsLT(LargestVT)) {
6715       if (!LargestVT.isVector() && !VT.isVector() &&
6716           TLI.isTruncateFree(LargestVT, VT))
6717         Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
6718       else
6719         Value = getMemsetValue(Src, VT, DAG, dl);
6720     }
6721     assert(Value.getValueType() == VT && "Value with wrong type.");
6722     SDValue Store = DAG.getStore(
6723         Chain, dl, Value,
6724         DAG.getMemBasePlusOffset(Dst, TypeSize::Fixed(DstOff), dl),
6725         DstPtrInfo.getWithOffset(DstOff), Alignment,
6726         isVol ? MachineMemOperand::MOVolatile : MachineMemOperand::MONone,
6727         NewAAInfo);
6728     OutChains.push_back(Store);
6729     DstOff += VT.getSizeInBits() / 8;
6730     Size -= VTSize;
6731   }
6732 
6733   return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
6734 }
6735 
6736 static void checkAddrSpaceIsValidForLibcall(const TargetLowering *TLI,
6737                                             unsigned AS) {
6738   // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
6739   // pointer operands can be losslessly bitcasted to pointers of address space 0
6740   if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
6741     report_fatal_error("cannot lower memory intrinsic in address space " +
6742                        Twine(AS));
6743   }
6744 }
6745 
6746 SDValue SelectionDAG::getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst,
6747                                 SDValue Src, SDValue Size, Align Alignment,
6748                                 bool isVol, bool AlwaysInline, bool isTailCall,
6749                                 MachinePointerInfo DstPtrInfo,
6750                                 MachinePointerInfo SrcPtrInfo,
6751                                 const AAMDNodes &AAInfo) {
6752   // Check to see if we should lower the memcpy to loads and stores first.
6753   // For cases within the target-specified limits, this is the best choice.
6754   ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6755   if (ConstantSize) {
6756     // Memcpy with size zero? Just return the original chain.
6757     if (ConstantSize->isNullValue())
6758       return Chain;
6759 
6760     SDValue Result = getMemcpyLoadsAndStores(
6761         *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
6762         isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
6763     if (Result.getNode())
6764       return Result;
6765   }
6766 
6767   // Then check to see if we should lower the memcpy with target-specific
6768   // code. If the target chooses to do this, this is the next best.
6769   if (TSI) {
6770     SDValue Result = TSI->EmitTargetCodeForMemcpy(
6771         *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
6772         DstPtrInfo, SrcPtrInfo);
6773     if (Result.getNode())
6774       return Result;
6775   }
6776 
6777   // If we really need inline code and the target declined to provide it,
6778   // use a (potentially long) sequence of loads and stores.
6779   if (AlwaysInline) {
6780     assert(ConstantSize && "AlwaysInline requires a constant size!");
6781     return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
6782                                    ConstantSize->getZExtValue(), Alignment,
6783                                    isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo);
6784   }
6785 
6786   checkAddrSpaceIsValidForLibcall(TLI, DstPtrInfo.getAddrSpace());
6787   checkAddrSpaceIsValidForLibcall(TLI, SrcPtrInfo.getAddrSpace());
6788 
6789   // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
6790   // memcpy is not guaranteed to be safe. libc memcpys aren't required to
6791   // respect volatile, so they may do things like read or write memory
6792   // beyond the given memory regions. But fixing this isn't easy, and most
6793   // people don't care.
6794 
6795   // Emit a library call.
6796   TargetLowering::ArgListTy Args;
6797   TargetLowering::ArgListEntry Entry;
6798   Entry.Ty = Type::getInt8PtrTy(*getContext());
6799   Entry.Node = Dst; Args.push_back(Entry);
6800   Entry.Node = Src; Args.push_back(Entry);
6801 
6802   Entry.Ty = getDataLayout().getIntPtrType(*getContext());
6803   Entry.Node = Size; Args.push_back(Entry);
6804   // FIXME: pass in SDLoc
6805   TargetLowering::CallLoweringInfo CLI(*this);
6806   CLI.setDebugLoc(dl)
6807       .setChain(Chain)
6808       .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
6809                     Dst.getValueType().getTypeForEVT(*getContext()),
6810                     getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
6811                                       TLI->getPointerTy(getDataLayout())),
6812                     std::move(Args))
6813       .setDiscardResult()
6814       .setTailCall(isTailCall);
6815 
6816   std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
6817   return CallResult.second;
6818 }
6819 
6820 SDValue SelectionDAG::getAtomicMemcpy(SDValue Chain, const SDLoc &dl,
6821                                       SDValue Dst, unsigned DstAlign,
6822                                       SDValue Src, unsigned SrcAlign,
6823                                       SDValue Size, Type *SizeTy,
6824                                       unsigned ElemSz, bool isTailCall,
6825                                       MachinePointerInfo DstPtrInfo,
6826                                       MachinePointerInfo SrcPtrInfo) {
6827   // Emit a library call.
6828   TargetLowering::ArgListTy Args;
6829   TargetLowering::ArgListEntry Entry;
6830   Entry.Ty = getDataLayout().getIntPtrType(*getContext());
6831   Entry.Node = Dst;
6832   Args.push_back(Entry);
6833 
6834   Entry.Node = Src;
6835   Args.push_back(Entry);
6836 
6837   Entry.Ty = SizeTy;
6838   Entry.Node = Size;
6839   Args.push_back(Entry);
6840 
6841   RTLIB::Libcall LibraryCall =
6842       RTLIB::getMEMCPY_ELEMENT_UNORDERED_ATOMIC(ElemSz);
6843   if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
6844     report_fatal_error("Unsupported element size");
6845 
6846   TargetLowering::CallLoweringInfo CLI(*this);
6847   CLI.setDebugLoc(dl)
6848       .setChain(Chain)
6849       .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
6850                     Type::getVoidTy(*getContext()),
6851                     getExternalSymbol(TLI->getLibcallName(LibraryCall),
6852                                       TLI->getPointerTy(getDataLayout())),
6853                     std::move(Args))
6854       .setDiscardResult()
6855       .setTailCall(isTailCall);
6856 
6857   std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
6858   return CallResult.second;
6859 }
6860 
6861 SDValue SelectionDAG::getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst,
6862                                  SDValue Src, SDValue Size, Align Alignment,
6863                                  bool isVol, bool isTailCall,
6864                                  MachinePointerInfo DstPtrInfo,
6865                                  MachinePointerInfo SrcPtrInfo,
6866                                  const AAMDNodes &AAInfo) {
6867   // Check to see if we should lower the memmove to loads and stores first.
6868   // For cases within the target-specified limits, this is the best choice.
6869   ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6870   if (ConstantSize) {
6871     // Memmove with size zero? Just return the original chain.
6872     if (ConstantSize->isNullValue())
6873       return Chain;
6874 
6875     SDValue Result = getMemmoveLoadsAndStores(
6876         *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
6877         isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
6878     if (Result.getNode())
6879       return Result;
6880   }
6881 
6882   // Then check to see if we should lower the memmove with target-specific
6883   // code. If the target chooses to do this, this is the next best.
6884   if (TSI) {
6885     SDValue Result =
6886         TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
6887                                       Alignment, isVol, DstPtrInfo, SrcPtrInfo);
6888     if (Result.getNode())
6889       return Result;
6890   }
6891 
6892   checkAddrSpaceIsValidForLibcall(TLI, DstPtrInfo.getAddrSpace());
6893   checkAddrSpaceIsValidForLibcall(TLI, SrcPtrInfo.getAddrSpace());
6894 
6895   // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
6896   // not be safe.  See memcpy above for more details.
6897 
6898   // Emit a library call.
6899   TargetLowering::ArgListTy Args;
6900   TargetLowering::ArgListEntry Entry;
6901   Entry.Ty = Type::getInt8PtrTy(*getContext());
6902   Entry.Node = Dst; Args.push_back(Entry);
6903   Entry.Node = Src; Args.push_back(Entry);
6904 
6905   Entry.Ty = getDataLayout().getIntPtrType(*getContext());
6906   Entry.Node = Size; Args.push_back(Entry);
6907   // FIXME:  pass in SDLoc
6908   TargetLowering::CallLoweringInfo CLI(*this);
6909   CLI.setDebugLoc(dl)
6910       .setChain(Chain)
6911       .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
6912                     Dst.getValueType().getTypeForEVT(*getContext()),
6913                     getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
6914                                       TLI->getPointerTy(getDataLayout())),
6915                     std::move(Args))
6916       .setDiscardResult()
6917       .setTailCall(isTailCall);
6918 
6919   std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
6920   return CallResult.second;
6921 }
6922 
6923 SDValue SelectionDAG::getAtomicMemmove(SDValue Chain, const SDLoc &dl,
6924                                        SDValue Dst, unsigned DstAlign,
6925                                        SDValue Src, unsigned SrcAlign,
6926                                        SDValue Size, Type *SizeTy,
6927                                        unsigned ElemSz, bool isTailCall,
6928                                        MachinePointerInfo DstPtrInfo,
6929                                        MachinePointerInfo SrcPtrInfo) {
6930   // Emit a library call.
6931   TargetLowering::ArgListTy Args;
6932   TargetLowering::ArgListEntry Entry;
6933   Entry.Ty = getDataLayout().getIntPtrType(*getContext());
6934   Entry.Node = Dst;
6935   Args.push_back(Entry);
6936 
6937   Entry.Node = Src;
6938   Args.push_back(Entry);
6939 
6940   Entry.Ty = SizeTy;
6941   Entry.Node = Size;
6942   Args.push_back(Entry);
6943 
6944   RTLIB::Libcall LibraryCall =
6945       RTLIB::getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(ElemSz);
6946   if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
6947     report_fatal_error("Unsupported element size");
6948 
6949   TargetLowering::CallLoweringInfo CLI(*this);
6950   CLI.setDebugLoc(dl)
6951       .setChain(Chain)
6952       .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
6953                     Type::getVoidTy(*getContext()),
6954                     getExternalSymbol(TLI->getLibcallName(LibraryCall),
6955                                       TLI->getPointerTy(getDataLayout())),
6956                     std::move(Args))
6957       .setDiscardResult()
6958       .setTailCall(isTailCall);
6959 
6960   std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
6961   return CallResult.second;
6962 }
6963 
6964 SDValue SelectionDAG::getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst,
6965                                 SDValue Src, SDValue Size, Align Alignment,
6966                                 bool isVol, bool isTailCall,
6967                                 MachinePointerInfo DstPtrInfo,
6968                                 const AAMDNodes &AAInfo) {
6969   // Check to see if we should lower the memset to stores first.
6970   // For cases within the target-specified limits, this is the best choice.
6971   ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6972   if (ConstantSize) {
6973     // Memset with size zero? Just return the original chain.
6974     if (ConstantSize->isNullValue())
6975       return Chain;
6976 
6977     SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
6978                                      ConstantSize->getZExtValue(), Alignment,
6979                                      isVol, DstPtrInfo, AAInfo);
6980 
6981     if (Result.getNode())
6982       return Result;
6983   }
6984 
6985   // Then check to see if we should lower the memset with target-specific
6986   // code. If the target chooses to do this, this is the next best.
6987   if (TSI) {
6988     SDValue Result = TSI->EmitTargetCodeForMemset(
6989         *this, dl, Chain, Dst, Src, Size, Alignment, isVol, DstPtrInfo);
6990     if (Result.getNode())
6991       return Result;
6992   }
6993 
6994   checkAddrSpaceIsValidForLibcall(TLI, DstPtrInfo.getAddrSpace());
6995 
6996   // Emit a library call.
6997   TargetLowering::ArgListTy Args;
6998   TargetLowering::ArgListEntry Entry;
6999   Entry.Node = Dst; Entry.Ty = Type::getInt8PtrTy(*getContext());
7000   Args.push_back(Entry);
7001   Entry.Node = Src;
7002   Entry.Ty = Src.getValueType().getTypeForEVT(*getContext());
7003   Args.push_back(Entry);
7004   Entry.Node = Size;
7005   Entry.Ty = getDataLayout().getIntPtrType(*getContext());
7006   Args.push_back(Entry);
7007 
7008   // FIXME: pass in SDLoc
7009   TargetLowering::CallLoweringInfo CLI(*this);
7010   CLI.setDebugLoc(dl)
7011       .setChain(Chain)
7012       .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
7013                     Dst.getValueType().getTypeForEVT(*getContext()),
7014                     getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
7015                                       TLI->getPointerTy(getDataLayout())),
7016                     std::move(Args))
7017       .setDiscardResult()
7018       .setTailCall(isTailCall);
7019 
7020   std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
7021   return CallResult.second;
7022 }
7023 
7024 SDValue SelectionDAG::getAtomicMemset(SDValue Chain, const SDLoc &dl,
7025                                       SDValue Dst, unsigned DstAlign,
7026                                       SDValue Value, SDValue Size, Type *SizeTy,
7027                                       unsigned ElemSz, bool isTailCall,
7028                                       MachinePointerInfo DstPtrInfo) {
7029   // Emit a library call.
7030   TargetLowering::ArgListTy Args;
7031   TargetLowering::ArgListEntry Entry;
7032   Entry.Ty = getDataLayout().getIntPtrType(*getContext());
7033   Entry.Node = Dst;
7034   Args.push_back(Entry);
7035 
7036   Entry.Ty = Type::getInt8Ty(*getContext());
7037   Entry.Node = Value;
7038   Args.push_back(Entry);
7039 
7040   Entry.Ty = SizeTy;
7041   Entry.Node = Size;
7042   Args.push_back(Entry);
7043 
7044   RTLIB::Libcall LibraryCall =
7045       RTLIB::getMEMSET_ELEMENT_UNORDERED_ATOMIC(ElemSz);
7046   if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
7047     report_fatal_error("Unsupported element size");
7048 
7049   TargetLowering::CallLoweringInfo CLI(*this);
7050   CLI.setDebugLoc(dl)
7051       .setChain(Chain)
7052       .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
7053                     Type::getVoidTy(*getContext()),
7054                     getExternalSymbol(TLI->getLibcallName(LibraryCall),
7055                                       TLI->getPointerTy(getDataLayout())),
7056                     std::move(Args))
7057       .setDiscardResult()
7058       .setTailCall(isTailCall);
7059 
7060   std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
7061   return CallResult.second;
7062 }
7063 
7064 SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
7065                                 SDVTList VTList, ArrayRef<SDValue> Ops,
7066                                 MachineMemOperand *MMO) {
7067   FoldingSetNodeID ID;
7068   ID.AddInteger(MemVT.getRawBits());
7069   AddNodeIDNode(ID, Opcode, VTList, Ops);
7070   ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
7071   void* IP = nullptr;
7072   if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
7073     cast<AtomicSDNode>(E)->refineAlignment(MMO);
7074     return SDValue(E, 0);
7075   }
7076 
7077   auto *N = newSDNode<AtomicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
7078                                     VTList, MemVT, MMO);
7079   createOperands(N, Ops);
7080 
7081   CSEMap.InsertNode(N, IP);
7082   InsertNode(N);
7083   return SDValue(N, 0);
7084 }
7085 
7086 SDValue SelectionDAG::getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl,
7087                                        EVT MemVT, SDVTList VTs, SDValue Chain,
7088                                        SDValue Ptr, SDValue Cmp, SDValue Swp,
7089                                        MachineMemOperand *MMO) {
7090   assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
7091          Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
7092   assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
7093 
7094   SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
7095   return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
7096 }
7097 
7098 SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
7099                                 SDValue Chain, SDValue Ptr, SDValue Val,
7100                                 MachineMemOperand *MMO) {
7101   assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
7102           Opcode == ISD::ATOMIC_LOAD_SUB ||
7103           Opcode == ISD::ATOMIC_LOAD_AND ||
7104           Opcode == ISD::ATOMIC_LOAD_CLR ||
7105           Opcode == ISD::ATOMIC_LOAD_OR ||
7106           Opcode == ISD::ATOMIC_LOAD_XOR ||
7107           Opcode == ISD::ATOMIC_LOAD_NAND ||
7108           Opcode == ISD::ATOMIC_LOAD_MIN ||
7109           Opcode == ISD::ATOMIC_LOAD_MAX ||
7110           Opcode == ISD::ATOMIC_LOAD_UMIN ||
7111           Opcode == ISD::ATOMIC_LOAD_UMAX ||
7112           Opcode == ISD::ATOMIC_LOAD_FADD ||
7113           Opcode == ISD::ATOMIC_LOAD_FSUB ||
7114           Opcode == ISD::ATOMIC_SWAP ||
7115           Opcode == ISD::ATOMIC_STORE) &&
7116          "Invalid Atomic Op");
7117 
7118   EVT VT = Val.getValueType();
7119 
7120   SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
7121                                                getVTList(VT, MVT::Other);
7122   SDValue Ops[] = {Chain, Ptr, Val};
7123   return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
7124 }
7125 
7126 SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
7127                                 EVT VT, SDValue Chain, SDValue Ptr,
7128                                 MachineMemOperand *MMO) {
7129   assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
7130 
7131   SDVTList VTs = getVTList(VT, MVT::Other);
7132   SDValue Ops[] = {Chain, Ptr};
7133   return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
7134 }
7135 
7136 /// getMergeValues - Create a MERGE_VALUES node from the given operands.
7137 SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, const SDLoc &dl) {
7138   if (Ops.size() == 1)
7139     return Ops[0];
7140 
7141   SmallVector<EVT, 4> VTs;
7142   VTs.reserve(Ops.size());
7143   for (const SDValue &Op : Ops)
7144     VTs.push_back(Op.getValueType());
7145   return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
7146 }
7147 
7148 SDValue SelectionDAG::getMemIntrinsicNode(
7149     unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
7150     EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
7151     MachineMemOperand::Flags Flags, uint64_t Size, const AAMDNodes &AAInfo) {
7152   if (!Size && MemVT.isScalableVector())
7153     Size = MemoryLocation::UnknownSize;
7154   else if (!Size)
7155     Size = MemVT.getStoreSize();
7156 
7157   MachineFunction &MF = getMachineFunction();
7158   MachineMemOperand *MMO =
7159       MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
7160 
7161   return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
7162 }
7163 
7164 SDValue SelectionDAG::getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl,
7165                                           SDVTList VTList,
7166                                           ArrayRef<SDValue> Ops, EVT MemVT,
7167                                           MachineMemOperand *MMO) {
7168   assert((Opcode == ISD::INTRINSIC_VOID ||
7169           Opcode == ISD::INTRINSIC_W_CHAIN ||
7170           Opcode == ISD::PREFETCH ||
7171           ((int)Opcode <= std::numeric_limits<int>::max() &&
7172            (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
7173          "Opcode is not a memory-accessing opcode!");
7174 
7175   // Memoize the node unless it returns a flag.
7176   MemIntrinsicSDNode *N;
7177   if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
7178     FoldingSetNodeID ID;
7179     AddNodeIDNode(ID, Opcode, VTList, Ops);
7180     ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
7181         Opcode, dl.getIROrder(), VTList, MemVT, MMO));
7182     ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
7183     void *IP = nullptr;
7184     if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
7185       cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
7186       return SDValue(E, 0);
7187     }
7188 
7189     N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
7190                                       VTList, MemVT, MMO);
7191     createOperands(N, Ops);
7192 
7193   CSEMap.InsertNode(N, IP);
7194   } else {
7195     N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
7196                                       VTList, MemVT, MMO);
7197     createOperands(N, Ops);
7198   }
7199   InsertNode(N);
7200   SDValue V(N, 0);
7201   NewSDValueDbgMsg(V, "Creating new node: ", this);
7202   return V;
7203 }
7204 
7205 SDValue SelectionDAG::getLifetimeNode(bool IsStart, const SDLoc &dl,
7206                                       SDValue Chain, int FrameIndex,
7207                                       int64_t Size, int64_t Offset) {
7208   const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
7209   const auto VTs = getVTList(MVT::Other);
7210   SDValue Ops[2] = {
7211       Chain,
7212       getFrameIndex(FrameIndex,
7213                     getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
7214                     true)};
7215 
7216   FoldingSetNodeID ID;
7217   AddNodeIDNode(ID, Opcode, VTs, Ops);
7218   ID.AddInteger(FrameIndex);
7219   ID.AddInteger(Size);
7220   ID.AddInteger(Offset);
7221   void *IP = nullptr;
7222   if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
7223     return SDValue(E, 0);
7224 
7225   LifetimeSDNode *N = newSDNode<LifetimeSDNode>(
7226       Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs, Size, Offset);
7227   createOperands(N, Ops);
7228   CSEMap.InsertNode(N, IP);
7229   InsertNode(N);
7230   SDValue V(N, 0);
7231   NewSDValueDbgMsg(V, "Creating new node: ", this);
7232   return V;
7233 }
7234 
7235 SDValue SelectionDAG::getPseudoProbeNode(const SDLoc &Dl, SDValue Chain,
7236                                          uint64_t Guid, uint64_t Index,
7237                                          uint32_t Attr) {
7238   const unsigned Opcode = ISD::PSEUDO_PROBE;
7239   const auto VTs = getVTList(MVT::Other);
7240   SDValue Ops[] = {Chain};
7241   FoldingSetNodeID ID;
7242   AddNodeIDNode(ID, Opcode, VTs, Ops);
7243   ID.AddInteger(Guid);
7244   ID.AddInteger(Index);
7245   void *IP = nullptr;
7246   if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
7247     return SDValue(E, 0);
7248 
7249   auto *N = newSDNode<PseudoProbeSDNode>(
7250       Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
7251   createOperands(N, Ops);
7252   CSEMap.InsertNode(N, IP);
7253   InsertNode(N);
7254   SDValue V(N, 0);
7255   NewSDValueDbgMsg(V, "Creating new node: ", this);
7256   return V;
7257 }
7258 
7259 /// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
7260 /// MachinePointerInfo record from it.  This is particularly useful because the
7261 /// code generator has many cases where it doesn't bother passing in a
7262 /// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
7263 static MachinePointerInfo InferPointerInfo(const MachinePointerInfo &Info,
7264                                            SelectionDAG &DAG, SDValue Ptr,
7265                                            int64_t Offset = 0) {
7266   // If this is FI+Offset, we can model it.
7267   if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
7268     return MachinePointerInfo::getFixedStack(DAG.getMachineFunction(),
7269                                              FI->getIndex(), Offset);
7270 
7271   // If this is (FI+Offset1)+Offset2, we can model it.
7272   if (Ptr.getOpcode() != ISD::ADD ||
7273       !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
7274       !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
7275     return Info;
7276 
7277   int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
7278   return MachinePointerInfo::getFixedStack(
7279       DAG.getMachineFunction(), FI,
7280       Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
7281 }
7282 
7283 /// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
7284 /// MachinePointerInfo record from it.  This is particularly useful because the
7285 /// code generator has many cases where it doesn't bother passing in a
7286 /// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
7287 static MachinePointerInfo InferPointerInfo(const MachinePointerInfo &Info,
7288                                            SelectionDAG &DAG, SDValue Ptr,
7289                                            SDValue OffsetOp) {
7290   // If the 'Offset' value isn't a constant, we can't handle this.
7291   if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
7292     return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
7293   if (OffsetOp.isUndef())
7294     return InferPointerInfo(Info, DAG, Ptr);
7295   return Info;
7296 }
7297 
7298 SDValue SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
7299                               EVT VT, const SDLoc &dl, SDValue Chain,
7300                               SDValue Ptr, SDValue Offset,
7301                               MachinePointerInfo PtrInfo, EVT MemVT,
7302                               Align Alignment,
7303                               MachineMemOperand::Flags MMOFlags,
7304                               const AAMDNodes &AAInfo, const MDNode *Ranges) {
7305   assert(Chain.getValueType() == MVT::Other &&
7306         "Invalid chain type");
7307 
7308   MMOFlags |= MachineMemOperand::MOLoad;
7309   assert((MMOFlags & MachineMemOperand::MOStore) == 0);
7310   // If we don't have a PtrInfo, infer the trivial frame index case to simplify
7311   // clients.
7312   if (PtrInfo.V.isNull())
7313     PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
7314 
7315   uint64_t Size = MemoryLocation::getSizeOrUnknown(MemVT.getStoreSize());
7316   MachineFunction &MF = getMachineFunction();
7317   MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
7318                                                    Alignment, AAInfo, Ranges);
7319   return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
7320 }
7321 
7322 SDValue SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
7323                               EVT VT, const SDLoc &dl, SDValue Chain,
7324                               SDValue Ptr, SDValue Offset, EVT MemVT,
7325                               MachineMemOperand *MMO) {
7326   if (VT == MemVT) {
7327     ExtType = ISD::NON_EXTLOAD;
7328   } else if (ExtType == ISD::NON_EXTLOAD) {
7329     assert(VT == MemVT && "Non-extending load from different memory type!");
7330   } else {
7331     // Extending load.
7332     assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
7333            "Should only be an extending load, not truncating!");
7334     assert(VT.isInteger() == MemVT.isInteger() &&
7335            "Cannot convert from FP to Int or Int -> FP!");
7336     assert(VT.isVector() == MemVT.isVector() &&
7337            "Cannot use an ext load to convert to or from a vector!");
7338     assert((!VT.isVector() ||
7339             VT.getVectorElementCount() == MemVT.getVectorElementCount()) &&
7340            "Cannot use an ext load to change the number of vector elements!");
7341   }
7342 
7343   bool Indexed = AM != ISD::UNINDEXED;
7344   assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
7345 
7346   SDVTList VTs = Indexed ?
7347     getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
7348   SDValue Ops[] = { Chain, Ptr, Offset };
7349   FoldingSetNodeID ID;
7350   AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
7351   ID.AddInteger(MemVT.getRawBits());
7352   ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
7353       dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
7354   ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
7355   void *IP = nullptr;
7356   if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
7357     cast<LoadSDNode>(E)->refineAlignment(MMO);
7358     return SDValue(E, 0);
7359   }
7360   auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
7361                                   ExtType, MemVT, MMO);
7362   createOperands(N, Ops);
7363 
7364   CSEMap.InsertNode(N, IP);
7365   InsertNode(N);
7366   SDValue V(N, 0);
7367   NewSDValueDbgMsg(V, "Creating new node: ", this);
7368   return V;
7369 }
7370 
7371 SDValue SelectionDAG::getLoad(EVT VT, const SDLoc &dl, SDValue Chain,
7372                               SDValue Ptr, MachinePointerInfo PtrInfo,
7373                               MaybeAlign Alignment,
7374                               MachineMemOperand::Flags MMOFlags,
7375                               const AAMDNodes &AAInfo, const MDNode *Ranges) {
7376   SDValue Undef = getUNDEF(Ptr.getValueType());
7377   return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
7378                  PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
7379 }
7380 
7381 SDValue SelectionDAG::getLoad(EVT VT, const SDLoc &dl, SDValue Chain,
7382                               SDValue Ptr, MachineMemOperand *MMO) {
7383   SDValue Undef = getUNDEF(Ptr.getValueType());
7384   return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
7385                  VT, MMO);
7386 }
7387 
7388 SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl,
7389                                  EVT VT, SDValue Chain, SDValue Ptr,
7390                                  MachinePointerInfo PtrInfo, EVT MemVT,
7391                                  MaybeAlign Alignment,
7392                                  MachineMemOperand::Flags MMOFlags,
7393                                  const AAMDNodes &AAInfo) {
7394   SDValue Undef = getUNDEF(Ptr.getValueType());
7395   return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
7396                  MemVT, Alignment, MMOFlags, AAInfo);
7397 }
7398 
7399 SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl,
7400                                  EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
7401                                  MachineMemOperand *MMO) {
7402   SDValue Undef = getUNDEF(Ptr.getValueType());
7403   return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
7404                  MemVT, MMO);
7405 }
7406 
7407 SDValue SelectionDAG::getIndexedLoad(SDValue OrigLoad, const SDLoc &dl,
7408                                      SDValue Base, SDValue Offset,
7409                                      ISD::MemIndexedMode AM) {
7410   LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
7411   assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
7412   // Don't propagate the invariant or dereferenceable flags.
7413   auto MMOFlags =
7414       LD->getMemOperand()->getFlags() &
7415       ~(MachineMemOperand::MOInvariant | MachineMemOperand::MODereferenceable);
7416   return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
7417                  LD->getChain(), Base, Offset, LD->getPointerInfo(),
7418                  LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
7419 }
7420 
7421 SDValue SelectionDAG::getStore(SDValue Chain, const SDLoc &dl, SDValue Val,
7422                                SDValue Ptr, MachinePointerInfo PtrInfo,
7423                                Align Alignment,
7424                                MachineMemOperand::Flags MMOFlags,
7425                                const AAMDNodes &AAInfo) {
7426   assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
7427 
7428   MMOFlags |= MachineMemOperand::MOStore;
7429   assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
7430 
7431   if (PtrInfo.V.isNull())
7432     PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
7433 
7434   MachineFunction &MF = getMachineFunction();
7435   uint64_t Size =
7436       MemoryLocation::getSizeOrUnknown(Val.getValueType().getStoreSize());
7437   MachineMemOperand *MMO =
7438       MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
7439   return getStore(Chain, dl, Val, Ptr, MMO);
7440 }
7441 
7442 SDValue SelectionDAG::getStore(SDValue Chain, const SDLoc &dl, SDValue Val,
7443                                SDValue Ptr, MachineMemOperand *MMO) {
7444   assert(Chain.getValueType() == MVT::Other &&
7445         "Invalid chain type");
7446   EVT VT = Val.getValueType();
7447   SDVTList VTs = getVTList(MVT::Other);
7448   SDValue Undef = getUNDEF(Ptr.getValueType());
7449   SDValue Ops[] = { Chain, Val, Ptr, Undef };
7450   FoldingSetNodeID ID;
7451   AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
7452   ID.AddInteger(VT.getRawBits());
7453   ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
7454       dl.getIROrder(), VTs, ISD::UNINDEXED, false, VT, MMO));
7455   ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
7456   void *IP = nullptr;
7457   if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
7458     cast<StoreSDNode>(E)->refineAlignment(MMO);
7459     return SDValue(E, 0);
7460   }
7461   auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
7462                                    ISD::UNINDEXED, false, VT, MMO);
7463   createOperands(N, Ops);
7464 
7465   CSEMap.InsertNode(N, IP);
7466   InsertNode(N);
7467   SDValue V(N, 0);
7468   NewSDValueDbgMsg(V, "Creating new node: ", this);
7469   return V;
7470 }
7471 
7472 SDValue SelectionDAG::getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val,
7473                                     SDValue Ptr, MachinePointerInfo PtrInfo,
7474                                     EVT SVT, Align Alignment,
7475                                     MachineMemOperand::Flags MMOFlags,
7476                                     const AAMDNodes &AAInfo) {
7477   assert(Chain.getValueType() == MVT::Other &&
7478         "Invalid chain type");
7479 
7480   MMOFlags |= MachineMemOperand::MOStore;
7481   assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
7482 
7483   if (PtrInfo.V.isNull())
7484     PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
7485 
7486   MachineFunction &MF = getMachineFunction();
7487   MachineMemOperand *MMO = MF.getMachineMemOperand(
7488       PtrInfo, MMOFlags, MemoryLocation::getSizeOrUnknown(SVT.getStoreSize()),
7489       Alignment, AAInfo);
7490   return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
7491 }
7492 
7493 SDValue SelectionDAG::getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val,
7494                                     SDValue Ptr, EVT SVT,
7495                                     MachineMemOperand *MMO) {
7496   EVT VT = Val.getValueType();
7497 
7498   assert(Chain.getValueType() == MVT::Other &&
7499         "Invalid chain type");
7500   if (VT == SVT)
7501     return getStore(Chain, dl, Val, Ptr, MMO);
7502 
7503   assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
7504          "Should only be a truncating store, not extending!");
7505   assert(VT.isInteger() == SVT.isInteger() &&
7506          "Can't do FP-INT conversion!");
7507   assert(VT.isVector() == SVT.isVector() &&
7508          "Cannot use trunc store to convert to or from a vector!");
7509   assert((!VT.isVector() ||
7510           VT.getVectorElementCount() == SVT.getVectorElementCount()) &&
7511          "Cannot use trunc store to change the number of vector elements!");
7512 
7513   SDVTList VTs = getVTList(MVT::Other);
7514   SDValue Undef = getUNDEF(Ptr.getValueType());
7515   SDValue Ops[] = { Chain, Val, Ptr, Undef };
7516   FoldingSetNodeID ID;
7517   AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
7518   ID.AddInteger(SVT.getRawBits());
7519   ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
7520       dl.getIROrder(), VTs, ISD::UNINDEXED, true, SVT, MMO));
7521   ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
7522   void *IP = nullptr;
7523   if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
7524     cast<StoreSDNode>(E)->refineAlignment(MMO);
7525     return SDValue(E, 0);
7526   }
7527   auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
7528                                    ISD::UNINDEXED, true, SVT, MMO);
7529   createOperands(N, Ops);
7530 
7531   CSEMap.InsertNode(N, IP);
7532   InsertNode(N);
7533   SDValue V(N, 0);
7534   NewSDValueDbgMsg(V, "Creating new node: ", this);
7535   return V;
7536 }
7537 
7538 SDValue SelectionDAG::getIndexedStore(SDValue OrigStore, const SDLoc &dl,
7539                                       SDValue Base, SDValue Offset,
7540                                       ISD::MemIndexedMode AM) {
7541   StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
7542   assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
7543   SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
7544   SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
7545   FoldingSetNodeID ID;
7546   AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
7547   ID.AddInteger(ST->getMemoryVT().getRawBits());
7548   ID.AddInteger(ST->getRawSubclassData());
7549   ID.AddInteger(ST->getPointerInfo().getAddrSpace());
7550   void *IP = nullptr;
7551   if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
7552     return SDValue(E, 0);
7553 
7554   auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
7555                                    ST->isTruncatingStore(), ST->getMemoryVT(),
7556                                    ST->getMemOperand());
7557   createOperands(N, Ops);
7558 
7559   CSEMap.InsertNode(N, IP);
7560   InsertNode(N);
7561   SDValue V(N, 0);
7562   NewSDValueDbgMsg(V, "Creating new node: ", this);
7563   return V;
7564 }
7565 
7566 SDValue SelectionDAG::getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain,
7567                                     SDValue Base, SDValue Offset, SDValue Mask,
7568                                     SDValue PassThru, EVT MemVT,
7569                                     MachineMemOperand *MMO,
7570                                     ISD::MemIndexedMode AM,
7571                                     ISD::LoadExtType ExtTy, bool isExpanding) {
7572   bool Indexed = AM != ISD::UNINDEXED;
7573   assert((Indexed || Offset.isUndef()) &&
7574          "Unindexed masked load with an offset!");
7575   SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
7576                          : getVTList(VT, MVT::Other);
7577   SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
7578   FoldingSetNodeID ID;
7579   AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
7580   ID.AddInteger(MemVT.getRawBits());
7581   ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
7582       dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
7583   ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
7584   void *IP = nullptr;
7585   if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
7586     cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
7587     return SDValue(E, 0);
7588   }
7589   auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
7590                                         AM, ExtTy, isExpanding, MemVT, MMO);
7591   createOperands(N, Ops);
7592 
7593   CSEMap.InsertNode(N, IP);
7594   InsertNode(N);
7595   SDValue V(N, 0);
7596   NewSDValueDbgMsg(V, "Creating new node: ", this);
7597   return V;
7598 }
7599 
7600 SDValue SelectionDAG::getIndexedMaskedLoad(SDValue OrigLoad, const SDLoc &dl,
7601                                            SDValue Base, SDValue Offset,
7602                                            ISD::MemIndexedMode AM) {
7603   MaskedLoadSDNode *LD = cast<MaskedLoadSDNode>(OrigLoad);
7604   assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
7605   return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
7606                        Offset, LD->getMask(), LD->getPassThru(),
7607                        LD->getMemoryVT(), LD->getMemOperand(), AM,
7608                        LD->getExtensionType(), LD->isExpandingLoad());
7609 }
7610 
7611 SDValue SelectionDAG::getMaskedStore(SDValue Chain, const SDLoc &dl,
7612                                      SDValue Val, SDValue Base, SDValue Offset,
7613                                      SDValue Mask, EVT MemVT,
7614                                      MachineMemOperand *MMO,
7615                                      ISD::MemIndexedMode AM, bool IsTruncating,
7616                                      bool IsCompressing) {
7617   assert(Chain.getValueType() == MVT::Other &&
7618         "Invalid chain type");
7619   bool Indexed = AM != ISD::UNINDEXED;
7620   assert((Indexed || Offset.isUndef()) &&
7621          "Unindexed masked store with an offset!");
7622   SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
7623                          : getVTList(MVT::Other);
7624   SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
7625   FoldingSetNodeID ID;
7626   AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
7627   ID.AddInteger(MemVT.getRawBits());
7628   ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
7629       dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
7630   ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
7631   void *IP = nullptr;
7632   if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
7633     cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
7634     return SDValue(E, 0);
7635   }
7636   auto *N =
7637       newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
7638                                    IsTruncating, IsCompressing, MemVT, MMO);
7639   createOperands(N, Ops);
7640 
7641   CSEMap.InsertNode(N, IP);
7642   InsertNode(N);
7643   SDValue V(N, 0);
7644   NewSDValueDbgMsg(V, "Creating new node: ", this);
7645   return V;
7646 }
7647 
7648 SDValue SelectionDAG::getIndexedMaskedStore(SDValue OrigStore, const SDLoc &dl,
7649                                             SDValue Base, SDValue Offset,
7650                                             ISD::MemIndexedMode AM) {
7651   MaskedStoreSDNode *ST = cast<MaskedStoreSDNode>(OrigStore);
7652   assert(ST->getOffset().isUndef() &&
7653          "Masked store is already a indexed store!");
7654   return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
7655                         ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
7656                         AM, ST->isTruncatingStore(), ST->isCompressingStore());
7657 }
7658 
7659 SDValue SelectionDAG::getMaskedGather(SDVTList VTs, EVT MemVT, const SDLoc &dl,
7660                                       ArrayRef<SDValue> Ops,
7661                                       MachineMemOperand *MMO,
7662                                       ISD::MemIndexType IndexType,
7663                                       ISD::LoadExtType ExtTy) {
7664   assert(Ops.size() == 6 && "Incompatible number of operands");
7665 
7666   FoldingSetNodeID ID;
7667   AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
7668   ID.AddInteger(MemVT.getRawBits());
7669   ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
7670       dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
7671   ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
7672   void *IP = nullptr;
7673   if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
7674     cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
7675     return SDValue(E, 0);
7676   }
7677 
7678   IndexType = TLI->getCanonicalIndexType(IndexType, MemVT, Ops[4]);
7679   auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
7680                                           VTs, MemVT, MMO, IndexType, ExtTy);
7681   createOperands(N, Ops);
7682 
7683   assert(N->getPassThru().getValueType() == N->getValueType(0) &&
7684          "Incompatible type of the PassThru value in MaskedGatherSDNode");
7685   assert(N->getMask().getValueType().getVectorElementCount() ==
7686              N->getValueType(0).getVectorElementCount() &&
7687          "Vector width mismatch between mask and data");
7688   assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
7689              N->getValueType(0).getVectorElementCount().isScalable() &&
7690          "Scalable flags of index and data do not match");
7691   assert(ElementCount::isKnownGE(
7692              N->getIndex().getValueType().getVectorElementCount(),
7693              N->getValueType(0).getVectorElementCount()) &&
7694          "Vector width mismatch between index and data");
7695   assert(isa<ConstantSDNode>(N->getScale()) &&
7696          cast<ConstantSDNode>(N->getScale())->getAPIntValue().isPowerOf2() &&
7697          "Scale should be a constant power of 2");
7698 
7699   CSEMap.InsertNode(N, IP);
7700   InsertNode(N);
7701   SDValue V(N, 0);
7702   NewSDValueDbgMsg(V, "Creating new node: ", this);
7703   return V;
7704 }
7705 
7706 SDValue SelectionDAG::getMaskedScatter(SDVTList VTs, EVT MemVT, const SDLoc &dl,
7707                                        ArrayRef<SDValue> Ops,
7708                                        MachineMemOperand *MMO,
7709                                        ISD::MemIndexType IndexType,
7710                                        bool IsTrunc) {
7711   assert(Ops.size() == 6 && "Incompatible number of operands");
7712 
7713   FoldingSetNodeID ID;
7714   AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
7715   ID.AddInteger(MemVT.getRawBits());
7716   ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
7717       dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
7718   ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
7719   void *IP = nullptr;
7720   if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
7721     cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
7722     return SDValue(E, 0);
7723   }
7724 
7725   IndexType = TLI->getCanonicalIndexType(IndexType, MemVT, Ops[4]);
7726   auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
7727                                            VTs, MemVT, MMO, IndexType, IsTrunc);
7728   createOperands(N, Ops);
7729 
7730   assert(N->getMask().getValueType().getVectorElementCount() ==
7731              N->getValue().getValueType().getVectorElementCount() &&
7732          "Vector width mismatch between mask and data");
7733   assert(
7734       N->getIndex().getValueType().getVectorElementCount().isScalable() ==
7735           N->getValue().getValueType().getVectorElementCount().isScalable() &&
7736       "Scalable flags of index and data do not match");
7737   assert(ElementCount::isKnownGE(
7738              N->getIndex().getValueType().getVectorElementCount(),
7739              N->getValue().getValueType().getVectorElementCount()) &&
7740          "Vector width mismatch between index and data");
7741   assert(isa<ConstantSDNode>(N->getScale()) &&
7742          cast<ConstantSDNode>(N->getScale())->getAPIntValue().isPowerOf2() &&
7743          "Scale should be a constant power of 2");
7744 
7745   CSEMap.InsertNode(N, IP);
7746   InsertNode(N);
7747   SDValue V(N, 0);
7748   NewSDValueDbgMsg(V, "Creating new node: ", this);
7749   return V;
7750 }
7751 
7752 SDValue SelectionDAG::simplifySelect(SDValue Cond, SDValue T, SDValue F) {
7753   // select undef, T, F --> T (if T is a constant), otherwise F
7754   // select, ?, undef, F --> F
7755   // select, ?, T, undef --> T
7756   if (Cond.isUndef())
7757     return isConstantValueOfAnyType(T) ? T : F;
7758   if (T.isUndef())
7759     return F;
7760   if (F.isUndef())
7761     return T;
7762 
7763   // select true, T, F --> T
7764   // select false, T, F --> F
7765   if (auto *CondC = dyn_cast<ConstantSDNode>(Cond))
7766     return CondC->isNullValue() ? F : T;
7767 
7768   // TODO: This should simplify VSELECT with constant condition using something
7769   // like this (but check boolean contents to be complete?):
7770   //  if (ISD::isBuildVectorAllOnes(Cond.getNode()))
7771   //    return T;
7772   //  if (ISD::isBuildVectorAllZeros(Cond.getNode()))
7773   //    return F;
7774 
7775   // select ?, T, T --> T
7776   if (T == F)
7777     return T;
7778 
7779   return SDValue();
7780 }
7781 
7782 SDValue SelectionDAG::simplifyShift(SDValue X, SDValue Y) {
7783   // shift undef, Y --> 0 (can always assume that the undef value is 0)
7784   if (X.isUndef())
7785     return getConstant(0, SDLoc(X.getNode()), X.getValueType());
7786   // shift X, undef --> undef (because it may shift by the bitwidth)
7787   if (Y.isUndef())
7788     return getUNDEF(X.getValueType());
7789 
7790   // shift 0, Y --> 0
7791   // shift X, 0 --> X
7792   if (isNullOrNullSplat(X) || isNullOrNullSplat(Y))
7793     return X;
7794 
7795   // shift X, C >= bitwidth(X) --> undef
7796   // All vector elements must be too big (or undef) to avoid partial undefs.
7797   auto isShiftTooBig = [X](ConstantSDNode *Val) {
7798     return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
7799   };
7800   if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
7801     return getUNDEF(X.getValueType());
7802 
7803   return SDValue();
7804 }
7805 
7806 SDValue SelectionDAG::simplifyFPBinop(unsigned Opcode, SDValue X, SDValue Y,
7807                                       SDNodeFlags Flags) {
7808   // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
7809   // (an undef operand can be chosen to be Nan/Inf), then the result of this
7810   // operation is poison. That result can be relaxed to undef.
7811   ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
7812   ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
7813   bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
7814                 (YC && YC->getValueAPF().isNaN());
7815   bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
7816                 (YC && YC->getValueAPF().isInfinity());
7817 
7818   if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
7819     return getUNDEF(X.getValueType());
7820 
7821   if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
7822     return getUNDEF(X.getValueType());
7823 
7824   if (!YC)
7825     return SDValue();
7826 
7827   // X + -0.0 --> X
7828   if (Opcode == ISD::FADD)
7829     if (YC->getValueAPF().isNegZero())
7830       return X;
7831 
7832   // X - +0.0 --> X
7833   if (Opcode == ISD::FSUB)
7834     if (YC->getValueAPF().isPosZero())
7835       return X;
7836 
7837   // X * 1.0 --> X
7838   // X / 1.0 --> X
7839   if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
7840     if (YC->getValueAPF().isExactlyValue(1.0))
7841       return X;
7842 
7843   // X * 0.0 --> 0.0
7844   if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
7845     if (YC->getValueAPF().isZero())
7846       return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
7847 
7848   return SDValue();
7849 }
7850 
7851 SDValue SelectionDAG::getVAArg(EVT VT, const SDLoc &dl, SDValue Chain,
7852                                SDValue Ptr, SDValue SV, unsigned Align) {
7853   SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
7854   return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
7855 }
7856 
7857 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7858                               ArrayRef<SDUse> Ops) {
7859   switch (Ops.size()) {
7860   case 0: return getNode(Opcode, DL, VT);
7861   case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
7862   case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
7863   case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
7864   default: break;
7865   }
7866 
7867   // Copy from an SDUse array into an SDValue array for use with
7868   // the regular getNode logic.
7869   SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
7870   return getNode(Opcode, DL, VT, NewOps);
7871 }
7872 
7873 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7874                               ArrayRef<SDValue> Ops) {
7875   SDNodeFlags Flags;
7876   if (Inserter)
7877     Flags = Inserter->getFlags();
7878   return getNode(Opcode, DL, VT, Ops, Flags);
7879 }
7880 
7881 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7882                               ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
7883   unsigned NumOps = Ops.size();
7884   switch (NumOps) {
7885   case 0: return getNode(Opcode, DL, VT);
7886   case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
7887   case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
7888   case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
7889   default: break;
7890   }
7891 
7892 #ifndef NDEBUG
7893   for (auto &Op : Ops)
7894     assert(Op.getOpcode() != ISD::DELETED_NODE &&
7895            "Operand is DELETED_NODE!");
7896 #endif
7897 
7898   switch (Opcode) {
7899   default: break;
7900   case ISD::BUILD_VECTOR:
7901     // Attempt to simplify BUILD_VECTOR.
7902     if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7903       return V;
7904     break;
7905   case ISD::CONCAT_VECTORS:
7906     if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7907       return V;
7908     break;
7909   case ISD::SELECT_CC:
7910     assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
7911     assert(Ops[0].getValueType() == Ops[1].getValueType() &&
7912            "LHS and RHS of condition must have same type!");
7913     assert(Ops[2].getValueType() == Ops[3].getValueType() &&
7914            "True and False arms of SelectCC must have same type!");
7915     assert(Ops[2].getValueType() == VT &&
7916            "select_cc node must be of same type as true and false value!");
7917     break;
7918   case ISD::BR_CC:
7919     assert(NumOps == 5 && "BR_CC takes 5 operands!");
7920     assert(Ops[2].getValueType() == Ops[3].getValueType() &&
7921            "LHS/RHS of comparison should match types!");
7922     break;
7923   }
7924 
7925   // Memoize nodes.
7926   SDNode *N;
7927   SDVTList VTs = getVTList(VT);
7928 
7929   if (VT != MVT::Glue) {
7930     FoldingSetNodeID ID;
7931     AddNodeIDNode(ID, Opcode, VTs, Ops);
7932     void *IP = nullptr;
7933 
7934     if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7935       return SDValue(E, 0);
7936 
7937     N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7938     createOperands(N, Ops);
7939 
7940     CSEMap.InsertNode(N, IP);
7941   } else {
7942     N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7943     createOperands(N, Ops);
7944   }
7945 
7946   N->setFlags(Flags);
7947   InsertNode(N);
7948   SDValue V(N, 0);
7949   NewSDValueDbgMsg(V, "Creating new node: ", this);
7950   return V;
7951 }
7952 
7953 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
7954                               ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
7955   return getNode(Opcode, DL, getVTList(ResultTys), Ops);
7956 }
7957 
7958 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
7959                               ArrayRef<SDValue> Ops) {
7960   SDNodeFlags Flags;
7961   if (Inserter)
7962     Flags = Inserter->getFlags();
7963   return getNode(Opcode, DL, VTList, Ops, Flags);
7964 }
7965 
7966 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
7967                               ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
7968   if (VTList.NumVTs == 1)
7969     return getNode(Opcode, DL, VTList.VTs[0], Ops);
7970 
7971 #ifndef NDEBUG
7972   for (auto &Op : Ops)
7973     assert(Op.getOpcode() != ISD::DELETED_NODE &&
7974            "Operand is DELETED_NODE!");
7975 #endif
7976 
7977   switch (Opcode) {
7978   case ISD::STRICT_FP_EXTEND:
7979     assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
7980            "Invalid STRICT_FP_EXTEND!");
7981     assert(VTList.VTs[0].isFloatingPoint() &&
7982            Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
7983     assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
7984            "STRICT_FP_EXTEND result type should be vector iff the operand "
7985            "type is vector!");
7986     assert((!VTList.VTs[0].isVector() ||
7987             VTList.VTs[0].getVectorNumElements() ==
7988             Ops[1].getValueType().getVectorNumElements()) &&
7989            "Vector element count mismatch!");
7990     assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
7991            "Invalid fpext node, dst <= src!");
7992     break;
7993   case ISD::STRICT_FP_ROUND:
7994     assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
7995     assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
7996            "STRICT_FP_ROUND result type should be vector iff the operand "
7997            "type is vector!");
7998     assert((!VTList.VTs[0].isVector() ||
7999             VTList.VTs[0].getVectorNumElements() ==
8000             Ops[1].getValueType().getVectorNumElements()) &&
8001            "Vector element count mismatch!");
8002     assert(VTList.VTs[0].isFloatingPoint() &&
8003            Ops[1].getValueType().isFloatingPoint() &&
8004            VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
8005            isa<ConstantSDNode>(Ops[2]) &&
8006            (cast<ConstantSDNode>(Ops[2])->getZExtValue() == 0 ||
8007             cast<ConstantSDNode>(Ops[2])->getZExtValue() == 1) &&
8008            "Invalid STRICT_FP_ROUND!");
8009     break;
8010 #if 0
8011   // FIXME: figure out how to safely handle things like
8012   // int foo(int x) { return 1 << (x & 255); }
8013   // int bar() { return foo(256); }
8014   case ISD::SRA_PARTS:
8015   case ISD::SRL_PARTS:
8016   case ISD::SHL_PARTS:
8017     if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
8018         cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
8019       return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
8020     else if (N3.getOpcode() == ISD::AND)
8021       if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
8022         // If the and is only masking out bits that cannot effect the shift,
8023         // eliminate the and.
8024         unsigned NumBits = VT.getScalarSizeInBits()*2;
8025         if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
8026           return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
8027       }
8028     break;
8029 #endif
8030   }
8031 
8032   // Memoize the node unless it returns a flag.
8033   SDNode *N;
8034   if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
8035     FoldingSetNodeID ID;
8036     AddNodeIDNode(ID, Opcode, VTList, Ops);
8037     void *IP = nullptr;
8038     if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
8039       return SDValue(E, 0);
8040 
8041     N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
8042     createOperands(N, Ops);
8043     CSEMap.InsertNode(N, IP);
8044   } else {
8045     N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
8046     createOperands(N, Ops);
8047   }
8048 
8049   N->setFlags(Flags);
8050   InsertNode(N);
8051   SDValue V(N, 0);
8052   NewSDValueDbgMsg(V, "Creating new node: ", this);
8053   return V;
8054 }
8055 
8056 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
8057                               SDVTList VTList) {
8058   return getNode(Opcode, DL, VTList, None);
8059 }
8060 
8061 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
8062                               SDValue N1) {
8063   SDValue Ops[] = { N1 };
8064   return getNode(Opcode, DL, VTList, Ops);
8065 }
8066 
8067 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
8068                               SDValue N1, SDValue N2) {
8069   SDValue Ops[] = { N1, N2 };
8070   return getNode(Opcode, DL, VTList, Ops);
8071 }
8072 
8073 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
8074                               SDValue N1, SDValue N2, SDValue N3) {
8075   SDValue Ops[] = { N1, N2, N3 };
8076   return getNode(Opcode, DL, VTList, Ops);
8077 }
8078 
8079 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
8080                               SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8081   SDValue Ops[] = { N1, N2, N3, N4 };
8082   return getNode(Opcode, DL, VTList, Ops);
8083 }
8084 
8085 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
8086                               SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8087                               SDValue N5) {
8088   SDValue Ops[] = { N1, N2, N3, N4, N5 };
8089   return getNode(Opcode, DL, VTList, Ops);
8090 }
8091 
8092 SDVTList SelectionDAG::getVTList(EVT VT) {
8093   return makeVTList(SDNode::getValueTypeList(VT), 1);
8094 }
8095 
8096 SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
8097   FoldingSetNodeID ID;
8098   ID.AddInteger(2U);
8099   ID.AddInteger(VT1.getRawBits());
8100   ID.AddInteger(VT2.getRawBits());
8101 
8102   void *IP = nullptr;
8103   SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
8104   if (!Result) {
8105     EVT *Array = Allocator.Allocate<EVT>(2);
8106     Array[0] = VT1;
8107     Array[1] = VT2;
8108     Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
8109     VTListMap.InsertNode(Result, IP);
8110   }
8111   return Result->getSDVTList();
8112 }
8113 
8114 SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
8115   FoldingSetNodeID ID;
8116   ID.AddInteger(3U);
8117   ID.AddInteger(VT1.getRawBits());
8118   ID.AddInteger(VT2.getRawBits());
8119   ID.AddInteger(VT3.getRawBits());
8120 
8121   void *IP = nullptr;
8122   SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
8123   if (!Result) {
8124     EVT *Array = Allocator.Allocate<EVT>(3);
8125     Array[0] = VT1;
8126     Array[1] = VT2;
8127     Array[2] = VT3;
8128     Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
8129     VTListMap.InsertNode(Result, IP);
8130   }
8131   return Result->getSDVTList();
8132 }
8133 
8134 SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
8135   FoldingSetNodeID ID;
8136   ID.AddInteger(4U);
8137   ID.AddInteger(VT1.getRawBits());
8138   ID.AddInteger(VT2.getRawBits());
8139   ID.AddInteger(VT3.getRawBits());
8140   ID.AddInteger(VT4.getRawBits());
8141 
8142   void *IP = nullptr;
8143   SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
8144   if (!Result) {
8145     EVT *Array = Allocator.Allocate<EVT>(4);
8146     Array[0] = VT1;
8147     Array[1] = VT2;
8148     Array[2] = VT3;
8149     Array[3] = VT4;
8150     Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
8151     VTListMap.InsertNode(Result, IP);
8152   }
8153   return Result->getSDVTList();
8154 }
8155 
8156 SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
8157   unsigned NumVTs = VTs.size();
8158   FoldingSetNodeID ID;
8159   ID.AddInteger(NumVTs);
8160   for (unsigned index = 0; index < NumVTs; index++) {
8161     ID.AddInteger(VTs[index].getRawBits());
8162   }
8163 
8164   void *IP = nullptr;
8165   SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
8166   if (!Result) {
8167     EVT *Array = Allocator.Allocate<EVT>(NumVTs);
8168     llvm::copy(VTs, Array);
8169     Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
8170     VTListMap.InsertNode(Result, IP);
8171   }
8172   return Result->getSDVTList();
8173 }
8174 
8175 
8176 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
8177 /// specified operands.  If the resultant node already exists in the DAG,
8178 /// this does not modify the specified node, instead it returns the node that
8179 /// already exists.  If the resultant node does not exist in the DAG, the
8180 /// input node is returned.  As a degenerate case, if you specify the same
8181 /// input operands as the node already has, the input node is returned.
8182 SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
8183   assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
8184 
8185   // Check to see if there is no change.
8186   if (Op == N->getOperand(0)) return N;
8187 
8188   // See if the modified node already exists.
8189   void *InsertPos = nullptr;
8190   if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
8191     return Existing;
8192 
8193   // Nope it doesn't.  Remove the node from its current place in the maps.
8194   if (InsertPos)
8195     if (!RemoveNodeFromCSEMaps(N))
8196       InsertPos = nullptr;
8197 
8198   // Now we update the operands.
8199   N->OperandList[0].set(Op);
8200 
8201   updateDivergence(N);
8202   // If this gets put into a CSE map, add it.
8203   if (InsertPos) CSEMap.InsertNode(N, InsertPos);
8204   return N;
8205 }
8206 
8207 SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
8208   assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
8209 
8210   // Check to see if there is no change.
8211   if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
8212     return N;   // No operands changed, just return the input node.
8213 
8214   // See if the modified node already exists.
8215   void *InsertPos = nullptr;
8216   if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
8217     return Existing;
8218 
8219   // Nope it doesn't.  Remove the node from its current place in the maps.
8220   if (InsertPos)
8221     if (!RemoveNodeFromCSEMaps(N))
8222       InsertPos = nullptr;
8223 
8224   // Now we update the operands.
8225   if (N->OperandList[0] != Op1)
8226     N->OperandList[0].set(Op1);
8227   if (N->OperandList[1] != Op2)
8228     N->OperandList[1].set(Op2);
8229 
8230   updateDivergence(N);
8231   // If this gets put into a CSE map, add it.
8232   if (InsertPos) CSEMap.InsertNode(N, InsertPos);
8233   return N;
8234 }
8235 
8236 SDNode *SelectionDAG::
8237 UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
8238   SDValue Ops[] = { Op1, Op2, Op3 };
8239   return UpdateNodeOperands(N, Ops);
8240 }
8241 
8242 SDNode *SelectionDAG::
8243 UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
8244                    SDValue Op3, SDValue Op4) {
8245   SDValue Ops[] = { Op1, Op2, Op3, Op4 };
8246   return UpdateNodeOperands(N, Ops);
8247 }
8248 
8249 SDNode *SelectionDAG::
8250 UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
8251                    SDValue Op3, SDValue Op4, SDValue Op5) {
8252   SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
8253   return UpdateNodeOperands(N, Ops);
8254 }
8255 
8256 SDNode *SelectionDAG::
8257 UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
8258   unsigned NumOps = Ops.size();
8259   assert(N->getNumOperands() == NumOps &&
8260          "Update with wrong number of operands");
8261 
8262   // If no operands changed just return the input node.
8263   if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
8264     return N;
8265 
8266   // See if the modified node already exists.
8267   void *InsertPos = nullptr;
8268   if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
8269     return Existing;
8270 
8271   // Nope it doesn't.  Remove the node from its current place in the maps.
8272   if (InsertPos)
8273     if (!RemoveNodeFromCSEMaps(N))
8274       InsertPos = nullptr;
8275 
8276   // Now we update the operands.
8277   for (unsigned i = 0; i != NumOps; ++i)
8278     if (N->OperandList[i] != Ops[i])
8279       N->OperandList[i].set(Ops[i]);
8280 
8281   updateDivergence(N);
8282   // If this gets put into a CSE map, add it.
8283   if (InsertPos) CSEMap.InsertNode(N, InsertPos);
8284   return N;
8285 }
8286 
8287 /// DropOperands - Release the operands and set this node to have
8288 /// zero operands.
8289 void SDNode::DropOperands() {
8290   // Unlike the code in MorphNodeTo that does this, we don't need to
8291   // watch for dead nodes here.
8292   for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
8293     SDUse &Use = *I++;
8294     Use.set(SDValue());
8295   }
8296 }
8297 
8298 void SelectionDAG::setNodeMemRefs(MachineSDNode *N,
8299                                   ArrayRef<MachineMemOperand *> NewMemRefs) {
8300   if (NewMemRefs.empty()) {
8301     N->clearMemRefs();
8302     return;
8303   }
8304 
8305   // Check if we can avoid allocating by storing a single reference directly.
8306   if (NewMemRefs.size() == 1) {
8307     N->MemRefs = NewMemRefs[0];
8308     N->NumMemRefs = 1;
8309     return;
8310   }
8311 
8312   MachineMemOperand **MemRefsBuffer =
8313       Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
8314   llvm::copy(NewMemRefs, MemRefsBuffer);
8315   N->MemRefs = MemRefsBuffer;
8316   N->NumMemRefs = static_cast<int>(NewMemRefs.size());
8317 }
8318 
8319 /// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
8320 /// machine opcode.
8321 ///
8322 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
8323                                    EVT VT) {
8324   SDVTList VTs = getVTList(VT);
8325   return SelectNodeTo(N, MachineOpc, VTs, None);
8326 }
8327 
8328 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
8329                                    EVT VT, SDValue Op1) {
8330   SDVTList VTs = getVTList(VT);
8331   SDValue Ops[] = { Op1 };
8332   return SelectNodeTo(N, MachineOpc, VTs, Ops);
8333 }
8334 
8335 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
8336                                    EVT VT, SDValue Op1,
8337                                    SDValue Op2) {
8338   SDVTList VTs = getVTList(VT);
8339   SDValue Ops[] = { Op1, Op2 };
8340   return SelectNodeTo(N, MachineOpc, VTs, Ops);
8341 }
8342 
8343 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
8344                                    EVT VT, SDValue Op1,
8345                                    SDValue Op2, SDValue Op3) {
8346   SDVTList VTs = getVTList(VT);
8347   SDValue Ops[] = { Op1, Op2, Op3 };
8348   return SelectNodeTo(N, MachineOpc, VTs, Ops);
8349 }
8350 
8351 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
8352                                    EVT VT, ArrayRef<SDValue> Ops) {
8353   SDVTList VTs = getVTList(VT);
8354   return SelectNodeTo(N, MachineOpc, VTs, Ops);
8355 }
8356 
8357 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
8358                                    EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
8359   SDVTList VTs = getVTList(VT1, VT2);
8360   return SelectNodeTo(N, MachineOpc, VTs, Ops);
8361 }
8362 
8363 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
8364                                    EVT VT1, EVT VT2) {
8365   SDVTList VTs = getVTList(VT1, VT2);
8366   return SelectNodeTo(N, MachineOpc, VTs, None);
8367 }
8368 
8369 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
8370                                    EVT VT1, EVT VT2, EVT VT3,
8371                                    ArrayRef<SDValue> Ops) {
8372   SDVTList VTs = getVTList(VT1, VT2, VT3);
8373   return SelectNodeTo(N, MachineOpc, VTs, Ops);
8374 }
8375 
8376 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
8377                                    EVT VT1, EVT VT2,
8378                                    SDValue Op1, SDValue Op2) {
8379   SDVTList VTs = getVTList(VT1, VT2);
8380   SDValue Ops[] = { Op1, Op2 };
8381   return SelectNodeTo(N, MachineOpc, VTs, Ops);
8382 }
8383 
8384 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
8385                                    SDVTList VTs,ArrayRef<SDValue> Ops) {
8386   SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
8387   // Reset the NodeID to -1.
8388   New->setNodeId(-1);
8389   if (New != N) {
8390     ReplaceAllUsesWith(N, New);
8391     RemoveDeadNode(N);
8392   }
8393   return New;
8394 }
8395 
8396 /// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
8397 /// the line number information on the merged node since it is not possible to
8398 /// preserve the information that operation is associated with multiple lines.
8399 /// This will make the debugger working better at -O0, were there is a higher
8400 /// probability having other instructions associated with that line.
8401 ///
8402 /// For IROrder, we keep the smaller of the two
8403 SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
8404   DebugLoc NLoc = N->getDebugLoc();
8405   if (NLoc && OptLevel == CodeGenOpt::None && OLoc.getDebugLoc() != NLoc) {
8406     N->setDebugLoc(DebugLoc());
8407   }
8408   unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
8409   N->setIROrder(Order);
8410   return N;
8411 }
8412 
8413 /// MorphNodeTo - This *mutates* the specified node to have the specified
8414 /// return type, opcode, and operands.
8415 ///
8416 /// Note that MorphNodeTo returns the resultant node.  If there is already a
8417 /// node of the specified opcode and operands, it returns that node instead of
8418 /// the current one.  Note that the SDLoc need not be the same.
8419 ///
8420 /// Using MorphNodeTo is faster than creating a new node and swapping it in
8421 /// with ReplaceAllUsesWith both because it often avoids allocating a new
8422 /// node, and because it doesn't require CSE recalculation for any of
8423 /// the node's users.
8424 ///
8425 /// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
8426 /// As a consequence it isn't appropriate to use from within the DAG combiner or
8427 /// the legalizer which maintain worklists that would need to be updated when
8428 /// deleting things.
8429 SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
8430                                   SDVTList VTs, ArrayRef<SDValue> Ops) {
8431   // If an identical node already exists, use it.
8432   void *IP = nullptr;
8433   if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
8434     FoldingSetNodeID ID;
8435     AddNodeIDNode(ID, Opc, VTs, Ops);
8436     if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
8437       return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
8438   }
8439 
8440   if (!RemoveNodeFromCSEMaps(N))
8441     IP = nullptr;
8442 
8443   // Start the morphing.
8444   N->NodeType = Opc;
8445   N->ValueList = VTs.VTs;
8446   N->NumValues = VTs.NumVTs;
8447 
8448   // Clear the operands list, updating used nodes to remove this from their
8449   // use list.  Keep track of any operands that become dead as a result.
8450   SmallPtrSet<SDNode*, 16> DeadNodeSet;
8451   for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
8452     SDUse &Use = *I++;
8453     SDNode *Used = Use.getNode();
8454     Use.set(SDValue());
8455     if (Used->use_empty())
8456       DeadNodeSet.insert(Used);
8457   }
8458 
8459   // For MachineNode, initialize the memory references information.
8460   if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N))
8461     MN->clearMemRefs();
8462 
8463   // Swap for an appropriately sized array from the recycler.
8464   removeOperands(N);
8465   createOperands(N, Ops);
8466 
8467   // Delete any nodes that are still dead after adding the uses for the
8468   // new operands.
8469   if (!DeadNodeSet.empty()) {
8470     SmallVector<SDNode *, 16> DeadNodes;
8471     for (SDNode *N : DeadNodeSet)
8472       if (N->use_empty())
8473         DeadNodes.push_back(N);
8474     RemoveDeadNodes(DeadNodes);
8475   }
8476 
8477   if (IP)
8478     CSEMap.InsertNode(N, IP);   // Memoize the new node.
8479   return N;
8480 }
8481 
8482 SDNode* SelectionDAG::mutateStrictFPToFP(SDNode *Node) {
8483   unsigned OrigOpc = Node->getOpcode();
8484   unsigned NewOpc;
8485   switch (OrigOpc) {
8486   default:
8487     llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
8488 #define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN)               \
8489   case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
8490 #define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN)               \
8491   case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
8492 #include "llvm/IR/ConstrainedOps.def"
8493   }
8494 
8495   assert(Node->getNumValues() == 2 && "Unexpected number of results!");
8496 
8497   // We're taking this node out of the chain, so we need to re-link things.
8498   SDValue InputChain = Node->getOperand(0);
8499   SDValue OutputChain = SDValue(Node, 1);
8500   ReplaceAllUsesOfValueWith(OutputChain, InputChain);
8501 
8502   SmallVector<SDValue, 3> Ops;
8503   for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
8504     Ops.push_back(Node->getOperand(i));
8505 
8506   SDVTList VTs = getVTList(Node->getValueType(0));
8507   SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
8508 
8509   // MorphNodeTo can operate in two ways: if an existing node with the
8510   // specified operands exists, it can just return it.  Otherwise, it
8511   // updates the node in place to have the requested operands.
8512   if (Res == Node) {
8513     // If we updated the node in place, reset the node ID.  To the isel,
8514     // this should be just like a newly allocated machine node.
8515     Res->setNodeId(-1);
8516   } else {
8517     ReplaceAllUsesWith(Node, Res);
8518     RemoveDeadNode(Node);
8519   }
8520 
8521   return Res;
8522 }
8523 
8524 /// getMachineNode - These are used for target selectors to create a new node
8525 /// with specified return type(s), MachineInstr opcode, and operands.
8526 ///
8527 /// Note that getMachineNode returns the resultant node.  If there is already a
8528 /// node of the specified opcode and operands, it returns that node instead of
8529 /// the current one.
8530 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
8531                                             EVT VT) {
8532   SDVTList VTs = getVTList(VT);
8533   return getMachineNode(Opcode, dl, VTs, None);
8534 }
8535 
8536 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
8537                                             EVT VT, SDValue Op1) {
8538   SDVTList VTs = getVTList(VT);
8539   SDValue Ops[] = { Op1 };
8540   return getMachineNode(Opcode, dl, VTs, Ops);
8541 }
8542 
8543 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
8544                                             EVT VT, SDValue Op1, SDValue Op2) {
8545   SDVTList VTs = getVTList(VT);
8546   SDValue Ops[] = { Op1, Op2 };
8547   return getMachineNode(Opcode, dl, VTs, Ops);
8548 }
8549 
8550 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
8551                                             EVT VT, SDValue Op1, SDValue Op2,
8552                                             SDValue Op3) {
8553   SDVTList VTs = getVTList(VT);
8554   SDValue Ops[] = { Op1, Op2, Op3 };
8555   return getMachineNode(Opcode, dl, VTs, Ops);
8556 }
8557 
8558 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
8559                                             EVT VT, ArrayRef<SDValue> Ops) {
8560   SDVTList VTs = getVTList(VT);
8561   return getMachineNode(Opcode, dl, VTs, Ops);
8562 }
8563 
8564 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
8565                                             EVT VT1, EVT VT2, SDValue Op1,
8566                                             SDValue Op2) {
8567   SDVTList VTs = getVTList(VT1, VT2);
8568   SDValue Ops[] = { Op1, Op2 };
8569   return getMachineNode(Opcode, dl, VTs, Ops);
8570 }
8571 
8572 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
8573                                             EVT VT1, EVT VT2, SDValue Op1,
8574                                             SDValue Op2, SDValue Op3) {
8575   SDVTList VTs = getVTList(VT1, VT2);
8576   SDValue Ops[] = { Op1, Op2, Op3 };
8577   return getMachineNode(Opcode, dl, VTs, Ops);
8578 }
8579 
8580 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
8581                                             EVT VT1, EVT VT2,
8582                                             ArrayRef<SDValue> Ops) {
8583   SDVTList VTs = getVTList(VT1, VT2);
8584   return getMachineNode(Opcode, dl, VTs, Ops);
8585 }
8586 
8587 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
8588                                             EVT VT1, EVT VT2, EVT VT3,
8589                                             SDValue Op1, SDValue Op2) {
8590   SDVTList VTs = getVTList(VT1, VT2, VT3);
8591   SDValue Ops[] = { Op1, Op2 };
8592   return getMachineNode(Opcode, dl, VTs, Ops);
8593 }
8594 
8595 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
8596                                             EVT VT1, EVT VT2, EVT VT3,
8597                                             SDValue Op1, SDValue Op2,
8598                                             SDValue Op3) {
8599   SDVTList VTs = getVTList(VT1, VT2, VT3);
8600   SDValue Ops[] = { Op1, Op2, Op3 };
8601   return getMachineNode(Opcode, dl, VTs, Ops);
8602 }
8603 
8604 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
8605                                             EVT VT1, EVT VT2, EVT VT3,
8606                                             ArrayRef<SDValue> Ops) {
8607   SDVTList VTs = getVTList(VT1, VT2, VT3);
8608   return getMachineNode(Opcode, dl, VTs, Ops);
8609 }
8610 
8611 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
8612                                             ArrayRef<EVT> ResultTys,
8613                                             ArrayRef<SDValue> Ops) {
8614   SDVTList VTs = getVTList(ResultTys);
8615   return getMachineNode(Opcode, dl, VTs, Ops);
8616 }
8617 
8618 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &DL,
8619                                             SDVTList VTs,
8620                                             ArrayRef<SDValue> Ops) {
8621   bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
8622   MachineSDNode *N;
8623   void *IP = nullptr;
8624 
8625   if (DoCSE) {
8626     FoldingSetNodeID ID;
8627     AddNodeIDNode(ID, ~Opcode, VTs, Ops);
8628     IP = nullptr;
8629     if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8630       return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
8631     }
8632   }
8633 
8634   // Allocate a new MachineSDNode.
8635   N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8636   createOperands(N, Ops);
8637 
8638   if (DoCSE)
8639     CSEMap.InsertNode(N, IP);
8640 
8641   InsertNode(N);
8642   NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
8643   return N;
8644 }
8645 
8646 /// getTargetExtractSubreg - A convenience function for creating
8647 /// TargetOpcode::EXTRACT_SUBREG nodes.
8648 SDValue SelectionDAG::getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT,
8649                                              SDValue Operand) {
8650   SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
8651   SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
8652                                   VT, Operand, SRIdxVal);
8653   return SDValue(Subreg, 0);
8654 }
8655 
8656 /// getTargetInsertSubreg - A convenience function for creating
8657 /// TargetOpcode::INSERT_SUBREG nodes.
8658 SDValue SelectionDAG::getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT,
8659                                             SDValue Operand, SDValue Subreg) {
8660   SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
8661   SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
8662                                   VT, Operand, Subreg, SRIdxVal);
8663   return SDValue(Result, 0);
8664 }
8665 
8666 /// getNodeIfExists - Get the specified node if it's already available, or
8667 /// else return NULL.
8668 SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
8669                                       ArrayRef<SDValue> Ops) {
8670   SDNodeFlags Flags;
8671   if (Inserter)
8672     Flags = Inserter->getFlags();
8673   return getNodeIfExists(Opcode, VTList, Ops, Flags);
8674 }
8675 
8676 SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
8677                                       ArrayRef<SDValue> Ops,
8678                                       const SDNodeFlags Flags) {
8679   if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
8680     FoldingSetNodeID ID;
8681     AddNodeIDNode(ID, Opcode, VTList, Ops);
8682     void *IP = nullptr;
8683     if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP)) {
8684       E->intersectFlagsWith(Flags);
8685       return E;
8686     }
8687   }
8688   return nullptr;
8689 }
8690 
8691 /// doesNodeExist - Check if a node exists without modifying its flags.
8692 bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
8693                                  ArrayRef<SDValue> Ops) {
8694   if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
8695     FoldingSetNodeID ID;
8696     AddNodeIDNode(ID, Opcode, VTList, Ops);
8697     void *IP = nullptr;
8698     if (FindNodeOrInsertPos(ID, SDLoc(), IP))
8699       return true;
8700   }
8701   return false;
8702 }
8703 
8704 /// getDbgValue - Creates a SDDbgValue node.
8705 ///
8706 /// SDNode
8707 SDDbgValue *SelectionDAG::getDbgValue(DIVariable *Var, DIExpression *Expr,
8708                                       SDNode *N, unsigned R, bool IsIndirect,
8709                                       const DebugLoc &DL, unsigned O) {
8710   assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
8711          "Expected inlined-at fields to agree");
8712   return new (DbgInfo->getAlloc())
8713       SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
8714                  {}, IsIndirect, DL, O,
8715                  /*IsVariadic=*/false);
8716 }
8717 
8718 /// Constant
8719 SDDbgValue *SelectionDAG::getConstantDbgValue(DIVariable *Var,
8720                                               DIExpression *Expr,
8721                                               const Value *C,
8722                                               const DebugLoc &DL, unsigned O) {
8723   assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
8724          "Expected inlined-at fields to agree");
8725   return new (DbgInfo->getAlloc())
8726       SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
8727                  /*IsIndirect=*/false, DL, O,
8728                  /*IsVariadic=*/false);
8729 }
8730 
8731 /// FrameIndex
8732 SDDbgValue *SelectionDAG::getFrameIndexDbgValue(DIVariable *Var,
8733                                                 DIExpression *Expr, unsigned FI,
8734                                                 bool IsIndirect,
8735                                                 const DebugLoc &DL,
8736                                                 unsigned O) {
8737   assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
8738          "Expected inlined-at fields to agree");
8739   return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
8740 }
8741 
8742 /// FrameIndex with dependencies
8743 SDDbgValue *SelectionDAG::getFrameIndexDbgValue(DIVariable *Var,
8744                                                 DIExpression *Expr, unsigned FI,
8745                                                 ArrayRef<SDNode *> Dependencies,
8746                                                 bool IsIndirect,
8747                                                 const DebugLoc &DL,
8748                                                 unsigned O) {
8749   assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
8750          "Expected inlined-at fields to agree");
8751   return new (DbgInfo->getAlloc())
8752       SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
8753                  Dependencies, IsIndirect, DL, O,
8754                  /*IsVariadic=*/false);
8755 }
8756 
8757 /// VReg
8758 SDDbgValue *SelectionDAG::getVRegDbgValue(DIVariable *Var, DIExpression *Expr,
8759                                           unsigned VReg, bool IsIndirect,
8760                                           const DebugLoc &DL, unsigned O) {
8761   assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
8762          "Expected inlined-at fields to agree");
8763   return new (DbgInfo->getAlloc())
8764       SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
8765                  {}, IsIndirect, DL, O,
8766                  /*IsVariadic=*/false);
8767 }
8768 
8769 SDDbgValue *SelectionDAG::getDbgValueList(DIVariable *Var, DIExpression *Expr,
8770                                           ArrayRef<SDDbgOperand> Locs,
8771                                           ArrayRef<SDNode *> Dependencies,
8772                                           bool IsIndirect, const DebugLoc &DL,
8773                                           unsigned O, bool IsVariadic) {
8774   assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
8775          "Expected inlined-at fields to agree");
8776   return new (DbgInfo->getAlloc())
8777       SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
8778                  DL, O, IsVariadic);
8779 }
8780 
8781 void SelectionDAG::transferDbgValues(SDValue From, SDValue To,
8782                                      unsigned OffsetInBits, unsigned SizeInBits,
8783                                      bool InvalidateDbg) {
8784   SDNode *FromNode = From.getNode();
8785   SDNode *ToNode = To.getNode();
8786   assert(FromNode && ToNode && "Can't modify dbg values");
8787 
8788   // PR35338
8789   // TODO: assert(From != To && "Redundant dbg value transfer");
8790   // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
8791   if (From == To || FromNode == ToNode)
8792     return;
8793 
8794   if (!FromNode->getHasDebugValue())
8795     return;
8796 
8797   SDDbgOperand FromLocOp =
8798       SDDbgOperand::fromNode(From.getNode(), From.getResNo());
8799   SDDbgOperand ToLocOp = SDDbgOperand::fromNode(To.getNode(), To.getResNo());
8800 
8801   SmallVector<SDDbgValue *, 2> ClonedDVs;
8802   for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
8803     if (Dbg->isInvalidated())
8804       continue;
8805 
8806     // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
8807 
8808     // Create a new location ops vector that is equal to the old vector, but
8809     // with each instance of FromLocOp replaced with ToLocOp.
8810     bool Changed = false;
8811     auto NewLocOps = Dbg->copyLocationOps();
8812     std::replace_if(
8813         NewLocOps.begin(), NewLocOps.end(),
8814         [&Changed, FromLocOp](const SDDbgOperand &Op) {
8815           bool Match = Op == FromLocOp;
8816           Changed |= Match;
8817           return Match;
8818         },
8819         ToLocOp);
8820     // Ignore this SDDbgValue if we didn't find a matching location.
8821     if (!Changed)
8822       continue;
8823 
8824     DIVariable *Var = Dbg->getVariable();
8825     auto *Expr = Dbg->getExpression();
8826     // If a fragment is requested, update the expression.
8827     if (SizeInBits) {
8828       // When splitting a larger (e.g., sign-extended) value whose
8829       // lower bits are described with an SDDbgValue, do not attempt
8830       // to transfer the SDDbgValue to the upper bits.
8831       if (auto FI = Expr->getFragmentInfo())
8832         if (OffsetInBits + SizeInBits > FI->SizeInBits)
8833           continue;
8834       auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
8835                                                              SizeInBits);
8836       if (!Fragment)
8837         continue;
8838       Expr = *Fragment;
8839     }
8840 
8841     auto AdditionalDependencies = Dbg->getAdditionalDependencies();
8842     // Clone the SDDbgValue and move it to To.
8843     SDDbgValue *Clone = getDbgValueList(
8844         Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
8845         Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
8846         Dbg->isVariadic());
8847     ClonedDVs.push_back(Clone);
8848 
8849     if (InvalidateDbg) {
8850       // Invalidate value and indicate the SDDbgValue should not be emitted.
8851       Dbg->setIsInvalidated();
8852       Dbg->setIsEmitted();
8853     }
8854   }
8855 
8856   for (SDDbgValue *Dbg : ClonedDVs) {
8857     assert(is_contained(Dbg->getSDNodes(), ToNode) &&
8858            "Transferred DbgValues should depend on the new SDNode");
8859     AddDbgValue(Dbg, false);
8860   }
8861 }
8862 
8863 void SelectionDAG::salvageDebugInfo(SDNode &N) {
8864   if (!N.getHasDebugValue())
8865     return;
8866 
8867   SmallVector<SDDbgValue *, 2> ClonedDVs;
8868   for (auto DV : GetDbgValues(&N)) {
8869     if (DV->isInvalidated())
8870       continue;
8871     switch (N.getOpcode()) {
8872     default:
8873       break;
8874     case ISD::ADD:
8875       SDValue N0 = N.getOperand(0);
8876       SDValue N1 = N.getOperand(1);
8877       if (!isConstantIntBuildVectorOrConstantInt(N0) &&
8878           isConstantIntBuildVectorOrConstantInt(N1)) {
8879         uint64_t Offset = N.getConstantOperandVal(1);
8880 
8881         // Rewrite an ADD constant node into a DIExpression. Since we are
8882         // performing arithmetic to compute the variable's *value* in the
8883         // DIExpression, we need to mark the expression with a
8884         // DW_OP_stack_value.
8885         auto *DIExpr = DV->getExpression();
8886         auto NewLocOps = DV->copyLocationOps();
8887         bool Changed = false;
8888         for (size_t i = 0; i < NewLocOps.size(); ++i) {
8889           // We're not given a ResNo to compare against because the whole
8890           // node is going away. We know that any ISD::ADD only has one
8891           // result, so we can assume any node match is using the result.
8892           if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
8893               NewLocOps[i].getSDNode() != &N)
8894             continue;
8895           NewLocOps[i] = SDDbgOperand::fromNode(N0.getNode(), N0.getResNo());
8896           SmallVector<uint64_t, 3> ExprOps;
8897           DIExpression::appendOffset(ExprOps, Offset);
8898           DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
8899           Changed = true;
8900         }
8901         (void)Changed;
8902         assert(Changed && "Salvage target doesn't use N");
8903 
8904         auto AdditionalDependencies = DV->getAdditionalDependencies();
8905         SDDbgValue *Clone = getDbgValueList(DV->getVariable(), DIExpr,
8906                                             NewLocOps, AdditionalDependencies,
8907                                             DV->isIndirect(), DV->getDebugLoc(),
8908                                             DV->getOrder(), DV->isVariadic());
8909         ClonedDVs.push_back(Clone);
8910         DV->setIsInvalidated();
8911         DV->setIsEmitted();
8912         LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
8913                    N0.getNode()->dumprFull(this);
8914                    dbgs() << " into " << *DIExpr << '\n');
8915       }
8916     }
8917   }
8918 
8919   for (SDDbgValue *Dbg : ClonedDVs) {
8920     assert(!Dbg->getSDNodes().empty() &&
8921            "Salvaged DbgValue should depend on a new SDNode");
8922     AddDbgValue(Dbg, false);
8923   }
8924 }
8925 
8926 /// Creates a SDDbgLabel node.
8927 SDDbgLabel *SelectionDAG::getDbgLabel(DILabel *Label,
8928                                       const DebugLoc &DL, unsigned O) {
8929   assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
8930          "Expected inlined-at fields to agree");
8931   return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
8932 }
8933 
8934 namespace {
8935 
8936 /// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
8937 /// pointed to by a use iterator is deleted, increment the use iterator
8938 /// so that it doesn't dangle.
8939 ///
8940 class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
8941   SDNode::use_iterator &UI;
8942   SDNode::use_iterator &UE;
8943 
8944   void NodeDeleted(SDNode *N, SDNode *E) override {
8945     // Increment the iterator as needed.
8946     while (UI != UE && N == *UI)
8947       ++UI;
8948   }
8949 
8950 public:
8951   RAUWUpdateListener(SelectionDAG &d,
8952                      SDNode::use_iterator &ui,
8953                      SDNode::use_iterator &ue)
8954     : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
8955 };
8956 
8957 } // end anonymous namespace
8958 
8959 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
8960 /// This can cause recursive merging of nodes in the DAG.
8961 ///
8962 /// This version assumes From has a single result value.
8963 ///
8964 void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
8965   SDNode *From = FromN.getNode();
8966   assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
8967          "Cannot replace with this method!");
8968   assert(From != To.getNode() && "Cannot replace uses of with self");
8969 
8970   // Preserve Debug Values
8971   transferDbgValues(FromN, To);
8972 
8973   // Iterate over all the existing uses of From. New uses will be added
8974   // to the beginning of the use list, which we avoid visiting.
8975   // This specifically avoids visiting uses of From that arise while the
8976   // replacement is happening, because any such uses would be the result
8977   // of CSE: If an existing node looks like From after one of its operands
8978   // is replaced by To, we don't want to replace of all its users with To
8979   // too. See PR3018 for more info.
8980   SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
8981   RAUWUpdateListener Listener(*this, UI, UE);
8982   while (UI != UE) {
8983     SDNode *User = *UI;
8984 
8985     // This node is about to morph, remove its old self from the CSE maps.
8986     RemoveNodeFromCSEMaps(User);
8987 
8988     // A user can appear in a use list multiple times, and when this
8989     // happens the uses are usually next to each other in the list.
8990     // To help reduce the number of CSE recomputations, process all
8991     // the uses of this user that we can find this way.
8992     do {
8993       SDUse &Use = UI.getUse();
8994       ++UI;
8995       Use.set(To);
8996       if (To->isDivergent() != From->isDivergent())
8997         updateDivergence(User);
8998     } while (UI != UE && *UI == User);
8999     // Now that we have modified User, add it back to the CSE maps.  If it
9000     // already exists there, recursively merge the results together.
9001     AddModifiedNodeToCSEMaps(User);
9002   }
9003 
9004   // If we just RAUW'd the root, take note.
9005   if (FromN == getRoot())
9006     setRoot(To);
9007 }
9008 
9009 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
9010 /// This can cause recursive merging of nodes in the DAG.
9011 ///
9012 /// This version assumes that for each value of From, there is a
9013 /// corresponding value in To in the same position with the same type.
9014 ///
9015 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
9016 #ifndef NDEBUG
9017   for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
9018     assert((!From->hasAnyUseOfValue(i) ||
9019             From->getValueType(i) == To->getValueType(i)) &&
9020            "Cannot use this version of ReplaceAllUsesWith!");
9021 #endif
9022 
9023   // Handle the trivial case.
9024   if (From == To)
9025     return;
9026 
9027   // Preserve Debug Info. Only do this if there's a use.
9028   for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
9029     if (From->hasAnyUseOfValue(i)) {
9030       assert((i < To->getNumValues()) && "Invalid To location");
9031       transferDbgValues(SDValue(From, i), SDValue(To, i));
9032     }
9033 
9034   // Iterate over just the existing users of From. See the comments in
9035   // the ReplaceAllUsesWith above.
9036   SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
9037   RAUWUpdateListener Listener(*this, UI, UE);
9038   while (UI != UE) {
9039     SDNode *User = *UI;
9040 
9041     // This node is about to morph, remove its old self from the CSE maps.
9042     RemoveNodeFromCSEMaps(User);
9043 
9044     // A user can appear in a use list multiple times, and when this
9045     // happens the uses are usually next to each other in the list.
9046     // To help reduce the number of CSE recomputations, process all
9047     // the uses of this user that we can find this way.
9048     do {
9049       SDUse &Use = UI.getUse();
9050       ++UI;
9051       Use.setNode(To);
9052       if (To->isDivergent() != From->isDivergent())
9053         updateDivergence(User);
9054     } while (UI != UE && *UI == User);
9055 
9056     // Now that we have modified User, add it back to the CSE maps.  If it
9057     // already exists there, recursively merge the results together.
9058     AddModifiedNodeToCSEMaps(User);
9059   }
9060 
9061   // If we just RAUW'd the root, take note.
9062   if (From == getRoot().getNode())
9063     setRoot(SDValue(To, getRoot().getResNo()));
9064 }
9065 
9066 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
9067 /// This can cause recursive merging of nodes in the DAG.
9068 ///
9069 /// This version can replace From with any result values.  To must match the
9070 /// number and types of values returned by From.
9071 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
9072   if (From->getNumValues() == 1)  // Handle the simple case efficiently.
9073     return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
9074 
9075   // Preserve Debug Info.
9076   for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
9077     transferDbgValues(SDValue(From, i), To[i]);
9078 
9079   // Iterate over just the existing users of From. See the comments in
9080   // the ReplaceAllUsesWith above.
9081   SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
9082   RAUWUpdateListener Listener(*this, UI, UE);
9083   while (UI != UE) {
9084     SDNode *User = *UI;
9085 
9086     // This node is about to morph, remove its old self from the CSE maps.
9087     RemoveNodeFromCSEMaps(User);
9088 
9089     // A user can appear in a use list multiple times, and when this happens the
9090     // uses are usually next to each other in the list.  To help reduce the
9091     // number of CSE and divergence recomputations, process all the uses of this
9092     // user that we can find this way.
9093     bool To_IsDivergent = false;
9094     do {
9095       SDUse &Use = UI.getUse();
9096       const SDValue &ToOp = To[Use.getResNo()];
9097       ++UI;
9098       Use.set(ToOp);
9099       To_IsDivergent |= ToOp->isDivergent();
9100     } while (UI != UE && *UI == User);
9101 
9102     if (To_IsDivergent != From->isDivergent())
9103       updateDivergence(User);
9104 
9105     // Now that we have modified User, add it back to the CSE maps.  If it
9106     // already exists there, recursively merge the results together.
9107     AddModifiedNodeToCSEMaps(User);
9108   }
9109 
9110   // If we just RAUW'd the root, take note.
9111   if (From == getRoot().getNode())
9112     setRoot(SDValue(To[getRoot().getResNo()]));
9113 }
9114 
9115 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
9116 /// uses of other values produced by From.getNode() alone.  The Deleted
9117 /// vector is handled the same way as for ReplaceAllUsesWith.
9118 void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
9119   // Handle the really simple, really trivial case efficiently.
9120   if (From == To) return;
9121 
9122   // Handle the simple, trivial, case efficiently.
9123   if (From.getNode()->getNumValues() == 1) {
9124     ReplaceAllUsesWith(From, To);
9125     return;
9126   }
9127 
9128   // Preserve Debug Info.
9129   transferDbgValues(From, To);
9130 
9131   // Iterate over just the existing users of From. See the comments in
9132   // the ReplaceAllUsesWith above.
9133   SDNode::use_iterator UI = From.getNode()->use_begin(),
9134                        UE = From.getNode()->use_end();
9135   RAUWUpdateListener Listener(*this, UI, UE);
9136   while (UI != UE) {
9137     SDNode *User = *UI;
9138     bool UserRemovedFromCSEMaps = false;
9139 
9140     // A user can appear in a use list multiple times, and when this
9141     // happens the uses are usually next to each other in the list.
9142     // To help reduce the number of CSE recomputations, process all
9143     // the uses of this user that we can find this way.
9144     do {
9145       SDUse &Use = UI.getUse();
9146 
9147       // Skip uses of different values from the same node.
9148       if (Use.getResNo() != From.getResNo()) {
9149         ++UI;
9150         continue;
9151       }
9152 
9153       // If this node hasn't been modified yet, it's still in the CSE maps,
9154       // so remove its old self from the CSE maps.
9155       if (!UserRemovedFromCSEMaps) {
9156         RemoveNodeFromCSEMaps(User);
9157         UserRemovedFromCSEMaps = true;
9158       }
9159 
9160       ++UI;
9161       Use.set(To);
9162       if (To->isDivergent() != From->isDivergent())
9163         updateDivergence(User);
9164     } while (UI != UE && *UI == User);
9165     // We are iterating over all uses of the From node, so if a use
9166     // doesn't use the specific value, no changes are made.
9167     if (!UserRemovedFromCSEMaps)
9168       continue;
9169 
9170     // Now that we have modified User, add it back to the CSE maps.  If it
9171     // already exists there, recursively merge the results together.
9172     AddModifiedNodeToCSEMaps(User);
9173   }
9174 
9175   // If we just RAUW'd the root, take note.
9176   if (From == getRoot())
9177     setRoot(To);
9178 }
9179 
9180 namespace {
9181 
9182   /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
9183   /// to record information about a use.
9184   struct UseMemo {
9185     SDNode *User;
9186     unsigned Index;
9187     SDUse *Use;
9188   };
9189 
9190   /// operator< - Sort Memos by User.
9191   bool operator<(const UseMemo &L, const UseMemo &R) {
9192     return (intptr_t)L.User < (intptr_t)R.User;
9193   }
9194 
9195 } // end anonymous namespace
9196 
9197 bool SelectionDAG::calculateDivergence(SDNode *N) {
9198   if (TLI->isSDNodeAlwaysUniform(N)) {
9199     assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, DA) &&
9200            "Conflicting divergence information!");
9201     return false;
9202   }
9203   if (TLI->isSDNodeSourceOfDivergence(N, FLI, DA))
9204     return true;
9205   for (auto &Op : N->ops()) {
9206     if (Op.Val.getValueType() != MVT::Other && Op.getNode()->isDivergent())
9207       return true;
9208   }
9209   return false;
9210 }
9211 
9212 void SelectionDAG::updateDivergence(SDNode *N) {
9213   SmallVector<SDNode *, 16> Worklist(1, N);
9214   do {
9215     N = Worklist.pop_back_val();
9216     bool IsDivergent = calculateDivergence(N);
9217     if (N->SDNodeBits.IsDivergent != IsDivergent) {
9218       N->SDNodeBits.IsDivergent = IsDivergent;
9219       llvm::append_range(Worklist, N->uses());
9220     }
9221   } while (!Worklist.empty());
9222 }
9223 
9224 void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
9225   DenseMap<SDNode *, unsigned> Degree;
9226   Order.reserve(AllNodes.size());
9227   for (auto &N : allnodes()) {
9228     unsigned NOps = N.getNumOperands();
9229     Degree[&N] = NOps;
9230     if (0 == NOps)
9231       Order.push_back(&N);
9232   }
9233   for (size_t I = 0; I != Order.size(); ++I) {
9234     SDNode *N = Order[I];
9235     for (auto U : N->uses()) {
9236       unsigned &UnsortedOps = Degree[U];
9237       if (0 == --UnsortedOps)
9238         Order.push_back(U);
9239     }
9240   }
9241 }
9242 
9243 #ifndef NDEBUG
9244 void SelectionDAG::VerifyDAGDiverence() {
9245   std::vector<SDNode *> TopoOrder;
9246   CreateTopologicalOrder(TopoOrder);
9247   for (auto *N : TopoOrder) {
9248     assert(calculateDivergence(N) == N->isDivergent() &&
9249            "Divergence bit inconsistency detected");
9250   }
9251 }
9252 #endif
9253 
9254 /// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
9255 /// uses of other values produced by From.getNode() alone.  The same value
9256 /// may appear in both the From and To list.  The Deleted vector is
9257 /// handled the same way as for ReplaceAllUsesWith.
9258 void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
9259                                               const SDValue *To,
9260                                               unsigned Num){
9261   // Handle the simple, trivial case efficiently.
9262   if (Num == 1)
9263     return ReplaceAllUsesOfValueWith(*From, *To);
9264 
9265   transferDbgValues(*From, *To);
9266 
9267   // Read up all the uses and make records of them. This helps
9268   // processing new uses that are introduced during the
9269   // replacement process.
9270   SmallVector<UseMemo, 4> Uses;
9271   for (unsigned i = 0; i != Num; ++i) {
9272     unsigned FromResNo = From[i].getResNo();
9273     SDNode *FromNode = From[i].getNode();
9274     for (SDNode::use_iterator UI = FromNode->use_begin(),
9275          E = FromNode->use_end(); UI != E; ++UI) {
9276       SDUse &Use = UI.getUse();
9277       if (Use.getResNo() == FromResNo) {
9278         UseMemo Memo = { *UI, i, &Use };
9279         Uses.push_back(Memo);
9280       }
9281     }
9282   }
9283 
9284   // Sort the uses, so that all the uses from a given User are together.
9285   llvm::sort(Uses);
9286 
9287   for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
9288        UseIndex != UseIndexEnd; ) {
9289     // We know that this user uses some value of From.  If it is the right
9290     // value, update it.
9291     SDNode *User = Uses[UseIndex].User;
9292 
9293     // This node is about to morph, remove its old self from the CSE maps.
9294     RemoveNodeFromCSEMaps(User);
9295 
9296     // The Uses array is sorted, so all the uses for a given User
9297     // are next to each other in the list.
9298     // To help reduce the number of CSE recomputations, process all
9299     // the uses of this user that we can find this way.
9300     do {
9301       unsigned i = Uses[UseIndex].Index;
9302       SDUse &Use = *Uses[UseIndex].Use;
9303       ++UseIndex;
9304 
9305       Use.set(To[i]);
9306     } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
9307 
9308     // Now that we have modified User, add it back to the CSE maps.  If it
9309     // already exists there, recursively merge the results together.
9310     AddModifiedNodeToCSEMaps(User);
9311   }
9312 }
9313 
9314 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
9315 /// based on their topological order. It returns the maximum id and a vector
9316 /// of the SDNodes* in assigned order by reference.
9317 unsigned SelectionDAG::AssignTopologicalOrder() {
9318   unsigned DAGSize = 0;
9319 
9320   // SortedPos tracks the progress of the algorithm. Nodes before it are
9321   // sorted, nodes after it are unsorted. When the algorithm completes
9322   // it is at the end of the list.
9323   allnodes_iterator SortedPos = allnodes_begin();
9324 
9325   // Visit all the nodes. Move nodes with no operands to the front of
9326   // the list immediately. Annotate nodes that do have operands with their
9327   // operand count. Before we do this, the Node Id fields of the nodes
9328   // may contain arbitrary values. After, the Node Id fields for nodes
9329   // before SortedPos will contain the topological sort index, and the
9330   // Node Id fields for nodes At SortedPos and after will contain the
9331   // count of outstanding operands.
9332   for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
9333     SDNode *N = &*I++;
9334     checkForCycles(N, this);
9335     unsigned Degree = N->getNumOperands();
9336     if (Degree == 0) {
9337       // A node with no uses, add it to the result array immediately.
9338       N->setNodeId(DAGSize++);
9339       allnodes_iterator Q(N);
9340       if (Q != SortedPos)
9341         SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
9342       assert(SortedPos != AllNodes.end() && "Overran node list");
9343       ++SortedPos;
9344     } else {
9345       // Temporarily use the Node Id as scratch space for the degree count.
9346       N->setNodeId(Degree);
9347     }
9348   }
9349 
9350   // Visit all the nodes. As we iterate, move nodes into sorted order,
9351   // such that by the time the end is reached all nodes will be sorted.
9352   for (SDNode &Node : allnodes()) {
9353     SDNode *N = &Node;
9354     checkForCycles(N, this);
9355     // N is in sorted position, so all its uses have one less operand
9356     // that needs to be sorted.
9357     for (SDNode *P : N->uses()) {
9358       unsigned Degree = P->getNodeId();
9359       assert(Degree != 0 && "Invalid node degree");
9360       --Degree;
9361       if (Degree == 0) {
9362         // All of P's operands are sorted, so P may sorted now.
9363         P->setNodeId(DAGSize++);
9364         if (P->getIterator() != SortedPos)
9365           SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
9366         assert(SortedPos != AllNodes.end() && "Overran node list");
9367         ++SortedPos;
9368       } else {
9369         // Update P's outstanding operand count.
9370         P->setNodeId(Degree);
9371       }
9372     }
9373     if (Node.getIterator() == SortedPos) {
9374 #ifndef NDEBUG
9375       allnodes_iterator I(N);
9376       SDNode *S = &*++I;
9377       dbgs() << "Overran sorted position:\n";
9378       S->dumprFull(this); dbgs() << "\n";
9379       dbgs() << "Checking if this is due to cycles\n";
9380       checkForCycles(this, true);
9381 #endif
9382       llvm_unreachable(nullptr);
9383     }
9384   }
9385 
9386   assert(SortedPos == AllNodes.end() &&
9387          "Topological sort incomplete!");
9388   assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
9389          "First node in topological sort is not the entry token!");
9390   assert(AllNodes.front().getNodeId() == 0 &&
9391          "First node in topological sort has non-zero id!");
9392   assert(AllNodes.front().getNumOperands() == 0 &&
9393          "First node in topological sort has operands!");
9394   assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
9395          "Last node in topologic sort has unexpected id!");
9396   assert(AllNodes.back().use_empty() &&
9397          "Last node in topologic sort has users!");
9398   assert(DAGSize == allnodes_size() && "Node count mismatch!");
9399   return DAGSize;
9400 }
9401 
9402 /// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
9403 /// value is produced by SD.
9404 void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
9405   for (SDNode *SD : DB->getSDNodes()) {
9406     if (!SD)
9407       continue;
9408     assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
9409     SD->setHasDebugValue(true);
9410   }
9411   DbgInfo->add(DB, isParameter);
9412 }
9413 
9414 void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
9415 
9416 SDValue SelectionDAG::makeEquivalentMemoryOrdering(SDValue OldChain,
9417                                                    SDValue NewMemOpChain) {
9418   assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
9419   assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
9420   // The new memory operation must have the same position as the old load in
9421   // terms of memory dependency. Create a TokenFactor for the old load and new
9422   // memory operation and update uses of the old load's output chain to use that
9423   // TokenFactor.
9424   if (OldChain == NewMemOpChain || OldChain.use_empty())
9425     return NewMemOpChain;
9426 
9427   SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
9428                                 OldChain, NewMemOpChain);
9429   ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
9430   UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
9431   return TokenFactor;
9432 }
9433 
9434 SDValue SelectionDAG::makeEquivalentMemoryOrdering(LoadSDNode *OldLoad,
9435                                                    SDValue NewMemOp) {
9436   assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
9437   SDValue OldChain = SDValue(OldLoad, 1);
9438   SDValue NewMemOpChain = NewMemOp.getValue(1);
9439   return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
9440 }
9441 
9442 SDValue SelectionDAG::getSymbolFunctionGlobalAddress(SDValue Op,
9443                                                      Function **OutFunction) {
9444   assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
9445 
9446   auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
9447   auto *Module = MF->getFunction().getParent();
9448   auto *Function = Module->getFunction(Symbol);
9449 
9450   if (OutFunction != nullptr)
9451       *OutFunction = Function;
9452 
9453   if (Function != nullptr) {
9454     auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
9455     return getGlobalAddress(Function, SDLoc(Op), PtrTy);
9456   }
9457 
9458   std::string ErrorStr;
9459   raw_string_ostream ErrorFormatter(ErrorStr);
9460 
9461   ErrorFormatter << "Undefined external symbol ";
9462   ErrorFormatter << '"' << Symbol << '"';
9463   ErrorFormatter.flush();
9464 
9465   report_fatal_error(ErrorStr);
9466 }
9467 
9468 //===----------------------------------------------------------------------===//
9469 //                              SDNode Class
9470 //===----------------------------------------------------------------------===//
9471 
9472 bool llvm::isNullConstant(SDValue V) {
9473   ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
9474   return Const != nullptr && Const->isNullValue();
9475 }
9476 
9477 bool llvm::isNullFPConstant(SDValue V) {
9478   ConstantFPSDNode *Const = dyn_cast<ConstantFPSDNode>(V);
9479   return Const != nullptr && Const->isZero() && !Const->isNegative();
9480 }
9481 
9482 bool llvm::isAllOnesConstant(SDValue V) {
9483   ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
9484   return Const != nullptr && Const->isAllOnesValue();
9485 }
9486 
9487 bool llvm::isOneConstant(SDValue V) {
9488   ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
9489   return Const != nullptr && Const->isOne();
9490 }
9491 
9492 SDValue llvm::peekThroughBitcasts(SDValue V) {
9493   while (V.getOpcode() == ISD::BITCAST)
9494     V = V.getOperand(0);
9495   return V;
9496 }
9497 
9498 SDValue llvm::peekThroughOneUseBitcasts(SDValue V) {
9499   while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
9500     V = V.getOperand(0);
9501   return V;
9502 }
9503 
9504 SDValue llvm::peekThroughExtractSubvectors(SDValue V) {
9505   while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
9506     V = V.getOperand(0);
9507   return V;
9508 }
9509 
9510 bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
9511   if (V.getOpcode() != ISD::XOR)
9512     return false;
9513   V = peekThroughBitcasts(V.getOperand(1));
9514   unsigned NumBits = V.getScalarValueSizeInBits();
9515   ConstantSDNode *C =
9516       isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
9517   return C && (C->getAPIntValue().countTrailingOnes() >= NumBits);
9518 }
9519 
9520 ConstantSDNode *llvm::isConstOrConstSplat(SDValue N, bool AllowUndefs,
9521                                           bool AllowTruncation) {
9522   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N))
9523     return CN;
9524 
9525   // SplatVectors can truncate their operands. Ignore that case here unless
9526   // AllowTruncation is set.
9527   if (N->getOpcode() == ISD::SPLAT_VECTOR) {
9528     EVT VecEltVT = N->getValueType(0).getVectorElementType();
9529     if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
9530       EVT CVT = CN->getValueType(0);
9531       assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
9532       if (AllowTruncation || CVT == VecEltVT)
9533         return CN;
9534     }
9535   }
9536 
9537   if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
9538     BitVector UndefElements;
9539     ConstantSDNode *CN = BV->getConstantSplatNode(&UndefElements);
9540 
9541     // BuildVectors can truncate their operands. Ignore that case here unless
9542     // AllowTruncation is set.
9543     if (CN && (UndefElements.none() || AllowUndefs)) {
9544       EVT CVT = CN->getValueType(0);
9545       EVT NSVT = N.getValueType().getScalarType();
9546       assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
9547       if (AllowTruncation || (CVT == NSVT))
9548         return CN;
9549     }
9550   }
9551 
9552   return nullptr;
9553 }
9554 
9555 ConstantSDNode *llvm::isConstOrConstSplat(SDValue N, const APInt &DemandedElts,
9556                                           bool AllowUndefs,
9557                                           bool AllowTruncation) {
9558   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N))
9559     return CN;
9560 
9561   if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
9562     BitVector UndefElements;
9563     ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
9564 
9565     // BuildVectors can truncate their operands. Ignore that case here unless
9566     // AllowTruncation is set.
9567     if (CN && (UndefElements.none() || AllowUndefs)) {
9568       EVT CVT = CN->getValueType(0);
9569       EVT NSVT = N.getValueType().getScalarType();
9570       assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
9571       if (AllowTruncation || (CVT == NSVT))
9572         return CN;
9573     }
9574   }
9575 
9576   return nullptr;
9577 }
9578 
9579 ConstantFPSDNode *llvm::isConstOrConstSplatFP(SDValue N, bool AllowUndefs) {
9580   if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
9581     return CN;
9582 
9583   if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
9584     BitVector UndefElements;
9585     ConstantFPSDNode *CN = BV->getConstantFPSplatNode(&UndefElements);
9586     if (CN && (UndefElements.none() || AllowUndefs))
9587       return CN;
9588   }
9589 
9590   if (N.getOpcode() == ISD::SPLAT_VECTOR)
9591     if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
9592       return CN;
9593 
9594   return nullptr;
9595 }
9596 
9597 ConstantFPSDNode *llvm::isConstOrConstSplatFP(SDValue N,
9598                                               const APInt &DemandedElts,
9599                                               bool AllowUndefs) {
9600   if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
9601     return CN;
9602 
9603   if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
9604     BitVector UndefElements;
9605     ConstantFPSDNode *CN =
9606         BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
9607     if (CN && (UndefElements.none() || AllowUndefs))
9608       return CN;
9609   }
9610 
9611   return nullptr;
9612 }
9613 
9614 bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
9615   // TODO: may want to use peekThroughBitcast() here.
9616   ConstantSDNode *C =
9617       isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
9618   return C && C->isNullValue();
9619 }
9620 
9621 bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
9622   // TODO: may want to use peekThroughBitcast() here.
9623   unsigned BitWidth = N.getScalarValueSizeInBits();
9624   ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
9625   return C && C->isOne() && C->getValueSizeInBits(0) == BitWidth;
9626 }
9627 
9628 bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
9629   N = peekThroughBitcasts(N);
9630   unsigned BitWidth = N.getScalarValueSizeInBits();
9631   ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
9632   return C && C->isAllOnesValue() && C->getValueSizeInBits(0) == BitWidth;
9633 }
9634 
9635 HandleSDNode::~HandleSDNode() {
9636   DropOperands();
9637 }
9638 
9639 GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
9640                                          const DebugLoc &DL,
9641                                          const GlobalValue *GA, EVT VT,
9642                                          int64_t o, unsigned TF)
9643     : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
9644   TheGlobal = GA;
9645 }
9646 
9647 AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, const DebugLoc &dl,
9648                                          EVT VT, unsigned SrcAS,
9649                                          unsigned DestAS)
9650     : SDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT)),
9651       SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
9652 
9653 MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
9654                      SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
9655     : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
9656   MemSDNodeBits.IsVolatile = MMO->isVolatile();
9657   MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
9658   MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
9659   MemSDNodeBits.IsInvariant = MMO->isInvariant();
9660 
9661   // We check here that the size of the memory operand fits within the size of
9662   // the MMO. This is because the MMO might indicate only a possible address
9663   // range instead of specifying the affected memory addresses precisely.
9664   // TODO: Make MachineMemOperands aware of scalable vectors.
9665   assert(memvt.getStoreSize().getKnownMinSize() <= MMO->getSize() &&
9666          "Size mismatch!");
9667 }
9668 
9669 /// Profile - Gather unique data for the node.
9670 ///
9671 void SDNode::Profile(FoldingSetNodeID &ID) const {
9672   AddNodeIDNode(ID, this);
9673 }
9674 
9675 namespace {
9676 
9677   struct EVTArray {
9678     std::vector<EVT> VTs;
9679 
9680     EVTArray() {
9681       VTs.reserve(MVT::VALUETYPE_SIZE);
9682       for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
9683         VTs.push_back(MVT((MVT::SimpleValueType)i));
9684     }
9685   };
9686 
9687 } // end anonymous namespace
9688 
9689 static ManagedStatic<std::set<EVT, EVT::compareRawBits>> EVTs;
9690 static ManagedStatic<EVTArray> SimpleVTArray;
9691 static ManagedStatic<sys::SmartMutex<true>> VTMutex;
9692 
9693 /// getValueTypeList - Return a pointer to the specified value type.
9694 ///
9695 const EVT *SDNode::getValueTypeList(EVT VT) {
9696   if (VT.isExtended()) {
9697     sys::SmartScopedLock<true> Lock(*VTMutex);
9698     return &(*EVTs->insert(VT).first);
9699   }
9700   assert(VT.getSimpleVT() < MVT::VALUETYPE_SIZE && "Value type out of range!");
9701   return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
9702 }
9703 
9704 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
9705 /// indicated value.  This method ignores uses of other values defined by this
9706 /// operation.
9707 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
9708   assert(Value < getNumValues() && "Bad value!");
9709 
9710   // TODO: Only iterate over uses of a given value of the node
9711   for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
9712     if (UI.getUse().getResNo() == Value) {
9713       if (NUses == 0)
9714         return false;
9715       --NUses;
9716     }
9717   }
9718 
9719   // Found exactly the right number of uses?
9720   return NUses == 0;
9721 }
9722 
9723 /// hasAnyUseOfValue - Return true if there are any use of the indicated
9724 /// value. This method ignores uses of other values defined by this operation.
9725 bool SDNode::hasAnyUseOfValue(unsigned Value) const {
9726   assert(Value < getNumValues() && "Bad value!");
9727 
9728   for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
9729     if (UI.getUse().getResNo() == Value)
9730       return true;
9731 
9732   return false;
9733 }
9734 
9735 /// isOnlyUserOf - Return true if this node is the only use of N.
9736 bool SDNode::isOnlyUserOf(const SDNode *N) const {
9737   bool Seen = false;
9738   for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
9739     SDNode *User = *I;
9740     if (User == this)
9741       Seen = true;
9742     else
9743       return false;
9744   }
9745 
9746   return Seen;
9747 }
9748 
9749 /// Return true if the only users of N are contained in Nodes.
9750 bool SDNode::areOnlyUsersOf(ArrayRef<const SDNode *> Nodes, const SDNode *N) {
9751   bool Seen = false;
9752   for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
9753     SDNode *User = *I;
9754     if (llvm::is_contained(Nodes, User))
9755       Seen = true;
9756     else
9757       return false;
9758   }
9759 
9760   return Seen;
9761 }
9762 
9763 /// isOperand - Return true if this node is an operand of N.
9764 bool SDValue::isOperandOf(const SDNode *N) const {
9765   return is_contained(N->op_values(), *this);
9766 }
9767 
9768 bool SDNode::isOperandOf(const SDNode *N) const {
9769   return any_of(N->op_values(),
9770                 [this](SDValue Op) { return this == Op.getNode(); });
9771 }
9772 
9773 /// reachesChainWithoutSideEffects - Return true if this operand (which must
9774 /// be a chain) reaches the specified operand without crossing any
9775 /// side-effecting instructions on any chain path.  In practice, this looks
9776 /// through token factors and non-volatile loads.  In order to remain efficient,
9777 /// this only looks a couple of nodes in, it does not do an exhaustive search.
9778 ///
9779 /// Note that we only need to examine chains when we're searching for
9780 /// side-effects; SelectionDAG requires that all side-effects are represented
9781 /// by chains, even if another operand would force a specific ordering. This
9782 /// constraint is necessary to allow transformations like splitting loads.
9783 bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
9784                                              unsigned Depth) const {
9785   if (*this == Dest) return true;
9786 
9787   // Don't search too deeply, we just want to be able to see through
9788   // TokenFactor's etc.
9789   if (Depth == 0) return false;
9790 
9791   // If this is a token factor, all inputs to the TF happen in parallel.
9792   if (getOpcode() == ISD::TokenFactor) {
9793     // First, try a shallow search.
9794     if (is_contained((*this)->ops(), Dest)) {
9795       // We found the chain we want as an operand of this TokenFactor.
9796       // Essentially, we reach the chain without side-effects if we could
9797       // serialize the TokenFactor into a simple chain of operations with
9798       // Dest as the last operation. This is automatically true if the
9799       // chain has one use: there are no other ordering constraints.
9800       // If the chain has more than one use, we give up: some other
9801       // use of Dest might force a side-effect between Dest and the current
9802       // node.
9803       if (Dest.hasOneUse())
9804         return true;
9805     }
9806     // Next, try a deep search: check whether every operand of the TokenFactor
9807     // reaches Dest.
9808     return llvm::all_of((*this)->ops(), [=](SDValue Op) {
9809       return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
9810     });
9811   }
9812 
9813   // Loads don't have side effects, look through them.
9814   if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
9815     if (Ld->isUnordered())
9816       return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
9817   }
9818   return false;
9819 }
9820 
9821 bool SDNode::hasPredecessor(const SDNode *N) const {
9822   SmallPtrSet<const SDNode *, 32> Visited;
9823   SmallVector<const SDNode *, 16> Worklist;
9824   Worklist.push_back(this);
9825   return hasPredecessorHelper(N, Visited, Worklist);
9826 }
9827 
9828 void SDNode::intersectFlagsWith(const SDNodeFlags Flags) {
9829   this->Flags.intersectWith(Flags);
9830 }
9831 
9832 SDValue
9833 SelectionDAG::matchBinOpReduction(SDNode *Extract, ISD::NodeType &BinOp,
9834                                   ArrayRef<ISD::NodeType> CandidateBinOps,
9835                                   bool AllowPartials) {
9836   // The pattern must end in an extract from index 0.
9837   if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
9838       !isNullConstant(Extract->getOperand(1)))
9839     return SDValue();
9840 
9841   // Match against one of the candidate binary ops.
9842   SDValue Op = Extract->getOperand(0);
9843   if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
9844         return Op.getOpcode() == unsigned(BinOp);
9845       }))
9846     return SDValue();
9847 
9848   // Floating-point reductions may require relaxed constraints on the final step
9849   // of the reduction because they may reorder intermediate operations.
9850   unsigned CandidateBinOp = Op.getOpcode();
9851   if (Op.getValueType().isFloatingPoint()) {
9852     SDNodeFlags Flags = Op->getFlags();
9853     switch (CandidateBinOp) {
9854     case ISD::FADD:
9855       if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
9856         return SDValue();
9857       break;
9858     default:
9859       llvm_unreachable("Unhandled FP opcode for binop reduction");
9860     }
9861   }
9862 
9863   // Matching failed - attempt to see if we did enough stages that a partial
9864   // reduction from a subvector is possible.
9865   auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
9866     if (!AllowPartials || !Op)
9867       return SDValue();
9868     EVT OpVT = Op.getValueType();
9869     EVT OpSVT = OpVT.getScalarType();
9870     EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
9871     if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
9872       return SDValue();
9873     BinOp = (ISD::NodeType)CandidateBinOp;
9874     return getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(Op), SubVT, Op,
9875                    getVectorIdxConstant(0, SDLoc(Op)));
9876   };
9877 
9878   // At each stage, we're looking for something that looks like:
9879   // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
9880   //                    <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
9881   //                               i32 undef, i32 undef, i32 undef, i32 undef>
9882   // %a = binop <8 x i32> %op, %s
9883   // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
9884   // we expect something like:
9885   // <4,5,6,7,u,u,u,u>
9886   // <2,3,u,u,u,u,u,u>
9887   // <1,u,u,u,u,u,u,u>
9888   // While a partial reduction match would be:
9889   // <2,3,u,u,u,u,u,u>
9890   // <1,u,u,u,u,u,u,u>
9891   unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
9892   SDValue PrevOp;
9893   for (unsigned i = 0; i < Stages; ++i) {
9894     unsigned MaskEnd = (1 << i);
9895 
9896     if (Op.getOpcode() != CandidateBinOp)
9897       return PartialReduction(PrevOp, MaskEnd);
9898 
9899     SDValue Op0 = Op.getOperand(0);
9900     SDValue Op1 = Op.getOperand(1);
9901 
9902     ShuffleVectorSDNode *Shuffle = dyn_cast<ShuffleVectorSDNode>(Op0);
9903     if (Shuffle) {
9904       Op = Op1;
9905     } else {
9906       Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
9907       Op = Op0;
9908     }
9909 
9910     // The first operand of the shuffle should be the same as the other operand
9911     // of the binop.
9912     if (!Shuffle || Shuffle->getOperand(0) != Op)
9913       return PartialReduction(PrevOp, MaskEnd);
9914 
9915     // Verify the shuffle has the expected (at this stage of the pyramid) mask.
9916     for (int Index = 0; Index < (int)MaskEnd; ++Index)
9917       if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
9918         return PartialReduction(PrevOp, MaskEnd);
9919 
9920     PrevOp = Op;
9921   }
9922 
9923   // Handle subvector reductions, which tend to appear after the shuffle
9924   // reduction stages.
9925   while (Op.getOpcode() == CandidateBinOp) {
9926     unsigned NumElts = Op.getValueType().getVectorNumElements();
9927     SDValue Op0 = Op.getOperand(0);
9928     SDValue Op1 = Op.getOperand(1);
9929     if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
9930         Op1.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
9931         Op0.getOperand(0) != Op1.getOperand(0))
9932       break;
9933     SDValue Src = Op0.getOperand(0);
9934     unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
9935     if (NumSrcElts != (2 * NumElts))
9936       break;
9937     if (!(Op0.getConstantOperandAPInt(1) == 0 &&
9938           Op1.getConstantOperandAPInt(1) == NumElts) &&
9939         !(Op1.getConstantOperandAPInt(1) == 0 &&
9940           Op0.getConstantOperandAPInt(1) == NumElts))
9941       break;
9942     Op = Src;
9943   }
9944 
9945   BinOp = (ISD::NodeType)CandidateBinOp;
9946   return Op;
9947 }
9948 
9949 SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
9950   assert(N->getNumValues() == 1 &&
9951          "Can't unroll a vector with multiple results!");
9952 
9953   EVT VT = N->getValueType(0);
9954   unsigned NE = VT.getVectorNumElements();
9955   EVT EltVT = VT.getVectorElementType();
9956   SDLoc dl(N);
9957 
9958   SmallVector<SDValue, 8> Scalars;
9959   SmallVector<SDValue, 4> Operands(N->getNumOperands());
9960 
9961   // If ResNE is 0, fully unroll the vector op.
9962   if (ResNE == 0)
9963     ResNE = NE;
9964   else if (NE > ResNE)
9965     NE = ResNE;
9966 
9967   unsigned i;
9968   for (i= 0; i != NE; ++i) {
9969     for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
9970       SDValue Operand = N->getOperand(j);
9971       EVT OperandVT = Operand.getValueType();
9972       if (OperandVT.isVector()) {
9973         // A vector operand; extract a single element.
9974         EVT OperandEltVT = OperandVT.getVectorElementType();
9975         Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT,
9976                               Operand, getVectorIdxConstant(i, dl));
9977       } else {
9978         // A scalar operand; just use it as is.
9979         Operands[j] = Operand;
9980       }
9981     }
9982 
9983     switch (N->getOpcode()) {
9984     default: {
9985       Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
9986                                 N->getFlags()));
9987       break;
9988     }
9989     case ISD::VSELECT:
9990       Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
9991       break;
9992     case ISD::SHL:
9993     case ISD::SRA:
9994     case ISD::SRL:
9995     case ISD::ROTL:
9996     case ISD::ROTR:
9997       Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
9998                                getShiftAmountOperand(Operands[0].getValueType(),
9999                                                      Operands[1])));
10000       break;
10001     case ISD::SIGN_EXTEND_INREG: {
10002       EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
10003       Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
10004                                 Operands[0],
10005                                 getValueType(ExtVT)));
10006     }
10007     }
10008   }
10009 
10010   for (; i < ResNE; ++i)
10011     Scalars.push_back(getUNDEF(EltVT));
10012 
10013   EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
10014   return getBuildVector(VecVT, dl, Scalars);
10015 }
10016 
10017 std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
10018     SDNode *N, unsigned ResNE) {
10019   unsigned Opcode = N->getOpcode();
10020   assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
10021           Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
10022           Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
10023          "Expected an overflow opcode");
10024 
10025   EVT ResVT = N->getValueType(0);
10026   EVT OvVT = N->getValueType(1);
10027   EVT ResEltVT = ResVT.getVectorElementType();
10028   EVT OvEltVT = OvVT.getVectorElementType();
10029   SDLoc dl(N);
10030 
10031   // If ResNE is 0, fully unroll the vector op.
10032   unsigned NE = ResVT.getVectorNumElements();
10033   if (ResNE == 0)
10034     ResNE = NE;
10035   else if (NE > ResNE)
10036     NE = ResNE;
10037 
10038   SmallVector<SDValue, 8> LHSScalars;
10039   SmallVector<SDValue, 8> RHSScalars;
10040   ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
10041   ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
10042 
10043   EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
10044   SDVTList VTs = getVTList(ResEltVT, SVT);
10045   SmallVector<SDValue, 8> ResScalars;
10046   SmallVector<SDValue, 8> OvScalars;
10047   for (unsigned i = 0; i < NE; ++i) {
10048     SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
10049     SDValue Ov =
10050         getSelect(dl, OvEltVT, Res.getValue(1),
10051                   getBoolConstant(true, dl, OvEltVT, ResVT),
10052                   getConstant(0, dl, OvEltVT));
10053 
10054     ResScalars.push_back(Res);
10055     OvScalars.push_back(Ov);
10056   }
10057 
10058   ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
10059   OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
10060 
10061   EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
10062   EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
10063   return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
10064                         getBuildVector(NewOvVT, dl, OvScalars));
10065 }
10066 
10067 bool SelectionDAG::areNonVolatileConsecutiveLoads(LoadSDNode *LD,
10068                                                   LoadSDNode *Base,
10069                                                   unsigned Bytes,
10070                                                   int Dist) const {
10071   if (LD->isVolatile() || Base->isVolatile())
10072     return false;
10073   // TODO: probably too restrictive for atomics, revisit
10074   if (!LD->isSimple())
10075     return false;
10076   if (LD->isIndexed() || Base->isIndexed())
10077     return false;
10078   if (LD->getChain() != Base->getChain())
10079     return false;
10080   EVT VT = LD->getValueType(0);
10081   if (VT.getSizeInBits() / 8 != Bytes)
10082     return false;
10083 
10084   auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
10085   auto LocDecomp = BaseIndexOffset::match(LD, *this);
10086 
10087   int64_t Offset = 0;
10088   if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
10089     return (Dist * Bytes == Offset);
10090   return false;
10091 }
10092 
10093 /// InferPtrAlignment - Infer alignment of a load / store address. Return None
10094 /// if it cannot be inferred.
10095 MaybeAlign SelectionDAG::InferPtrAlign(SDValue Ptr) const {
10096   // If this is a GlobalAddress + cst, return the alignment.
10097   const GlobalValue *GV = nullptr;
10098   int64_t GVOffset = 0;
10099   if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
10100     unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
10101     KnownBits Known(PtrWidth);
10102     llvm::computeKnownBits(GV, Known, getDataLayout());
10103     unsigned AlignBits = Known.countMinTrailingZeros();
10104     if (AlignBits)
10105       return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
10106   }
10107 
10108   // If this is a direct reference to a stack slot, use information about the
10109   // stack slot's alignment.
10110   int FrameIdx = INT_MIN;
10111   int64_t FrameOffset = 0;
10112   if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
10113     FrameIdx = FI->getIndex();
10114   } else if (isBaseWithConstantOffset(Ptr) &&
10115              isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
10116     // Handle FI+Cst
10117     FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
10118     FrameOffset = Ptr.getConstantOperandVal(1);
10119   }
10120 
10121   if (FrameIdx != INT_MIN) {
10122     const MachineFrameInfo &MFI = getMachineFunction().getFrameInfo();
10123     return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
10124   }
10125 
10126   return None;
10127 }
10128 
10129 /// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
10130 /// which is split (or expanded) into two not necessarily identical pieces.
10131 std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
10132   // Currently all types are split in half.
10133   EVT LoVT, HiVT;
10134   if (!VT.isVector())
10135     LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
10136   else
10137     LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
10138 
10139   return std::make_pair(LoVT, HiVT);
10140 }
10141 
10142 /// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
10143 /// type, dependent on an enveloping VT that has been split into two identical
10144 /// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
10145 std::pair<EVT, EVT>
10146 SelectionDAG::GetDependentSplitDestVTs(const EVT &VT, const EVT &EnvVT,
10147                                        bool *HiIsEmpty) const {
10148   EVT EltTp = VT.getVectorElementType();
10149   // Examples:
10150   //   custom VL=8  with enveloping VL=8/8 yields 8/0 (hi empty)
10151   //   custom VL=9  with enveloping VL=8/8 yields 8/1
10152   //   custom VL=10 with enveloping VL=8/8 yields 8/2
10153   //   etc.
10154   ElementCount VTNumElts = VT.getVectorElementCount();
10155   ElementCount EnvNumElts = EnvVT.getVectorElementCount();
10156   assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
10157          "Mixing fixed width and scalable vectors when enveloping a type");
10158   EVT LoVT, HiVT;
10159   if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
10160     LoVT = EnvVT;
10161     HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
10162     *HiIsEmpty = false;
10163   } else {
10164     // Flag that hi type has zero storage size, but return split envelop type
10165     // (this would be easier if vector types with zero elements were allowed).
10166     LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
10167     HiVT = EnvVT;
10168     *HiIsEmpty = true;
10169   }
10170   return std::make_pair(LoVT, HiVT);
10171 }
10172 
10173 /// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
10174 /// low/high part.
10175 std::pair<SDValue, SDValue>
10176 SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
10177                           const EVT &HiVT) {
10178   assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
10179          LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
10180          "Splitting vector with an invalid mixture of fixed and scalable "
10181          "vector types");
10182   assert(LoVT.getVectorMinNumElements() + HiVT.getVectorMinNumElements() <=
10183              N.getValueType().getVectorMinNumElements() &&
10184          "More vector elements requested than available!");
10185   SDValue Lo, Hi;
10186   Lo =
10187       getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N, getVectorIdxConstant(0, DL));
10188   // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
10189   // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
10190   // IDX with the runtime scaling factor of the result vector type. For
10191   // fixed-width result vectors, that runtime scaling factor is 1.
10192   Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
10193                getVectorIdxConstant(LoVT.getVectorMinNumElements(), DL));
10194   return std::make_pair(Lo, Hi);
10195 }
10196 
10197 /// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
10198 SDValue SelectionDAG::WidenVector(const SDValue &N, const SDLoc &DL) {
10199   EVT VT = N.getValueType();
10200   EVT WideVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
10201                                 NextPowerOf2(VT.getVectorNumElements()));
10202   return getNode(ISD::INSERT_SUBVECTOR, DL, WideVT, getUNDEF(WideVT), N,
10203                  getVectorIdxConstant(0, DL));
10204 }
10205 
10206 void SelectionDAG::ExtractVectorElements(SDValue Op,
10207                                          SmallVectorImpl<SDValue> &Args,
10208                                          unsigned Start, unsigned Count,
10209                                          EVT EltVT) {
10210   EVT VT = Op.getValueType();
10211   if (Count == 0)
10212     Count = VT.getVectorNumElements();
10213   if (EltVT == EVT())
10214     EltVT = VT.getVectorElementType();
10215   SDLoc SL(Op);
10216   for (unsigned i = Start, e = Start + Count; i != e; ++i) {
10217     Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Op,
10218                            getVectorIdxConstant(i, SL)));
10219   }
10220 }
10221 
10222 // getAddressSpace - Return the address space this GlobalAddress belongs to.
10223 unsigned GlobalAddressSDNode::getAddressSpace() const {
10224   return getGlobal()->getType()->getAddressSpace();
10225 }
10226 
10227 Type *ConstantPoolSDNode::getType() const {
10228   if (isMachineConstantPoolEntry())
10229     return Val.MachineCPVal->getType();
10230   return Val.ConstVal->getType();
10231 }
10232 
10233 bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
10234                                         unsigned &SplatBitSize,
10235                                         bool &HasAnyUndefs,
10236                                         unsigned MinSplatBits,
10237                                         bool IsBigEndian) const {
10238   EVT VT = getValueType(0);
10239   assert(VT.isVector() && "Expected a vector type");
10240   unsigned VecWidth = VT.getSizeInBits();
10241   if (MinSplatBits > VecWidth)
10242     return false;
10243 
10244   // FIXME: The widths are based on this node's type, but build vectors can
10245   // truncate their operands.
10246   SplatValue = APInt(VecWidth, 0);
10247   SplatUndef = APInt(VecWidth, 0);
10248 
10249   // Get the bits. Bits with undefined values (when the corresponding element
10250   // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
10251   // in SplatValue. If any of the values are not constant, give up and return
10252   // false.
10253   unsigned int NumOps = getNumOperands();
10254   assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
10255   unsigned EltWidth = VT.getScalarSizeInBits();
10256 
10257   for (unsigned j = 0; j < NumOps; ++j) {
10258     unsigned i = IsBigEndian ? NumOps - 1 - j : j;
10259     SDValue OpVal = getOperand(i);
10260     unsigned BitPos = j * EltWidth;
10261 
10262     if (OpVal.isUndef())
10263       SplatUndef.setBits(BitPos, BitPos + EltWidth);
10264     else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
10265       SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
10266     else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
10267       SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
10268     else
10269       return false;
10270   }
10271 
10272   // The build_vector is all constants or undefs. Find the smallest element
10273   // size that splats the vector.
10274   HasAnyUndefs = (SplatUndef != 0);
10275 
10276   // FIXME: This does not work for vectors with elements less than 8 bits.
10277   while (VecWidth > 8) {
10278     unsigned HalfSize = VecWidth / 2;
10279     APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
10280     APInt LowValue = SplatValue.extractBits(HalfSize, 0);
10281     APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
10282     APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
10283 
10284     // If the two halves do not match (ignoring undef bits), stop here.
10285     if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
10286         MinSplatBits > HalfSize)
10287       break;
10288 
10289     SplatValue = HighValue | LowValue;
10290     SplatUndef = HighUndef & LowUndef;
10291 
10292     VecWidth = HalfSize;
10293   }
10294 
10295   SplatBitSize = VecWidth;
10296   return true;
10297 }
10298 
10299 SDValue BuildVectorSDNode::getSplatValue(const APInt &DemandedElts,
10300                                          BitVector *UndefElements) const {
10301   unsigned NumOps = getNumOperands();
10302   if (UndefElements) {
10303     UndefElements->clear();
10304     UndefElements->resize(NumOps);
10305   }
10306   assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
10307   if (!DemandedElts)
10308     return SDValue();
10309   SDValue Splatted;
10310   for (unsigned i = 0; i != NumOps; ++i) {
10311     if (!DemandedElts[i])
10312       continue;
10313     SDValue Op = getOperand(i);
10314     if (Op.isUndef()) {
10315       if (UndefElements)
10316         (*UndefElements)[i] = true;
10317     } else if (!Splatted) {
10318       Splatted = Op;
10319     } else if (Splatted != Op) {
10320       return SDValue();
10321     }
10322   }
10323 
10324   if (!Splatted) {
10325     unsigned FirstDemandedIdx = DemandedElts.countTrailingZeros();
10326     assert(getOperand(FirstDemandedIdx).isUndef() &&
10327            "Can only have a splat without a constant for all undefs.");
10328     return getOperand(FirstDemandedIdx);
10329   }
10330 
10331   return Splatted;
10332 }
10333 
10334 SDValue BuildVectorSDNode::getSplatValue(BitVector *UndefElements) const {
10335   APInt DemandedElts = APInt::getAllOnesValue(getNumOperands());
10336   return getSplatValue(DemandedElts, UndefElements);
10337 }
10338 
10339 bool BuildVectorSDNode::getRepeatedSequence(const APInt &DemandedElts,
10340                                             SmallVectorImpl<SDValue> &Sequence,
10341                                             BitVector *UndefElements) const {
10342   unsigned NumOps = getNumOperands();
10343   Sequence.clear();
10344   if (UndefElements) {
10345     UndefElements->clear();
10346     UndefElements->resize(NumOps);
10347   }
10348   assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
10349   if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
10350     return false;
10351 
10352   // Set the undefs even if we don't find a sequence (like getSplatValue).
10353   if (UndefElements)
10354     for (unsigned I = 0; I != NumOps; ++I)
10355       if (DemandedElts[I] && getOperand(I).isUndef())
10356         (*UndefElements)[I] = true;
10357 
10358   // Iteratively widen the sequence length looking for repetitions.
10359   for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
10360     Sequence.append(SeqLen, SDValue());
10361     for (unsigned I = 0; I != NumOps; ++I) {
10362       if (!DemandedElts[I])
10363         continue;
10364       SDValue &SeqOp = Sequence[I % SeqLen];
10365       SDValue Op = getOperand(I);
10366       if (Op.isUndef()) {
10367         if (!SeqOp)
10368           SeqOp = Op;
10369         continue;
10370       }
10371       if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
10372         Sequence.clear();
10373         break;
10374       }
10375       SeqOp = Op;
10376     }
10377     if (!Sequence.empty())
10378       return true;
10379   }
10380 
10381   assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
10382   return false;
10383 }
10384 
10385 bool BuildVectorSDNode::getRepeatedSequence(SmallVectorImpl<SDValue> &Sequence,
10386                                             BitVector *UndefElements) const {
10387   APInt DemandedElts = APInt::getAllOnesValue(getNumOperands());
10388   return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
10389 }
10390 
10391 ConstantSDNode *
10392 BuildVectorSDNode::getConstantSplatNode(const APInt &DemandedElts,
10393                                         BitVector *UndefElements) const {
10394   return dyn_cast_or_null<ConstantSDNode>(
10395       getSplatValue(DemandedElts, UndefElements));
10396 }
10397 
10398 ConstantSDNode *
10399 BuildVectorSDNode::getConstantSplatNode(BitVector *UndefElements) const {
10400   return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
10401 }
10402 
10403 ConstantFPSDNode *
10404 BuildVectorSDNode::getConstantFPSplatNode(const APInt &DemandedElts,
10405                                           BitVector *UndefElements) const {
10406   return dyn_cast_or_null<ConstantFPSDNode>(
10407       getSplatValue(DemandedElts, UndefElements));
10408 }
10409 
10410 ConstantFPSDNode *
10411 BuildVectorSDNode::getConstantFPSplatNode(BitVector *UndefElements) const {
10412   return dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements));
10413 }
10414 
10415 int32_t
10416 BuildVectorSDNode::getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements,
10417                                                    uint32_t BitWidth) const {
10418   if (ConstantFPSDNode *CN =
10419           dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements))) {
10420     bool IsExact;
10421     APSInt IntVal(BitWidth);
10422     const APFloat &APF = CN->getValueAPF();
10423     if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
10424             APFloat::opOK ||
10425         !IsExact)
10426       return -1;
10427 
10428     return IntVal.exactLogBase2();
10429   }
10430   return -1;
10431 }
10432 
10433 bool BuildVectorSDNode::isConstant() const {
10434   for (const SDValue &Op : op_values()) {
10435     unsigned Opc = Op.getOpcode();
10436     if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
10437       return false;
10438   }
10439   return true;
10440 }
10441 
10442 bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
10443   // Find the first non-undef value in the shuffle mask.
10444   unsigned i, e;
10445   for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
10446     /* search */;
10447 
10448   // If all elements are undefined, this shuffle can be considered a splat
10449   // (although it should eventually get simplified away completely).
10450   if (i == e)
10451     return true;
10452 
10453   // Make sure all remaining elements are either undef or the same as the first
10454   // non-undef value.
10455   for (int Idx = Mask[i]; i != e; ++i)
10456     if (Mask[i] >= 0 && Mask[i] != Idx)
10457       return false;
10458   return true;
10459 }
10460 
10461 // Returns the SDNode if it is a constant integer BuildVector
10462 // or constant integer.
10463 SDNode *SelectionDAG::isConstantIntBuildVectorOrConstantInt(SDValue N) const {
10464   if (isa<ConstantSDNode>(N))
10465     return N.getNode();
10466   if (ISD::isBuildVectorOfConstantSDNodes(N.getNode()))
10467     return N.getNode();
10468   // Treat a GlobalAddress supporting constant offset folding as a
10469   // constant integer.
10470   if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N))
10471     if (GA->getOpcode() == ISD::GlobalAddress &&
10472         TLI->isOffsetFoldingLegal(GA))
10473       return GA;
10474   if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
10475       isa<ConstantSDNode>(N.getOperand(0)))
10476     return N.getNode();
10477   return nullptr;
10478 }
10479 
10480 // Returns the SDNode if it is a constant float BuildVector
10481 // or constant float.
10482 SDNode *SelectionDAG::isConstantFPBuildVectorOrConstantFP(SDValue N) const {
10483   if (isa<ConstantFPSDNode>(N))
10484     return N.getNode();
10485 
10486   if (ISD::isBuildVectorOfConstantFPSDNodes(N.getNode()))
10487     return N.getNode();
10488 
10489   return nullptr;
10490 }
10491 
10492 void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
10493   assert(!Node->OperandList && "Node already has operands");
10494   assert(SDNode::getMaxNumOperands() >= Vals.size() &&
10495          "too many operands to fit into SDNode");
10496   SDUse *Ops = OperandRecycler.allocate(
10497       ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
10498 
10499   bool IsDivergent = false;
10500   for (unsigned I = 0; I != Vals.size(); ++I) {
10501     Ops[I].setUser(Node);
10502     Ops[I].setInitial(Vals[I]);
10503     if (Ops[I].Val.getValueType() != MVT::Other) // Skip Chain. It does not carry divergence.
10504       IsDivergent |= Ops[I].getNode()->isDivergent();
10505   }
10506   Node->NumOperands = Vals.size();
10507   Node->OperandList = Ops;
10508   if (!TLI->isSDNodeAlwaysUniform(Node)) {
10509     IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, DA);
10510     Node->SDNodeBits.IsDivergent = IsDivergent;
10511   }
10512   checkForCycles(Node);
10513 }
10514 
10515 SDValue SelectionDAG::getTokenFactor(const SDLoc &DL,
10516                                      SmallVectorImpl<SDValue> &Vals) {
10517   size_t Limit = SDNode::getMaxNumOperands();
10518   while (Vals.size() > Limit) {
10519     unsigned SliceIdx = Vals.size() - Limit;
10520     auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
10521     SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
10522     Vals.erase(Vals.begin() + SliceIdx, Vals.end());
10523     Vals.emplace_back(NewTF);
10524   }
10525   return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
10526 }
10527 
10528 SDValue SelectionDAG::getNeutralElement(unsigned Opcode, const SDLoc &DL,
10529                                         EVT VT, SDNodeFlags Flags) {
10530   switch (Opcode) {
10531   default:
10532     return SDValue();
10533   case ISD::ADD:
10534   case ISD::OR:
10535   case ISD::XOR:
10536   case ISD::UMAX:
10537     return getConstant(0, DL, VT);
10538   case ISD::MUL:
10539     return getConstant(1, DL, VT);
10540   case ISD::AND:
10541   case ISD::UMIN:
10542     return getAllOnesConstant(DL, VT);
10543   case ISD::SMAX:
10544     return getConstant(APInt::getSignedMinValue(VT.getSizeInBits()), DL, VT);
10545   case ISD::SMIN:
10546     return getConstant(APInt::getSignedMaxValue(VT.getSizeInBits()), DL, VT);
10547   case ISD::FADD:
10548     return getConstantFP(-0.0, DL, VT);
10549   case ISD::FMUL:
10550     return getConstantFP(1.0, DL, VT);
10551   case ISD::FMINNUM:
10552   case ISD::FMAXNUM: {
10553     // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
10554     const fltSemantics &Semantics = EVTToAPFloatSemantics(VT);
10555     APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
10556                         !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
10557                         APFloat::getLargest(Semantics);
10558     if (Opcode == ISD::FMAXNUM)
10559       NeutralAF.changeSign();
10560 
10561     return getConstantFP(NeutralAF, DL, VT);
10562   }
10563   }
10564 }
10565 
10566 #ifndef NDEBUG
10567 static void checkForCyclesHelper(const SDNode *N,
10568                                  SmallPtrSetImpl<const SDNode*> &Visited,
10569                                  SmallPtrSetImpl<const SDNode*> &Checked,
10570                                  const llvm::SelectionDAG *DAG) {
10571   // If this node has already been checked, don't check it again.
10572   if (Checked.count(N))
10573     return;
10574 
10575   // If a node has already been visited on this depth-first walk, reject it as
10576   // a cycle.
10577   if (!Visited.insert(N).second) {
10578     errs() << "Detected cycle in SelectionDAG\n";
10579     dbgs() << "Offending node:\n";
10580     N->dumprFull(DAG); dbgs() << "\n";
10581     abort();
10582   }
10583 
10584   for (const SDValue &Op : N->op_values())
10585     checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
10586 
10587   Checked.insert(N);
10588   Visited.erase(N);
10589 }
10590 #endif
10591 
10592 void llvm::checkForCycles(const llvm::SDNode *N,
10593                           const llvm::SelectionDAG *DAG,
10594                           bool force) {
10595 #ifndef NDEBUG
10596   bool check = force;
10597 #ifdef EXPENSIVE_CHECKS
10598   check = true;
10599 #endif  // EXPENSIVE_CHECKS
10600   if (check) {
10601     assert(N && "Checking nonexistent SDNode");
10602     SmallPtrSet<const SDNode*, 32> visited;
10603     SmallPtrSet<const SDNode*, 32> checked;
10604     checkForCyclesHelper(N, visited, checked, DAG);
10605   }
10606 #endif  // !NDEBUG
10607 }
10608 
10609 void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
10610   checkForCycles(DAG->getRoot().getNode(), DAG, force);
10611 }
10612