1 //===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This implements the SelectionDAG class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/CodeGen/SelectionDAG.h"
15 #include "SDNodeDbgValue.h"
16 #include "llvm/ADT/APSInt.h"
17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/SmallSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/Analysis/ValueTracking.h"
23 #include "llvm/CodeGen/MachineBasicBlock.h"
24 #include "llvm/CodeGen/MachineConstantPool.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineModuleInfo.h"
27 #include "llvm/CodeGen/SelectionDAGTargetInfo.h"
28 #include "llvm/IR/CallingConv.h"
29 #include "llvm/IR/Constants.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/DebugInfo.h"
32 #include "llvm/IR/DerivedTypes.h"
33 #include "llvm/IR/Function.h"
34 #include "llvm/IR/GlobalAlias.h"
35 #include "llvm/IR/GlobalVariable.h"
36 #include "llvm/IR/Intrinsics.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/ManagedStatic.h"
40 #include "llvm/Support/MathExtras.h"
41 #include "llvm/Support/Mutex.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include "llvm/Target/TargetInstrInfo.h"
44 #include "llvm/Target/TargetIntrinsicInfo.h"
45 #include "llvm/Target/TargetLowering.h"
46 #include "llvm/Target/TargetMachine.h"
47 #include "llvm/Target/TargetOptions.h"
48 #include "llvm/Target/TargetRegisterInfo.h"
49 #include "llvm/Target/TargetSubtargetInfo.h"
50 #include <algorithm>
51 #include <cmath>
52 #include <utility>
53 
54 using namespace llvm;
55 
56 /// makeVTList - Return an instance of the SDVTList struct initialized with the
57 /// specified members.
58 static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
59   SDVTList Res = {VTs, NumVTs};
60   return Res;
61 }
62 
63 // Default null implementations of the callbacks.
64 void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
65 void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
66 
67 //===----------------------------------------------------------------------===//
68 //                              ConstantFPSDNode Class
69 //===----------------------------------------------------------------------===//
70 
71 /// isExactlyValue - We don't rely on operator== working on double values, as
72 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
73 /// As such, this method can be used to do an exact bit-for-bit comparison of
74 /// two floating point values.
75 bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
76   return getValueAPF().bitwiseIsEqual(V);
77 }
78 
79 bool ConstantFPSDNode::isValueValidForType(EVT VT,
80                                            const APFloat& Val) {
81   assert(VT.isFloatingPoint() && "Can only convert between FP types");
82 
83   // convert modifies in place, so make a copy.
84   APFloat Val2 = APFloat(Val);
85   bool losesInfo;
86   (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
87                       APFloat::rmNearestTiesToEven,
88                       &losesInfo);
89   return !losesInfo;
90 }
91 
92 //===----------------------------------------------------------------------===//
93 //                              ISD Namespace
94 //===----------------------------------------------------------------------===//
95 
96 /// isBuildVectorAllOnes - Return true if the specified node is a
97 /// BUILD_VECTOR where all of the elements are ~0 or undef.
98 bool ISD::isBuildVectorAllOnes(const SDNode *N) {
99   // Look through a bit convert.
100   while (N->getOpcode() == ISD::BITCAST)
101     N = N->getOperand(0).getNode();
102 
103   if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
104 
105   unsigned i = 0, e = N->getNumOperands();
106 
107   // Skip over all of the undef values.
108   while (i != e && N->getOperand(i).isUndef())
109     ++i;
110 
111   // Do not accept an all-undef vector.
112   if (i == e) return false;
113 
114   // Do not accept build_vectors that aren't all constants or which have non-~0
115   // elements. We have to be a bit careful here, as the type of the constant
116   // may not be the same as the type of the vector elements due to type
117   // legalization (the elements are promoted to a legal type for the target and
118   // a vector of a type may be legal when the base element type is not).
119   // We only want to check enough bits to cover the vector elements, because
120   // we care if the resultant vector is all ones, not whether the individual
121   // constants are.
122   SDValue NotZero = N->getOperand(i);
123   unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
124   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
125     if (CN->getAPIntValue().countTrailingOnes() < EltSize)
126       return false;
127   } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
128     if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
129       return false;
130   } else
131     return false;
132 
133   // Okay, we have at least one ~0 value, check to see if the rest match or are
134   // undefs. Even with the above element type twiddling, this should be OK, as
135   // the same type legalization should have applied to all the elements.
136   for (++i; i != e; ++i)
137     if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
138       return false;
139   return true;
140 }
141 
142 
143 /// isBuildVectorAllZeros - Return true if the specified node is a
144 /// BUILD_VECTOR where all of the elements are 0 or undef.
145 bool ISD::isBuildVectorAllZeros(const SDNode *N) {
146   // Look through a bit convert.
147   while (N->getOpcode() == ISD::BITCAST)
148     N = N->getOperand(0).getNode();
149 
150   if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
151 
152   bool IsAllUndef = true;
153   for (const SDValue &Op : N->op_values()) {
154     if (Op.isUndef())
155       continue;
156     IsAllUndef = false;
157     // Do not accept build_vectors that aren't all constants or which have non-0
158     // elements. We have to be a bit careful here, as the type of the constant
159     // may not be the same as the type of the vector elements due to type
160     // legalization (the elements are promoted to a legal type for the target
161     // and a vector of a type may be legal when the base element type is not).
162     // We only want to check enough bits to cover the vector elements, because
163     // we care if the resultant vector is all zeros, not whether the individual
164     // constants are.
165     unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
166     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
167       if (CN->getAPIntValue().countTrailingZeros() < EltSize)
168         return false;
169     } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Op)) {
170       if (CFPN->getValueAPF().bitcastToAPInt().countTrailingZeros() < EltSize)
171         return false;
172     } else
173       return false;
174   }
175 
176   // Do not accept an all-undef vector.
177   if (IsAllUndef)
178     return false;
179   return true;
180 }
181 
182 /// \brief Return true if the specified node is a BUILD_VECTOR node of
183 /// all ConstantSDNode or undef.
184 bool ISD::isBuildVectorOfConstantSDNodes(const SDNode *N) {
185   if (N->getOpcode() != ISD::BUILD_VECTOR)
186     return false;
187 
188   for (const SDValue &Op : N->op_values()) {
189     if (Op.isUndef())
190       continue;
191     if (!isa<ConstantSDNode>(Op))
192       return false;
193   }
194   return true;
195 }
196 
197 /// \brief Return true if the specified node is a BUILD_VECTOR node of
198 /// all ConstantFPSDNode or undef.
199 bool ISD::isBuildVectorOfConstantFPSDNodes(const SDNode *N) {
200   if (N->getOpcode() != ISD::BUILD_VECTOR)
201     return false;
202 
203   for (const SDValue &Op : N->op_values()) {
204     if (Op.isUndef())
205       continue;
206     if (!isa<ConstantFPSDNode>(Op))
207       return false;
208   }
209   return true;
210 }
211 
212 /// allOperandsUndef - Return true if the node has at least one operand
213 /// and all operands of the specified node are ISD::UNDEF.
214 bool ISD::allOperandsUndef(const SDNode *N) {
215   // Return false if the node has no operands.
216   // This is "logically inconsistent" with the definition of "all" but
217   // is probably the desired behavior.
218   if (N->getNumOperands() == 0)
219     return false;
220 
221   for (const SDValue &Op : N->op_values())
222     if (!Op.isUndef())
223       return false;
224 
225   return true;
226 }
227 
228 ISD::NodeType ISD::getExtForLoadExtType(bool IsFP, ISD::LoadExtType ExtType) {
229   switch (ExtType) {
230   case ISD::EXTLOAD:
231     return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
232   case ISD::SEXTLOAD:
233     return ISD::SIGN_EXTEND;
234   case ISD::ZEXTLOAD:
235     return ISD::ZERO_EXTEND;
236   default:
237     break;
238   }
239 
240   llvm_unreachable("Invalid LoadExtType");
241 }
242 
243 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
244 /// when given the operation for (X op Y).
245 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
246   // To perform this operation, we just need to swap the L and G bits of the
247   // operation.
248   unsigned OldL = (Operation >> 2) & 1;
249   unsigned OldG = (Operation >> 1) & 1;
250   return ISD::CondCode((Operation & ~6) |  // Keep the N, U, E bits
251                        (OldL << 1) |       // New G bit
252                        (OldG << 2));       // New L bit.
253 }
254 
255 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
256 /// 'op' is a valid SetCC operation.
257 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
258   unsigned Operation = Op;
259   if (isInteger)
260     Operation ^= 7;   // Flip L, G, E bits, but not U.
261   else
262     Operation ^= 15;  // Flip all of the condition bits.
263 
264   if (Operation > ISD::SETTRUE2)
265     Operation &= ~8;  // Don't let N and U bits get set.
266 
267   return ISD::CondCode(Operation);
268 }
269 
270 
271 /// isSignedOp - For an integer comparison, return 1 if the comparison is a
272 /// signed operation and 2 if the result is an unsigned comparison.  Return zero
273 /// if the operation does not depend on the sign of the input (setne and seteq).
274 static int isSignedOp(ISD::CondCode Opcode) {
275   switch (Opcode) {
276   default: llvm_unreachable("Illegal integer setcc operation!");
277   case ISD::SETEQ:
278   case ISD::SETNE: return 0;
279   case ISD::SETLT:
280   case ISD::SETLE:
281   case ISD::SETGT:
282   case ISD::SETGE: return 1;
283   case ISD::SETULT:
284   case ISD::SETULE:
285   case ISD::SETUGT:
286   case ISD::SETUGE: return 2;
287   }
288 }
289 
290 /// getSetCCOrOperation - Return the result of a logical OR between different
291 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)).  This function
292 /// returns SETCC_INVALID if it is not possible to represent the resultant
293 /// comparison.
294 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
295                                        bool isInteger) {
296   if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
297     // Cannot fold a signed integer setcc with an unsigned integer setcc.
298     return ISD::SETCC_INVALID;
299 
300   unsigned Op = Op1 | Op2;  // Combine all of the condition bits.
301 
302   // If the N and U bits get set then the resultant comparison DOES suddenly
303   // care about orderedness, and is true when ordered.
304   if (Op > ISD::SETTRUE2)
305     Op &= ~16;     // Clear the U bit if the N bit is set.
306 
307   // Canonicalize illegal integer setcc's.
308   if (isInteger && Op == ISD::SETUNE)  // e.g. SETUGT | SETULT
309     Op = ISD::SETNE;
310 
311   return ISD::CondCode(Op);
312 }
313 
314 /// getSetCCAndOperation - Return the result of a logical AND between different
315 /// comparisons of identical values: ((X op1 Y) & (X op2 Y)).  This
316 /// function returns zero if it is not possible to represent the resultant
317 /// comparison.
318 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
319                                         bool isInteger) {
320   if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
321     // Cannot fold a signed setcc with an unsigned setcc.
322     return ISD::SETCC_INVALID;
323 
324   // Combine all of the condition bits.
325   ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
326 
327   // Canonicalize illegal integer setcc's.
328   if (isInteger) {
329     switch (Result) {
330     default: break;
331     case ISD::SETUO : Result = ISD::SETFALSE; break;  // SETUGT & SETULT
332     case ISD::SETOEQ:                                 // SETEQ  & SETU[LG]E
333     case ISD::SETUEQ: Result = ISD::SETEQ   ; break;  // SETUGE & SETULE
334     case ISD::SETOLT: Result = ISD::SETULT  ; break;  // SETULT & SETNE
335     case ISD::SETOGT: Result = ISD::SETUGT  ; break;  // SETUGT & SETNE
336     }
337   }
338 
339   return Result;
340 }
341 
342 //===----------------------------------------------------------------------===//
343 //                           SDNode Profile Support
344 //===----------------------------------------------------------------------===//
345 
346 /// AddNodeIDOpcode - Add the node opcode to the NodeID data.
347 ///
348 static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC)  {
349   ID.AddInteger(OpC);
350 }
351 
352 /// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
353 /// solely with their pointer.
354 static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
355   ID.AddPointer(VTList.VTs);
356 }
357 
358 /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
359 ///
360 static void AddNodeIDOperands(FoldingSetNodeID &ID,
361                               ArrayRef<SDValue> Ops) {
362   for (auto& Op : Ops) {
363     ID.AddPointer(Op.getNode());
364     ID.AddInteger(Op.getResNo());
365   }
366 }
367 
368 /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
369 ///
370 static void AddNodeIDOperands(FoldingSetNodeID &ID,
371                               ArrayRef<SDUse> Ops) {
372   for (auto& Op : Ops) {
373     ID.AddPointer(Op.getNode());
374     ID.AddInteger(Op.getResNo());
375   }
376 }
377 
378 static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned short OpC,
379                           SDVTList VTList, ArrayRef<SDValue> OpList) {
380   AddNodeIDOpcode(ID, OpC);
381   AddNodeIDValueTypes(ID, VTList);
382   AddNodeIDOperands(ID, OpList);
383 }
384 
385 /// If this is an SDNode with special info, add this info to the NodeID data.
386 static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
387   switch (N->getOpcode()) {
388   case ISD::TargetExternalSymbol:
389   case ISD::ExternalSymbol:
390   case ISD::MCSymbol:
391     llvm_unreachable("Should only be used on nodes with operands");
392   default: break;  // Normal nodes don't need extra info.
393   case ISD::TargetConstant:
394   case ISD::Constant: {
395     const ConstantSDNode *C = cast<ConstantSDNode>(N);
396     ID.AddPointer(C->getConstantIntValue());
397     ID.AddBoolean(C->isOpaque());
398     break;
399   }
400   case ISD::TargetConstantFP:
401   case ISD::ConstantFP: {
402     ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
403     break;
404   }
405   case ISD::TargetGlobalAddress:
406   case ISD::GlobalAddress:
407   case ISD::TargetGlobalTLSAddress:
408   case ISD::GlobalTLSAddress: {
409     const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
410     ID.AddPointer(GA->getGlobal());
411     ID.AddInteger(GA->getOffset());
412     ID.AddInteger(GA->getTargetFlags());
413     ID.AddInteger(GA->getAddressSpace());
414     break;
415   }
416   case ISD::BasicBlock:
417     ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
418     break;
419   case ISD::Register:
420     ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
421     break;
422   case ISD::RegisterMask:
423     ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
424     break;
425   case ISD::SRCVALUE:
426     ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
427     break;
428   case ISD::FrameIndex:
429   case ISD::TargetFrameIndex:
430     ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
431     break;
432   case ISD::JumpTable:
433   case ISD::TargetJumpTable:
434     ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
435     ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
436     break;
437   case ISD::ConstantPool:
438   case ISD::TargetConstantPool: {
439     const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
440     ID.AddInteger(CP->getAlignment());
441     ID.AddInteger(CP->getOffset());
442     if (CP->isMachineConstantPoolEntry())
443       CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
444     else
445       ID.AddPointer(CP->getConstVal());
446     ID.AddInteger(CP->getTargetFlags());
447     break;
448   }
449   case ISD::TargetIndex: {
450     const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
451     ID.AddInteger(TI->getIndex());
452     ID.AddInteger(TI->getOffset());
453     ID.AddInteger(TI->getTargetFlags());
454     break;
455   }
456   case ISD::LOAD: {
457     const LoadSDNode *LD = cast<LoadSDNode>(N);
458     ID.AddInteger(LD->getMemoryVT().getRawBits());
459     ID.AddInteger(LD->getRawSubclassData());
460     ID.AddInteger(LD->getPointerInfo().getAddrSpace());
461     break;
462   }
463   case ISD::STORE: {
464     const StoreSDNode *ST = cast<StoreSDNode>(N);
465     ID.AddInteger(ST->getMemoryVT().getRawBits());
466     ID.AddInteger(ST->getRawSubclassData());
467     ID.AddInteger(ST->getPointerInfo().getAddrSpace());
468     break;
469   }
470   case ISD::ATOMIC_CMP_SWAP:
471   case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
472   case ISD::ATOMIC_SWAP:
473   case ISD::ATOMIC_LOAD_ADD:
474   case ISD::ATOMIC_LOAD_SUB:
475   case ISD::ATOMIC_LOAD_AND:
476   case ISD::ATOMIC_LOAD_OR:
477   case ISD::ATOMIC_LOAD_XOR:
478   case ISD::ATOMIC_LOAD_NAND:
479   case ISD::ATOMIC_LOAD_MIN:
480   case ISD::ATOMIC_LOAD_MAX:
481   case ISD::ATOMIC_LOAD_UMIN:
482   case ISD::ATOMIC_LOAD_UMAX:
483   case ISD::ATOMIC_LOAD:
484   case ISD::ATOMIC_STORE: {
485     const AtomicSDNode *AT = cast<AtomicSDNode>(N);
486     ID.AddInteger(AT->getMemoryVT().getRawBits());
487     ID.AddInteger(AT->getRawSubclassData());
488     ID.AddInteger(AT->getPointerInfo().getAddrSpace());
489     break;
490   }
491   case ISD::PREFETCH: {
492     const MemSDNode *PF = cast<MemSDNode>(N);
493     ID.AddInteger(PF->getPointerInfo().getAddrSpace());
494     break;
495   }
496   case ISD::VECTOR_SHUFFLE: {
497     const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
498     for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
499          i != e; ++i)
500       ID.AddInteger(SVN->getMaskElt(i));
501     break;
502   }
503   case ISD::TargetBlockAddress:
504   case ISD::BlockAddress: {
505     const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
506     ID.AddPointer(BA->getBlockAddress());
507     ID.AddInteger(BA->getOffset());
508     ID.AddInteger(BA->getTargetFlags());
509     break;
510   }
511   } // end switch (N->getOpcode())
512 
513   // Target specific memory nodes could also have address spaces to check.
514   if (N->isTargetMemoryOpcode())
515     ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
516 }
517 
518 /// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
519 /// data.
520 static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
521   AddNodeIDOpcode(ID, N->getOpcode());
522   // Add the return value info.
523   AddNodeIDValueTypes(ID, N->getVTList());
524   // Add the operand info.
525   AddNodeIDOperands(ID, N->ops());
526 
527   // Handle SDNode leafs with special info.
528   AddNodeIDCustom(ID, N);
529 }
530 
531 /// encodeMemSDNodeFlags - Generic routine for computing a value for use in
532 /// the CSE map that carries volatility, temporalness, indexing mode, and
533 /// extension/truncation information.
534 ///
535 static inline unsigned
536 encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
537                      bool isNonTemporal, bool isInvariant) {
538   assert((ConvType & 3) == ConvType &&
539          "ConvType may not require more than 2 bits!");
540   assert((AM & 7) == AM &&
541          "AM may not require more than 3 bits!");
542   return ConvType |
543          (AM << 2) |
544          (isVolatile << 5) |
545          (isNonTemporal << 6) |
546          (isInvariant << 7);
547 }
548 
549 //===----------------------------------------------------------------------===//
550 //                              SelectionDAG Class
551 //===----------------------------------------------------------------------===//
552 
553 /// doNotCSE - Return true if CSE should not be performed for this node.
554 static bool doNotCSE(SDNode *N) {
555   if (N->getValueType(0) == MVT::Glue)
556     return true; // Never CSE anything that produces a flag.
557 
558   switch (N->getOpcode()) {
559   default: break;
560   case ISD::HANDLENODE:
561   case ISD::EH_LABEL:
562     return true;   // Never CSE these nodes.
563   }
564 
565   // Check that remaining values produced are not flags.
566   for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
567     if (N->getValueType(i) == MVT::Glue)
568       return true; // Never CSE anything that produces a flag.
569 
570   return false;
571 }
572 
573 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
574 /// SelectionDAG.
575 void SelectionDAG::RemoveDeadNodes() {
576   // Create a dummy node (which is not added to allnodes), that adds a reference
577   // to the root node, preventing it from being deleted.
578   HandleSDNode Dummy(getRoot());
579 
580   SmallVector<SDNode*, 128> DeadNodes;
581 
582   // Add all obviously-dead nodes to the DeadNodes worklist.
583   for (SDNode &Node : allnodes())
584     if (Node.use_empty())
585       DeadNodes.push_back(&Node);
586 
587   RemoveDeadNodes(DeadNodes);
588 
589   // If the root changed (e.g. it was a dead load, update the root).
590   setRoot(Dummy.getValue());
591 }
592 
593 /// RemoveDeadNodes - This method deletes the unreachable nodes in the
594 /// given list, and any nodes that become unreachable as a result.
595 void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
596 
597   // Process the worklist, deleting the nodes and adding their uses to the
598   // worklist.
599   while (!DeadNodes.empty()) {
600     SDNode *N = DeadNodes.pop_back_val();
601 
602     for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
603       DUL->NodeDeleted(N, nullptr);
604 
605     // Take the node out of the appropriate CSE map.
606     RemoveNodeFromCSEMaps(N);
607 
608     // Next, brutally remove the operand list.  This is safe to do, as there are
609     // no cycles in the graph.
610     for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
611       SDUse &Use = *I++;
612       SDNode *Operand = Use.getNode();
613       Use.set(SDValue());
614 
615       // Now that we removed this operand, see if there are no uses of it left.
616       if (Operand->use_empty())
617         DeadNodes.push_back(Operand);
618     }
619 
620     DeallocateNode(N);
621   }
622 }
623 
624 void SelectionDAG::RemoveDeadNode(SDNode *N){
625   SmallVector<SDNode*, 16> DeadNodes(1, N);
626 
627   // Create a dummy node that adds a reference to the root node, preventing
628   // it from being deleted.  (This matters if the root is an operand of the
629   // dead node.)
630   HandleSDNode Dummy(getRoot());
631 
632   RemoveDeadNodes(DeadNodes);
633 }
634 
635 void SelectionDAG::DeleteNode(SDNode *N) {
636   // First take this out of the appropriate CSE map.
637   RemoveNodeFromCSEMaps(N);
638 
639   // Finally, remove uses due to operands of this node, remove from the
640   // AllNodes list, and delete the node.
641   DeleteNodeNotInCSEMaps(N);
642 }
643 
644 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
645   assert(N->getIterator() != AllNodes.begin() &&
646          "Cannot delete the entry node!");
647   assert(N->use_empty() && "Cannot delete a node that is not dead!");
648 
649   // Drop all of the operands and decrement used node's use counts.
650   N->DropOperands();
651 
652   DeallocateNode(N);
653 }
654 
655 void SDDbgInfo::erase(const SDNode *Node) {
656   DbgValMapType::iterator I = DbgValMap.find(Node);
657   if (I == DbgValMap.end())
658     return;
659   for (auto &Val: I->second)
660     Val->setIsInvalidated();
661   DbgValMap.erase(I);
662 }
663 
664 void SelectionDAG::DeallocateNode(SDNode *N) {
665   // If we have operands, deallocate them.
666   removeOperands(N);
667 
668   // Set the opcode to DELETED_NODE to help catch bugs when node
669   // memory is reallocated.
670   N->NodeType = ISD::DELETED_NODE;
671 
672   NodeAllocator.Deallocate(AllNodes.remove(N));
673 
674   // If any of the SDDbgValue nodes refer to this SDNode, invalidate
675   // them and forget about that node.
676   DbgInfo->erase(N);
677 }
678 
679 #ifndef NDEBUG
680 /// VerifySDNode - Sanity check the given SDNode.  Aborts if it is invalid.
681 static void VerifySDNode(SDNode *N) {
682   switch (N->getOpcode()) {
683   default:
684     break;
685   case ISD::BUILD_PAIR: {
686     EVT VT = N->getValueType(0);
687     assert(N->getNumValues() == 1 && "Too many results!");
688     assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
689            "Wrong return type!");
690     assert(N->getNumOperands() == 2 && "Wrong number of operands!");
691     assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
692            "Mismatched operand types!");
693     assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
694            "Wrong operand type!");
695     assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
696            "Wrong return type size");
697     break;
698   }
699   case ISD::BUILD_VECTOR: {
700     assert(N->getNumValues() == 1 && "Too many results!");
701     assert(N->getValueType(0).isVector() && "Wrong return type!");
702     assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
703            "Wrong number of operands!");
704     EVT EltVT = N->getValueType(0).getVectorElementType();
705     for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
706       assert((I->getValueType() == EltVT ||
707              (EltVT.isInteger() && I->getValueType().isInteger() &&
708               EltVT.bitsLE(I->getValueType()))) &&
709             "Wrong operand type!");
710       assert(I->getValueType() == N->getOperand(0).getValueType() &&
711              "Operands must all have the same type");
712     }
713     break;
714   }
715   }
716 }
717 #endif // NDEBUG
718 
719 /// \brief Insert a newly allocated node into the DAG.
720 ///
721 /// Handles insertion into the all nodes list and CSE map, as well as
722 /// verification and other common operations when a new node is allocated.
723 void SelectionDAG::InsertNode(SDNode *N) {
724   AllNodes.push_back(N);
725 #ifndef NDEBUG
726   N->PersistentId = NextPersistentId++;
727   VerifySDNode(N);
728 #endif
729 }
730 
731 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
732 /// correspond to it.  This is useful when we're about to delete or repurpose
733 /// the node.  We don't want future request for structurally identical nodes
734 /// to return N anymore.
735 bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
736   bool Erased = false;
737   switch (N->getOpcode()) {
738   case ISD::HANDLENODE: return false;  // noop.
739   case ISD::CONDCODE:
740     assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
741            "Cond code doesn't exist!");
742     Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
743     CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
744     break;
745   case ISD::ExternalSymbol:
746     Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
747     break;
748   case ISD::TargetExternalSymbol: {
749     ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
750     Erased = TargetExternalSymbols.erase(
751                std::pair<std::string,unsigned char>(ESN->getSymbol(),
752                                                     ESN->getTargetFlags()));
753     break;
754   }
755   case ISD::MCSymbol: {
756     auto *MCSN = cast<MCSymbolSDNode>(N);
757     Erased = MCSymbols.erase(MCSN->getMCSymbol());
758     break;
759   }
760   case ISD::VALUETYPE: {
761     EVT VT = cast<VTSDNode>(N)->getVT();
762     if (VT.isExtended()) {
763       Erased = ExtendedValueTypeNodes.erase(VT);
764     } else {
765       Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
766       ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
767     }
768     break;
769   }
770   default:
771     // Remove it from the CSE Map.
772     assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
773     assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
774     Erased = CSEMap.RemoveNode(N);
775     break;
776   }
777 #ifndef NDEBUG
778   // Verify that the node was actually in one of the CSE maps, unless it has a
779   // flag result (which cannot be CSE'd) or is one of the special cases that are
780   // not subject to CSE.
781   if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
782       !N->isMachineOpcode() && !doNotCSE(N)) {
783     N->dump(this);
784     dbgs() << "\n";
785     llvm_unreachable("Node is not in map!");
786   }
787 #endif
788   return Erased;
789 }
790 
791 /// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
792 /// maps and modified in place. Add it back to the CSE maps, unless an identical
793 /// node already exists, in which case transfer all its users to the existing
794 /// node. This transfer can potentially trigger recursive merging.
795 ///
796 void
797 SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
798   // For node types that aren't CSE'd, just act as if no identical node
799   // already exists.
800   if (!doNotCSE(N)) {
801     SDNode *Existing = CSEMap.GetOrInsertNode(N);
802     if (Existing != N) {
803       // If there was already an existing matching node, use ReplaceAllUsesWith
804       // to replace the dead one with the existing one.  This can cause
805       // recursive merging of other unrelated nodes down the line.
806       ReplaceAllUsesWith(N, Existing);
807 
808       // N is now dead. Inform the listeners and delete it.
809       for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
810         DUL->NodeDeleted(N, Existing);
811       DeleteNodeNotInCSEMaps(N);
812       return;
813     }
814   }
815 
816   // If the node doesn't already exist, we updated it.  Inform listeners.
817   for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
818     DUL->NodeUpdated(N);
819 }
820 
821 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
822 /// were replaced with those specified.  If this node is never memoized,
823 /// return null, otherwise return a pointer to the slot it would take.  If a
824 /// node already exists with these operands, the slot will be non-null.
825 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
826                                            void *&InsertPos) {
827   if (doNotCSE(N))
828     return nullptr;
829 
830   SDValue Ops[] = { Op };
831   FoldingSetNodeID ID;
832   AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
833   AddNodeIDCustom(ID, N);
834   SDNode *Node = FindNodeOrInsertPos(ID, N->getDebugLoc(), InsertPos);
835   if (Node)
836     if (const SDNodeFlags *Flags = N->getFlags())
837       Node->intersectFlagsWith(Flags);
838   return Node;
839 }
840 
841 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
842 /// were replaced with those specified.  If this node is never memoized,
843 /// return null, otherwise return a pointer to the slot it would take.  If a
844 /// node already exists with these operands, the slot will be non-null.
845 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
846                                            SDValue Op1, SDValue Op2,
847                                            void *&InsertPos) {
848   if (doNotCSE(N))
849     return nullptr;
850 
851   SDValue Ops[] = { Op1, Op2 };
852   FoldingSetNodeID ID;
853   AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
854   AddNodeIDCustom(ID, N);
855   SDNode *Node = FindNodeOrInsertPos(ID, N->getDebugLoc(), InsertPos);
856   if (Node)
857     if (const SDNodeFlags *Flags = N->getFlags())
858       Node->intersectFlagsWith(Flags);
859   return Node;
860 }
861 
862 
863 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
864 /// were replaced with those specified.  If this node is never memoized,
865 /// return null, otherwise return a pointer to the slot it would take.  If a
866 /// node already exists with these operands, the slot will be non-null.
867 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
868                                            void *&InsertPos) {
869   if (doNotCSE(N))
870     return nullptr;
871 
872   FoldingSetNodeID ID;
873   AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
874   AddNodeIDCustom(ID, N);
875   SDNode *Node = FindNodeOrInsertPos(ID, N->getDebugLoc(), InsertPos);
876   if (Node)
877     if (const SDNodeFlags *Flags = N->getFlags())
878       Node->intersectFlagsWith(Flags);
879   return Node;
880 }
881 
882 /// getEVTAlignment - Compute the default alignment value for the
883 /// given type.
884 ///
885 unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
886   Type *Ty = VT == MVT::iPTR ?
887                    PointerType::get(Type::getInt8Ty(*getContext()), 0) :
888                    VT.getTypeForEVT(*getContext());
889 
890   return getDataLayout().getABITypeAlignment(Ty);
891 }
892 
893 // EntryNode could meaningfully have debug info if we can find it...
894 SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
895     : TM(tm), TSI(nullptr), TLI(nullptr), OptLevel(OL),
896       EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
897       Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
898       UpdateListeners(nullptr) {
899   InsertNode(&EntryNode);
900   DbgInfo = new SDDbgInfo();
901 }
902 
903 void SelectionDAG::init(MachineFunction &mf) {
904   MF = &mf;
905   TLI = getSubtarget().getTargetLowering();
906   TSI = getSubtarget().getSelectionDAGInfo();
907   Context = &mf.getFunction()->getContext();
908 }
909 
910 SelectionDAG::~SelectionDAG() {
911   assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
912   allnodes_clear();
913   OperandRecycler.clear(OperandAllocator);
914   delete DbgInfo;
915 }
916 
917 void SelectionDAG::allnodes_clear() {
918   assert(&*AllNodes.begin() == &EntryNode);
919   AllNodes.remove(AllNodes.begin());
920   while (!AllNodes.empty())
921     DeallocateNode(&AllNodes.front());
922 #ifndef NDEBUG
923   NextPersistentId = 0;
924 #endif
925 }
926 
927 SDNode *SelectionDAG::GetBinarySDNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
928                                       SDValue N1, SDValue N2,
929                                       const SDNodeFlags *Flags) {
930   SDValue Ops[] = {N1, N2};
931 
932   if (isBinOpWithFlags(Opcode)) {
933     // If no flags were passed in, use a default flags object.
934     SDNodeFlags F;
935     if (Flags == nullptr)
936       Flags = &F;
937 
938     auto *FN = newSDNode<BinaryWithFlagsSDNode>(Opcode, DL.getIROrder(),
939                                                 DL.getDebugLoc(), VTs, *Flags);
940     createOperands(FN, Ops);
941 
942     return FN;
943   }
944 
945   auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
946   createOperands(N, Ops);
947   return N;
948 }
949 
950 SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
951                                           void *&InsertPos) {
952   SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
953   if (N) {
954     switch (N->getOpcode()) {
955     default: break;
956     case ISD::Constant:
957     case ISD::ConstantFP:
958       llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
959                        "debug location.  Use another overload.");
960     }
961   }
962   return N;
963 }
964 
965 SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
966                                           DebugLoc DL, void *&InsertPos) {
967   SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
968   if (N) {
969     switch (N->getOpcode()) {
970     default: break; // Process only regular (non-target) constant nodes.
971     case ISD::Constant:
972     case ISD::ConstantFP:
973       // Erase debug location from the node if the node is used at several
974       // different places to do not propagate one location to all uses as it
975       // leads to incorrect debug info.
976       if (N->getDebugLoc() != DL)
977         N->setDebugLoc(DebugLoc());
978       break;
979     }
980   }
981   return N;
982 }
983 
984 void SelectionDAG::clear() {
985   allnodes_clear();
986   OperandRecycler.clear(OperandAllocator);
987   OperandAllocator.Reset();
988   CSEMap.clear();
989 
990   ExtendedValueTypeNodes.clear();
991   ExternalSymbols.clear();
992   TargetExternalSymbols.clear();
993   MCSymbols.clear();
994   std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
995             static_cast<CondCodeSDNode*>(nullptr));
996   std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
997             static_cast<SDNode*>(nullptr));
998 
999   EntryNode.UseList = nullptr;
1000   InsertNode(&EntryNode);
1001   Root = getEntryNode();
1002   DbgInfo->clear();
1003 }
1004 
1005 SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
1006   return VT.bitsGT(Op.getValueType()) ?
1007     getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1008     getNode(ISD::TRUNCATE, DL, VT, Op);
1009 }
1010 
1011 SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
1012   return VT.bitsGT(Op.getValueType()) ?
1013     getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1014     getNode(ISD::TRUNCATE, DL, VT, Op);
1015 }
1016 
1017 SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
1018   return VT.bitsGT(Op.getValueType()) ?
1019     getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1020     getNode(ISD::TRUNCATE, DL, VT, Op);
1021 }
1022 
1023 SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, SDLoc SL, EVT VT,
1024                                         EVT OpVT) {
1025   if (VT.bitsLE(Op.getValueType()))
1026     return getNode(ISD::TRUNCATE, SL, VT, Op);
1027 
1028   TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
1029   return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1030 }
1031 
1032 SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
1033   assert(!VT.isVector() &&
1034          "getZeroExtendInReg should use the vector element type instead of "
1035          "the vector type!");
1036   if (Op.getValueType() == VT) return Op;
1037   unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
1038   APInt Imm = APInt::getLowBitsSet(BitWidth,
1039                                    VT.getSizeInBits());
1040   return getNode(ISD::AND, DL, Op.getValueType(), Op,
1041                  getConstant(Imm, DL, Op.getValueType()));
1042 }
1043 
1044 SDValue SelectionDAG::getAnyExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1045   assert(VT.isVector() && "This DAG node is restricted to vector types.");
1046   assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1047          "The sizes of the input and result must match in order to perform the "
1048          "extend in-register.");
1049   assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1050          "The destination vector type must have fewer lanes than the input.");
1051   return getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Op);
1052 }
1053 
1054 SDValue SelectionDAG::getSignExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1055   assert(VT.isVector() && "This DAG node is restricted to vector types.");
1056   assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1057          "The sizes of the input and result must match in order to perform the "
1058          "extend in-register.");
1059   assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1060          "The destination vector type must have fewer lanes than the input.");
1061   return getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, VT, Op);
1062 }
1063 
1064 SDValue SelectionDAG::getZeroExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1065   assert(VT.isVector() && "This DAG node is restricted to vector types.");
1066   assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1067          "The sizes of the input and result must match in order to perform the "
1068          "extend in-register.");
1069   assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1070          "The destination vector type must have fewer lanes than the input.");
1071   return getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, VT, Op);
1072 }
1073 
1074 /// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1075 ///
1076 SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
1077   EVT EltVT = VT.getScalarType();
1078   SDValue NegOne =
1079     getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL, VT);
1080   return getNode(ISD::XOR, DL, VT, Val, NegOne);
1081 }
1082 
1083 SDValue SelectionDAG::getLogicalNOT(SDLoc DL, SDValue Val, EVT VT) {
1084   EVT EltVT = VT.getScalarType();
1085   SDValue TrueValue;
1086   switch (TLI->getBooleanContents(VT)) {
1087     case TargetLowering::ZeroOrOneBooleanContent:
1088     case TargetLowering::UndefinedBooleanContent:
1089       TrueValue = getConstant(1, DL, VT);
1090       break;
1091     case TargetLowering::ZeroOrNegativeOneBooleanContent:
1092       TrueValue = getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL,
1093                               VT);
1094       break;
1095   }
1096   return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1097 }
1098 
1099 SDValue SelectionDAG::getConstant(uint64_t Val, SDLoc DL, EVT VT, bool isT,
1100                                   bool isO) {
1101   EVT EltVT = VT.getScalarType();
1102   assert((EltVT.getSizeInBits() >= 64 ||
1103          (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1104          "getConstant with a uint64_t value that doesn't fit in the type!");
1105   return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO);
1106 }
1107 
1108 SDValue SelectionDAG::getConstant(const APInt &Val, SDLoc DL, EVT VT, bool isT,
1109                                   bool isO) {
1110   return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1111 }
1112 
1113 SDValue SelectionDAG::getConstant(const ConstantInt &Val, SDLoc DL, EVT VT,
1114                                   bool isT, bool isO) {
1115   assert(VT.isInteger() && "Cannot create FP integer constant!");
1116 
1117   EVT EltVT = VT.getScalarType();
1118   const ConstantInt *Elt = &Val;
1119 
1120   // In some cases the vector type is legal but the element type is illegal and
1121   // needs to be promoted, for example v8i8 on ARM.  In this case, promote the
1122   // inserted value (the type does not need to match the vector element type).
1123   // Any extra bits introduced will be truncated away.
1124   if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1125       TargetLowering::TypePromoteInteger) {
1126    EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1127    APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1128    Elt = ConstantInt::get(*getContext(), NewVal);
1129   }
1130   // In other cases the element type is illegal and needs to be expanded, for
1131   // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1132   // the value into n parts and use a vector type with n-times the elements.
1133   // Then bitcast to the type requested.
1134   // Legalizing constants too early makes the DAGCombiner's job harder so we
1135   // only legalize if the DAG tells us we must produce legal types.
1136   else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1137            TLI->getTypeAction(*getContext(), EltVT) ==
1138            TargetLowering::TypeExpandInteger) {
1139     APInt NewVal = Elt->getValue();
1140     EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1141     unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1142     unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1143     EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1144 
1145     // Check the temporary vector is the correct size. If this fails then
1146     // getTypeToTransformTo() probably returned a type whose size (in bits)
1147     // isn't a power-of-2 factor of the requested type size.
1148     assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1149 
1150     SmallVector<SDValue, 2> EltParts;
1151     for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1152       EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1153                                            .trunc(ViaEltSizeInBits), DL,
1154                                      ViaEltVT, isT, isO));
1155     }
1156 
1157     // EltParts is currently in little endian order. If we actually want
1158     // big-endian order then reverse it now.
1159     if (getDataLayout().isBigEndian())
1160       std::reverse(EltParts.begin(), EltParts.end());
1161 
1162     // The elements must be reversed when the element order is different
1163     // to the endianness of the elements (because the BITCAST is itself a
1164     // vector shuffle in this situation). However, we do not need any code to
1165     // perform this reversal because getConstant() is producing a vector
1166     // splat.
1167     // This situation occurs in MIPS MSA.
1168 
1169     SmallVector<SDValue, 8> Ops;
1170     for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1171       Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1172 
1173     SDValue Result = getNode(ISD::BITCAST, DL, VT,
1174                              getNode(ISD::BUILD_VECTOR, DL, ViaVecVT, Ops));
1175     return Result;
1176   }
1177 
1178   assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1179          "APInt size does not match type size!");
1180   unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1181   FoldingSetNodeID ID;
1182   AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
1183   ID.AddPointer(Elt);
1184   ID.AddBoolean(isO);
1185   void *IP = nullptr;
1186   SDNode *N = nullptr;
1187   if ((N = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP)))
1188     if (!VT.isVector())
1189       return SDValue(N, 0);
1190 
1191   if (!N) {
1192     N = newSDNode<ConstantSDNode>(isT, isO, Elt, DL.getDebugLoc(), EltVT);
1193     CSEMap.InsertNode(N, IP);
1194     InsertNode(N);
1195   }
1196 
1197   SDValue Result(N, 0);
1198   if (VT.isVector())
1199     Result = getSplatBuildVector(VT, DL, Result);
1200   return Result;
1201 }
1202 
1203 SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, SDLoc DL, bool isTarget) {
1204   return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1205 }
1206 
1207 SDValue SelectionDAG::getConstantFP(const APFloat& V, SDLoc DL, EVT VT,
1208                                     bool isTarget) {
1209   return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1210 }
1211 
1212 SDValue SelectionDAG::getConstantFP(const ConstantFP& V, SDLoc DL, EVT VT,
1213                                     bool isTarget){
1214   assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1215 
1216   EVT EltVT = VT.getScalarType();
1217 
1218   // Do the map lookup using the actual bit pattern for the floating point
1219   // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1220   // we don't have issues with SNANs.
1221   unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1222   FoldingSetNodeID ID;
1223   AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
1224   ID.AddPointer(&V);
1225   void *IP = nullptr;
1226   SDNode *N = nullptr;
1227   if ((N = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP)))
1228     if (!VT.isVector())
1229       return SDValue(N, 0);
1230 
1231   if (!N) {
1232     N = newSDNode<ConstantFPSDNode>(isTarget, &V, DL.getDebugLoc(), EltVT);
1233     CSEMap.InsertNode(N, IP);
1234     InsertNode(N);
1235   }
1236 
1237   SDValue Result(N, 0);
1238   if (VT.isVector())
1239     Result = getSplatBuildVector(VT, DL, Result);
1240   return Result;
1241 }
1242 
1243 SDValue SelectionDAG::getConstantFP(double Val, SDLoc DL, EVT VT,
1244                                     bool isTarget) {
1245   EVT EltVT = VT.getScalarType();
1246   if (EltVT == MVT::f32)
1247     return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1248   else if (EltVT == MVT::f64)
1249     return getConstantFP(APFloat(Val), DL, VT, isTarget);
1250   else if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1251            EltVT == MVT::f16) {
1252     bool Ignored;
1253     APFloat APF = APFloat(Val);
1254     APF.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
1255                 &Ignored);
1256     return getConstantFP(APF, DL, VT, isTarget);
1257   } else
1258     llvm_unreachable("Unsupported type in getConstantFP");
1259 }
1260 
1261 SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
1262                                        EVT VT, int64_t Offset,
1263                                        bool isTargetGA,
1264                                        unsigned char TargetFlags) {
1265   assert((TargetFlags == 0 || isTargetGA) &&
1266          "Cannot set target flags on target-independent globals");
1267 
1268   // Truncate (with sign-extension) the offset value to the pointer size.
1269   unsigned BitWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
1270   if (BitWidth < 64)
1271     Offset = SignExtend64(Offset, BitWidth);
1272 
1273   unsigned Opc;
1274   if (GV->isThreadLocal())
1275     Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1276   else
1277     Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
1278 
1279   FoldingSetNodeID ID;
1280   AddNodeIDNode(ID, Opc, getVTList(VT), None);
1281   ID.AddPointer(GV);
1282   ID.AddInteger(Offset);
1283   ID.AddInteger(TargetFlags);
1284   ID.AddInteger(GV->getType()->getAddressSpace());
1285   void *IP = nullptr;
1286   if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP))
1287     return SDValue(E, 0);
1288 
1289   auto *N = newSDNode<GlobalAddressSDNode>(
1290       Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VT, Offset, TargetFlags);
1291   CSEMap.InsertNode(N, IP);
1292     InsertNode(N);
1293   return SDValue(N, 0);
1294 }
1295 
1296 SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1297   unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1298   FoldingSetNodeID ID;
1299   AddNodeIDNode(ID, Opc, getVTList(VT), None);
1300   ID.AddInteger(FI);
1301   void *IP = nullptr;
1302   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1303     return SDValue(E, 0);
1304 
1305   auto *N = newSDNode<FrameIndexSDNode>(FI, VT, isTarget);
1306   CSEMap.InsertNode(N, IP);
1307   InsertNode(N);
1308   return SDValue(N, 0);
1309 }
1310 
1311 SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1312                                    unsigned char TargetFlags) {
1313   assert((TargetFlags == 0 || isTarget) &&
1314          "Cannot set target flags on target-independent jump tables");
1315   unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1316   FoldingSetNodeID ID;
1317   AddNodeIDNode(ID, Opc, getVTList(VT), None);
1318   ID.AddInteger(JTI);
1319   ID.AddInteger(TargetFlags);
1320   void *IP = nullptr;
1321   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1322     return SDValue(E, 0);
1323 
1324   auto *N = newSDNode<JumpTableSDNode>(JTI, VT, isTarget, TargetFlags);
1325   CSEMap.InsertNode(N, IP);
1326   InsertNode(N);
1327   return SDValue(N, 0);
1328 }
1329 
1330 SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
1331                                       unsigned Alignment, int Offset,
1332                                       bool isTarget,
1333                                       unsigned char TargetFlags) {
1334   assert((TargetFlags == 0 || isTarget) &&
1335          "Cannot set target flags on target-independent globals");
1336   if (Alignment == 0)
1337     Alignment = getDataLayout().getPrefTypeAlignment(C->getType());
1338   unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1339   FoldingSetNodeID ID;
1340   AddNodeIDNode(ID, Opc, getVTList(VT), None);
1341   ID.AddInteger(Alignment);
1342   ID.AddInteger(Offset);
1343   ID.AddPointer(C);
1344   ID.AddInteger(TargetFlags);
1345   void *IP = nullptr;
1346   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1347     return SDValue(E, 0);
1348 
1349   auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VT, Offset, Alignment,
1350                                           TargetFlags);
1351   CSEMap.InsertNode(N, IP);
1352   InsertNode(N);
1353   return SDValue(N, 0);
1354 }
1355 
1356 
1357 SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
1358                                       unsigned Alignment, int Offset,
1359                                       bool isTarget,
1360                                       unsigned char TargetFlags) {
1361   assert((TargetFlags == 0 || isTarget) &&
1362          "Cannot set target flags on target-independent globals");
1363   if (Alignment == 0)
1364     Alignment = getDataLayout().getPrefTypeAlignment(C->getType());
1365   unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1366   FoldingSetNodeID ID;
1367   AddNodeIDNode(ID, Opc, getVTList(VT), None);
1368   ID.AddInteger(Alignment);
1369   ID.AddInteger(Offset);
1370   C->addSelectionDAGCSEId(ID);
1371   ID.AddInteger(TargetFlags);
1372   void *IP = nullptr;
1373   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1374     return SDValue(E, 0);
1375 
1376   auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VT, Offset, Alignment,
1377                                           TargetFlags);
1378   CSEMap.InsertNode(N, IP);
1379   InsertNode(N);
1380   return SDValue(N, 0);
1381 }
1382 
1383 SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1384                                      unsigned char TargetFlags) {
1385   FoldingSetNodeID ID;
1386   AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
1387   ID.AddInteger(Index);
1388   ID.AddInteger(Offset);
1389   ID.AddInteger(TargetFlags);
1390   void *IP = nullptr;
1391   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1392     return SDValue(E, 0);
1393 
1394   auto *N = newSDNode<TargetIndexSDNode>(Index, VT, Offset, TargetFlags);
1395   CSEMap.InsertNode(N, IP);
1396   InsertNode(N);
1397   return SDValue(N, 0);
1398 }
1399 
1400 SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
1401   FoldingSetNodeID ID;
1402   AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
1403   ID.AddPointer(MBB);
1404   void *IP = nullptr;
1405   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1406     return SDValue(E, 0);
1407 
1408   auto *N = newSDNode<BasicBlockSDNode>(MBB);
1409   CSEMap.InsertNode(N, IP);
1410   InsertNode(N);
1411   return SDValue(N, 0);
1412 }
1413 
1414 SDValue SelectionDAG::getValueType(EVT VT) {
1415   if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1416       ValueTypeNodes.size())
1417     ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
1418 
1419   SDNode *&N = VT.isExtended() ?
1420     ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
1421 
1422   if (N) return SDValue(N, 0);
1423   N = newSDNode<VTSDNode>(VT);
1424   InsertNode(N);
1425   return SDValue(N, 0);
1426 }
1427 
1428 SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
1429   SDNode *&N = ExternalSymbols[Sym];
1430   if (N) return SDValue(N, 0);
1431   N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, VT);
1432   InsertNode(N);
1433   return SDValue(N, 0);
1434 }
1435 
1436 SDValue SelectionDAG::getMCSymbol(MCSymbol *Sym, EVT VT) {
1437   SDNode *&N = MCSymbols[Sym];
1438   if (N)
1439     return SDValue(N, 0);
1440   N = newSDNode<MCSymbolSDNode>(Sym, VT);
1441   InsertNode(N);
1442   return SDValue(N, 0);
1443 }
1444 
1445 SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
1446                                               unsigned char TargetFlags) {
1447   SDNode *&N =
1448     TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1449                                                                TargetFlags)];
1450   if (N) return SDValue(N, 0);
1451   N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, VT);
1452   InsertNode(N);
1453   return SDValue(N, 0);
1454 }
1455 
1456 SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
1457   if ((unsigned)Cond >= CondCodeNodes.size())
1458     CondCodeNodes.resize(Cond+1);
1459 
1460   if (!CondCodeNodes[Cond]) {
1461     auto *N = newSDNode<CondCodeSDNode>(Cond);
1462     CondCodeNodes[Cond] = N;
1463     InsertNode(N);
1464   }
1465 
1466   return SDValue(CondCodeNodes[Cond], 0);
1467 }
1468 
1469 // commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1470 // the shuffle mask M that point at N1 to point at N2, and indices that point
1471 // N2 to point at N1.
1472 static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1473   std::swap(N1, N2);
1474   ShuffleVectorSDNode::commuteMask(M);
1475 }
1476 
1477 SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
1478                                        SDValue N2, const int *Mask) {
1479   assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1480          "Invalid VECTOR_SHUFFLE");
1481 
1482   // Canonicalize shuffle undef, undef -> undef
1483   if (N1.isUndef() && N2.isUndef())
1484     return getUNDEF(VT);
1485 
1486   // Validate that all indices in Mask are within the range of the elements
1487   // input to the shuffle.
1488   unsigned NElts = VT.getVectorNumElements();
1489   SmallVector<int, 8> MaskVec;
1490   for (unsigned i = 0; i != NElts; ++i) {
1491     assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
1492     MaskVec.push_back(Mask[i]);
1493   }
1494 
1495   // Canonicalize shuffle v, v -> v, undef
1496   if (N1 == N2) {
1497     N2 = getUNDEF(VT);
1498     for (unsigned i = 0; i != NElts; ++i)
1499       if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
1500   }
1501 
1502   // Canonicalize shuffle undef, v -> v, undef.  Commute the shuffle mask.
1503   if (N1.isUndef())
1504     commuteShuffle(N1, N2, MaskVec);
1505 
1506   // If shuffling a splat, try to blend the splat instead. We do this here so
1507   // that even when this arises during lowering we don't have to re-handle it.
1508   auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
1509     BitVector UndefElements;
1510     SDValue Splat = BV->getSplatValue(&UndefElements);
1511     if (!Splat)
1512       return;
1513 
1514     for (int i = 0; i < (int)NElts; ++i) {
1515       if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + (int)NElts))
1516         continue;
1517 
1518       // If this input comes from undef, mark it as such.
1519       if (UndefElements[MaskVec[i] - Offset]) {
1520         MaskVec[i] = -1;
1521         continue;
1522       }
1523 
1524       // If we can blend a non-undef lane, use that instead.
1525       if (!UndefElements[i])
1526         MaskVec[i] = i + Offset;
1527     }
1528   };
1529   if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
1530     BlendSplat(N1BV, 0);
1531   if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
1532     BlendSplat(N2BV, NElts);
1533 
1534   // Canonicalize all index into lhs, -> shuffle lhs, undef
1535   // Canonicalize all index into rhs, -> shuffle rhs, undef
1536   bool AllLHS = true, AllRHS = true;
1537   bool N2Undef = N2.isUndef();
1538   for (unsigned i = 0; i != NElts; ++i) {
1539     if (MaskVec[i] >= (int)NElts) {
1540       if (N2Undef)
1541         MaskVec[i] = -1;
1542       else
1543         AllLHS = false;
1544     } else if (MaskVec[i] >= 0) {
1545       AllRHS = false;
1546     }
1547   }
1548   if (AllLHS && AllRHS)
1549     return getUNDEF(VT);
1550   if (AllLHS && !N2Undef)
1551     N2 = getUNDEF(VT);
1552   if (AllRHS) {
1553     N1 = getUNDEF(VT);
1554     commuteShuffle(N1, N2, MaskVec);
1555   }
1556   // Reset our undef status after accounting for the mask.
1557   N2Undef = N2.isUndef();
1558   // Re-check whether both sides ended up undef.
1559   if (N1.isUndef() && N2Undef)
1560     return getUNDEF(VT);
1561 
1562   // If Identity shuffle return that node.
1563   bool Identity = true, AllSame = true;
1564   for (unsigned i = 0; i != NElts; ++i) {
1565     if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
1566     if (MaskVec[i] != MaskVec[0]) AllSame = false;
1567   }
1568   if (Identity && NElts)
1569     return N1;
1570 
1571   // Shuffling a constant splat doesn't change the result.
1572   if (N2Undef) {
1573     SDValue V = N1;
1574 
1575     // Look through any bitcasts. We check that these don't change the number
1576     // (and size) of elements and just changes their types.
1577     while (V.getOpcode() == ISD::BITCAST)
1578       V = V->getOperand(0);
1579 
1580     // A splat should always show up as a build vector node.
1581     if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
1582       BitVector UndefElements;
1583       SDValue Splat = BV->getSplatValue(&UndefElements);
1584       // If this is a splat of an undef, shuffling it is also undef.
1585       if (Splat && Splat.isUndef())
1586         return getUNDEF(VT);
1587 
1588       bool SameNumElts =
1589           V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
1590 
1591       // We only have a splat which can skip shuffles if there is a splatted
1592       // value and no undef lanes rearranged by the shuffle.
1593       if (Splat && UndefElements.none()) {
1594         // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
1595         // number of elements match or the value splatted is a zero constant.
1596         if (SameNumElts)
1597           return N1;
1598         if (auto *C = dyn_cast<ConstantSDNode>(Splat))
1599           if (C->isNullValue())
1600             return N1;
1601       }
1602 
1603       // If the shuffle itself creates a splat, build the vector directly.
1604       if (AllSame && SameNumElts) {
1605         EVT BuildVT = BV->getValueType(0);
1606         const SDValue &Splatted = BV->getOperand(MaskVec[0]);
1607         SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
1608 
1609         // We may have jumped through bitcasts, so the type of the
1610         // BUILD_VECTOR may not match the type of the shuffle.
1611         if (BuildVT != VT)
1612           NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
1613         return NewBV;
1614       }
1615     }
1616   }
1617 
1618   FoldingSetNodeID ID;
1619   SDValue Ops[2] = { N1, N2 };
1620   AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
1621   for (unsigned i = 0; i != NElts; ++i)
1622     ID.AddInteger(MaskVec[i]);
1623 
1624   void* IP = nullptr;
1625   if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP))
1626     return SDValue(E, 0);
1627 
1628   // Allocate the mask array for the node out of the BumpPtrAllocator, since
1629   // SDNode doesn't have access to it.  This memory will be "leaked" when
1630   // the node is deallocated, but recovered when the NodeAllocator is released.
1631   int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1632   memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
1633 
1634   auto *N = newSDNode<ShuffleVectorSDNode>(VT, dl.getIROrder(),
1635                                            dl.getDebugLoc(), MaskAlloc);
1636   createOperands(N, Ops);
1637 
1638   CSEMap.InsertNode(N, IP);
1639   InsertNode(N);
1640   return SDValue(N, 0);
1641 }
1642 
1643 SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) {
1644   MVT VT = SV.getSimpleValueType(0);
1645   SmallVector<int, 8> MaskVec(SV.getMask().begin(), SV.getMask().end());
1646   ShuffleVectorSDNode::commuteMask(MaskVec);
1647 
1648   SDValue Op0 = SV.getOperand(0);
1649   SDValue Op1 = SV.getOperand(1);
1650   return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, &MaskVec[0]);
1651 }
1652 
1653 SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
1654                                        SDValue Val, SDValue DTy,
1655                                        SDValue STy, SDValue Rnd, SDValue Sat,
1656                                        ISD::CvtCode Code) {
1657   // If the src and dest types are the same and the conversion is between
1658   // integer types of the same sign or two floats, no conversion is necessary.
1659   if (DTy == STy &&
1660       (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
1661     return Val;
1662 
1663   FoldingSetNodeID ID;
1664   SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1665   AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
1666   void* IP = nullptr;
1667   if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP))
1668     return SDValue(E, 0);
1669 
1670   auto *N =
1671       newSDNode<CvtRndSatSDNode>(VT, dl.getIROrder(), dl.getDebugLoc(), Code);
1672   createOperands(N, Ops);
1673 
1674   CSEMap.InsertNode(N, IP);
1675   InsertNode(N);
1676   return SDValue(N, 0);
1677 }
1678 
1679 SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
1680   FoldingSetNodeID ID;
1681   AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
1682   ID.AddInteger(RegNo);
1683   void *IP = nullptr;
1684   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1685     return SDValue(E, 0);
1686 
1687   auto *N = newSDNode<RegisterSDNode>(RegNo, VT);
1688   CSEMap.InsertNode(N, IP);
1689   InsertNode(N);
1690   return SDValue(N, 0);
1691 }
1692 
1693 SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1694   FoldingSetNodeID ID;
1695   AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
1696   ID.AddPointer(RegMask);
1697   void *IP = nullptr;
1698   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1699     return SDValue(E, 0);
1700 
1701   auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
1702   CSEMap.InsertNode(N, IP);
1703   InsertNode(N);
1704   return SDValue(N, 0);
1705 }
1706 
1707 SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
1708   FoldingSetNodeID ID;
1709   SDValue Ops[] = { Root };
1710   AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
1711   ID.AddPointer(Label);
1712   void *IP = nullptr;
1713   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1714     return SDValue(E, 0);
1715 
1716   auto *N = newSDNode<EHLabelSDNode>(dl.getIROrder(), dl.getDebugLoc(), Label);
1717   createOperands(N, Ops);
1718 
1719   CSEMap.InsertNode(N, IP);
1720   InsertNode(N);
1721   return SDValue(N, 0);
1722 }
1723 
1724 
1725 SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
1726                                       int64_t Offset,
1727                                       bool isTarget,
1728                                       unsigned char TargetFlags) {
1729   unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1730 
1731   FoldingSetNodeID ID;
1732   AddNodeIDNode(ID, Opc, getVTList(VT), None);
1733   ID.AddPointer(BA);
1734   ID.AddInteger(Offset);
1735   ID.AddInteger(TargetFlags);
1736   void *IP = nullptr;
1737   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1738     return SDValue(E, 0);
1739 
1740   auto *N = newSDNode<BlockAddressSDNode>(Opc, VT, BA, Offset, TargetFlags);
1741   CSEMap.InsertNode(N, IP);
1742   InsertNode(N);
1743   return SDValue(N, 0);
1744 }
1745 
1746 SDValue SelectionDAG::getSrcValue(const Value *V) {
1747   assert((!V || V->getType()->isPointerTy()) &&
1748          "SrcValue is not a pointer?");
1749 
1750   FoldingSetNodeID ID;
1751   AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
1752   ID.AddPointer(V);
1753 
1754   void *IP = nullptr;
1755   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1756     return SDValue(E, 0);
1757 
1758   auto *N = newSDNode<SrcValueSDNode>(V);
1759   CSEMap.InsertNode(N, IP);
1760   InsertNode(N);
1761   return SDValue(N, 0);
1762 }
1763 
1764 /// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1765 SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1766   FoldingSetNodeID ID;
1767   AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
1768   ID.AddPointer(MD);
1769 
1770   void *IP = nullptr;
1771   if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1772     return SDValue(E, 0);
1773 
1774   auto *N = newSDNode<MDNodeSDNode>(MD);
1775   CSEMap.InsertNode(N, IP);
1776   InsertNode(N);
1777   return SDValue(N, 0);
1778 }
1779 
1780 SDValue SelectionDAG::getBitcast(EVT VT, SDValue V) {
1781   if (VT == V.getValueType())
1782     return V;
1783 
1784   return getNode(ISD::BITCAST, SDLoc(V), VT, V);
1785 }
1786 
1787 /// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1788 SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1789                                        unsigned SrcAS, unsigned DestAS) {
1790   SDValue Ops[] = {Ptr};
1791   FoldingSetNodeID ID;
1792   AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
1793   ID.AddInteger(SrcAS);
1794   ID.AddInteger(DestAS);
1795 
1796   void *IP = nullptr;
1797   if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP))
1798     return SDValue(E, 0);
1799 
1800   auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
1801                                            VT, SrcAS, DestAS);
1802   createOperands(N, Ops);
1803 
1804   CSEMap.InsertNode(N, IP);
1805   InsertNode(N);
1806   return SDValue(N, 0);
1807 }
1808 
1809 /// getShiftAmountOperand - Return the specified value casted to
1810 /// the target's desired shift amount type.
1811 SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
1812   EVT OpTy = Op.getValueType();
1813   EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
1814   if (OpTy == ShTy || OpTy.isVector()) return Op;
1815 
1816   return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
1817 }
1818 
1819 SDValue SelectionDAG::expandVAArg(SDNode *Node) {
1820   SDLoc dl(Node);
1821   const TargetLowering &TLI = getTargetLoweringInfo();
1822   const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1823   EVT VT = Node->getValueType(0);
1824   SDValue Tmp1 = Node->getOperand(0);
1825   SDValue Tmp2 = Node->getOperand(1);
1826   unsigned Align = Node->getConstantOperandVal(3);
1827 
1828   SDValue VAListLoad =
1829     getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1, Tmp2,
1830             MachinePointerInfo(V), false, false, false, 0);
1831   SDValue VAList = VAListLoad;
1832 
1833   if (Align > TLI.getMinStackArgumentAlignment()) {
1834     assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
1835 
1836     VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
1837                      getConstant(Align - 1, dl, VAList.getValueType()));
1838 
1839     VAList = getNode(ISD::AND, dl, VAList.getValueType(), VAList,
1840                      getConstant(-(int64_t)Align, dl, VAList.getValueType()));
1841   }
1842 
1843   // Increment the pointer, VAList, to the next vaarg
1844   Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
1845                  getConstant(getDataLayout().getTypeAllocSize(
1846                                                VT.getTypeForEVT(*getContext())),
1847                              dl, VAList.getValueType()));
1848   // Store the incremented VAList to the legalized pointer
1849   Tmp1 = getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2,
1850                   MachinePointerInfo(V), false, false, 0);
1851   // Load the actual argument out of the pointer VAList
1852   return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo(),
1853                  false, false, false, 0);
1854 }
1855 
1856 SDValue SelectionDAG::expandVACopy(SDNode *Node) {
1857   SDLoc dl(Node);
1858   const TargetLowering &TLI = getTargetLoweringInfo();
1859   // This defaults to loading a pointer from the input and storing it to the
1860   // output, returning the chain.
1861   const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
1862   const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
1863   SDValue Tmp1 = getLoad(TLI.getPointerTy(getDataLayout()), dl,
1864                          Node->getOperand(0), Node->getOperand(2),
1865                          MachinePointerInfo(VS), false, false, false, 0);
1866   return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
1867                   MachinePointerInfo(VD), false, false, 0);
1868 }
1869 
1870 /// CreateStackTemporary - Create a stack temporary, suitable for holding the
1871 /// specified value type.
1872 SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
1873   MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1874   unsigned ByteSize = VT.getStoreSize();
1875   Type *Ty = VT.getTypeForEVT(*getContext());
1876   unsigned StackAlign =
1877       std::max((unsigned)getDataLayout().getPrefTypeAlignment(Ty), minAlign);
1878 
1879   int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
1880   return getFrameIndex(FrameIdx, TLI->getPointerTy(getDataLayout()));
1881 }
1882 
1883 /// CreateStackTemporary - Create a stack temporary suitable for holding
1884 /// either of the specified value types.
1885 SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
1886   unsigned Bytes = std::max(VT1.getStoreSize(), VT2.getStoreSize());
1887   Type *Ty1 = VT1.getTypeForEVT(*getContext());
1888   Type *Ty2 = VT2.getTypeForEVT(*getContext());
1889   const DataLayout &DL = getDataLayout();
1890   unsigned Align =
1891       std::max(DL.getPrefTypeAlignment(Ty1), DL.getPrefTypeAlignment(Ty2));
1892 
1893   MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1894   int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
1895   return getFrameIndex(FrameIdx, TLI->getPointerTy(getDataLayout()));
1896 }
1897 
1898 SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
1899                                 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
1900   // These setcc operations always fold.
1901   switch (Cond) {
1902   default: break;
1903   case ISD::SETFALSE:
1904   case ISD::SETFALSE2: return getConstant(0, dl, VT);
1905   case ISD::SETTRUE:
1906   case ISD::SETTRUE2: {
1907     TargetLowering::BooleanContent Cnt =
1908         TLI->getBooleanContents(N1->getValueType(0));
1909     return getConstant(
1910         Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, dl,
1911         VT);
1912   }
1913 
1914   case ISD::SETOEQ:
1915   case ISD::SETOGT:
1916   case ISD::SETOGE:
1917   case ISD::SETOLT:
1918   case ISD::SETOLE:
1919   case ISD::SETONE:
1920   case ISD::SETO:
1921   case ISD::SETUO:
1922   case ISD::SETUEQ:
1923   case ISD::SETUNE:
1924     assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
1925     break;
1926   }
1927 
1928   if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2)) {
1929     const APInt &C2 = N2C->getAPIntValue();
1930     if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1)) {
1931       const APInt &C1 = N1C->getAPIntValue();
1932 
1933       switch (Cond) {
1934       default: llvm_unreachable("Unknown integer setcc!");
1935       case ISD::SETEQ:  return getConstant(C1 == C2, dl, VT);
1936       case ISD::SETNE:  return getConstant(C1 != C2, dl, VT);
1937       case ISD::SETULT: return getConstant(C1.ult(C2), dl, VT);
1938       case ISD::SETUGT: return getConstant(C1.ugt(C2), dl, VT);
1939       case ISD::SETULE: return getConstant(C1.ule(C2), dl, VT);
1940       case ISD::SETUGE: return getConstant(C1.uge(C2), dl, VT);
1941       case ISD::SETLT:  return getConstant(C1.slt(C2), dl, VT);
1942       case ISD::SETGT:  return getConstant(C1.sgt(C2), dl, VT);
1943       case ISD::SETLE:  return getConstant(C1.sle(C2), dl, VT);
1944       case ISD::SETGE:  return getConstant(C1.sge(C2), dl, VT);
1945       }
1946     }
1947   }
1948   if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1)) {
1949     if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2)) {
1950       APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
1951       switch (Cond) {
1952       default: break;
1953       case ISD::SETEQ:  if (R==APFloat::cmpUnordered)
1954                           return getUNDEF(VT);
1955                         // fall through
1956       case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, dl, VT);
1957       case ISD::SETNE:  if (R==APFloat::cmpUnordered)
1958                           return getUNDEF(VT);
1959                         // fall through
1960       case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
1961                                            R==APFloat::cmpLessThan, dl, VT);
1962       case ISD::SETLT:  if (R==APFloat::cmpUnordered)
1963                           return getUNDEF(VT);
1964                         // fall through
1965       case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, dl, VT);
1966       case ISD::SETGT:  if (R==APFloat::cmpUnordered)
1967                           return getUNDEF(VT);
1968                         // fall through
1969       case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, dl, VT);
1970       case ISD::SETLE:  if (R==APFloat::cmpUnordered)
1971                           return getUNDEF(VT);
1972                         // fall through
1973       case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
1974                                            R==APFloat::cmpEqual, dl, VT);
1975       case ISD::SETGE:  if (R==APFloat::cmpUnordered)
1976                           return getUNDEF(VT);
1977                         // fall through
1978       case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
1979                                            R==APFloat::cmpEqual, dl, VT);
1980       case ISD::SETO:   return getConstant(R!=APFloat::cmpUnordered, dl, VT);
1981       case ISD::SETUO:  return getConstant(R==APFloat::cmpUnordered, dl, VT);
1982       case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1983                                            R==APFloat::cmpEqual, dl, VT);
1984       case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, dl, VT);
1985       case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1986                                            R==APFloat::cmpLessThan, dl, VT);
1987       case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1988                                            R==APFloat::cmpUnordered, dl, VT);
1989       case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, dl, VT);
1990       case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, dl, VT);
1991       }
1992     } else {
1993       // Ensure that the constant occurs on the RHS.
1994       ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1995       MVT CompVT = N1.getValueType().getSimpleVT();
1996       if (!TLI->isCondCodeLegal(SwappedCond, CompVT))
1997         return SDValue();
1998 
1999       return getSetCC(dl, VT, N2, N1, SwappedCond);
2000     }
2001   }
2002 
2003   // Could not fold it.
2004   return SDValue();
2005 }
2006 
2007 /// SignBitIsZero - Return true if the sign bit of Op is known to be zero.  We
2008 /// use this predicate to simplify operations downstream.
2009 bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
2010   // This predicate is not safe for vector operations.
2011   if (Op.getValueType().isVector())
2012     return false;
2013 
2014   unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
2015   return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
2016 }
2017 
2018 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero.  We use
2019 /// this predicate to simplify operations downstream.  Mask is known to be zero
2020 /// for bits that V cannot have.
2021 bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
2022                                      unsigned Depth) const {
2023   APInt KnownZero, KnownOne;
2024   computeKnownBits(Op, KnownZero, KnownOne, Depth);
2025   return (KnownZero & Mask) == Mask;
2026 }
2027 
2028 /// Determine which bits of Op are known to be either zero or one and return
2029 /// them in the KnownZero/KnownOne bitsets.
2030 void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
2031                                     APInt &KnownOne, unsigned Depth) const {
2032   unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
2033 
2034   KnownZero = KnownOne = APInt(BitWidth, 0);   // Don't know anything.
2035   if (Depth == 6)
2036     return;  // Limit search depth.
2037 
2038   APInt KnownZero2, KnownOne2;
2039 
2040   switch (Op.getOpcode()) {
2041   case ISD::Constant:
2042     // We know all of the bits for a constant!
2043     KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
2044     KnownZero = ~KnownOne;
2045     break;
2046   case ISD::AND:
2047     // If either the LHS or the RHS are Zero, the result is zero.
2048     computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2049     computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
2050 
2051     // Output known-1 bits are only known if set in both the LHS & RHS.
2052     KnownOne &= KnownOne2;
2053     // Output known-0 are known to be clear if zero in either the LHS | RHS.
2054     KnownZero |= KnownZero2;
2055     break;
2056   case ISD::OR:
2057     computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2058     computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
2059 
2060     // Output known-0 bits are only known if clear in both the LHS & RHS.
2061     KnownZero &= KnownZero2;
2062     // Output known-1 are known to be set if set in either the LHS | RHS.
2063     KnownOne |= KnownOne2;
2064     break;
2065   case ISD::XOR: {
2066     computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2067     computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
2068 
2069     // Output known-0 bits are known if clear or set in both the LHS & RHS.
2070     APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
2071     // Output known-1 are known to be set if set in only one of the LHS, RHS.
2072     KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
2073     KnownZero = KnownZeroOut;
2074     break;
2075   }
2076   case ISD::MUL: {
2077     computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2078     computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
2079 
2080     // If low bits are zero in either operand, output low known-0 bits.
2081     // Also compute a conserative estimate for high known-0 bits.
2082     // More trickiness is possible, but this is sufficient for the
2083     // interesting case of alignment computation.
2084     KnownOne.clearAllBits();
2085     unsigned TrailZ = KnownZero.countTrailingOnes() +
2086                       KnownZero2.countTrailingOnes();
2087     unsigned LeadZ =  std::max(KnownZero.countLeadingOnes() +
2088                                KnownZero2.countLeadingOnes(),
2089                                BitWidth) - BitWidth;
2090 
2091     TrailZ = std::min(TrailZ, BitWidth);
2092     LeadZ = std::min(LeadZ, BitWidth);
2093     KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
2094                 APInt::getHighBitsSet(BitWidth, LeadZ);
2095     break;
2096   }
2097   case ISD::UDIV: {
2098     // For the purposes of computing leading zeros we can conservatively
2099     // treat a udiv as a logical right shift by the power of 2 known to
2100     // be less than the denominator.
2101     computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
2102     unsigned LeadZ = KnownZero2.countLeadingOnes();
2103 
2104     KnownOne2.clearAllBits();
2105     KnownZero2.clearAllBits();
2106     computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
2107     unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
2108     if (RHSUnknownLeadingOnes != BitWidth)
2109       LeadZ = std::min(BitWidth,
2110                        LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
2111 
2112     KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
2113     break;
2114   }
2115   case ISD::SELECT:
2116     computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
2117     computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
2118 
2119     // Only known if known in both the LHS and RHS.
2120     KnownOne &= KnownOne2;
2121     KnownZero &= KnownZero2;
2122     break;
2123   case ISD::SELECT_CC:
2124     computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
2125     computeKnownBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
2126 
2127     // Only known if known in both the LHS and RHS.
2128     KnownOne &= KnownOne2;
2129     KnownZero &= KnownZero2;
2130     break;
2131   case ISD::SADDO:
2132   case ISD::UADDO:
2133   case ISD::SSUBO:
2134   case ISD::USUBO:
2135   case ISD::SMULO:
2136   case ISD::UMULO:
2137     if (Op.getResNo() != 1)
2138       break;
2139     // The boolean result conforms to getBooleanContents.
2140     // If we know the result of a setcc has the top bits zero, use this info.
2141     // We know that we have an integer-based boolean since these operations
2142     // are only available for integer.
2143     if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2144             TargetLowering::ZeroOrOneBooleanContent &&
2145         BitWidth > 1)
2146       KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2147     break;
2148   case ISD::SETCC:
2149     // If we know the result of a setcc has the top bits zero, use this info.
2150     if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2151             TargetLowering::ZeroOrOneBooleanContent &&
2152         BitWidth > 1)
2153       KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2154     break;
2155   case ISD::SHL:
2156     // (shl X, C1) & C2 == 0   iff   (X & C2 >>u C1) == 0
2157     if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2158       unsigned ShAmt = SA->getZExtValue();
2159 
2160       // If the shift count is an invalid immediate, don't do anything.
2161       if (ShAmt >= BitWidth)
2162         break;
2163 
2164       computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2165       KnownZero <<= ShAmt;
2166       KnownOne  <<= ShAmt;
2167       // low bits known zero.
2168       KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
2169     }
2170     break;
2171   case ISD::SRL:
2172     // (ushr X, C1) & C2 == 0   iff  (-1 >> C1) & C2 == 0
2173     if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2174       unsigned ShAmt = SA->getZExtValue();
2175 
2176       // If the shift count is an invalid immediate, don't do anything.
2177       if (ShAmt >= BitWidth)
2178         break;
2179 
2180       computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2181       KnownZero = KnownZero.lshr(ShAmt);
2182       KnownOne  = KnownOne.lshr(ShAmt);
2183 
2184       APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
2185       KnownZero |= HighBits;  // High bits known zero.
2186     }
2187     break;
2188   case ISD::SRA:
2189     if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2190       unsigned ShAmt = SA->getZExtValue();
2191 
2192       // If the shift count is an invalid immediate, don't do anything.
2193       if (ShAmt >= BitWidth)
2194         break;
2195 
2196       // If any of the demanded bits are produced by the sign extension, we also
2197       // demand the input sign bit.
2198       APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
2199 
2200       computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2201       KnownZero = KnownZero.lshr(ShAmt);
2202       KnownOne  = KnownOne.lshr(ShAmt);
2203 
2204       // Handle the sign bits.
2205       APInt SignBit = APInt::getSignBit(BitWidth);
2206       SignBit = SignBit.lshr(ShAmt);  // Adjust to where it is now in the mask.
2207 
2208       if (KnownZero.intersects(SignBit)) {
2209         KnownZero |= HighBits;  // New bits are known zero.
2210       } else if (KnownOne.intersects(SignBit)) {
2211         KnownOne  |= HighBits;  // New bits are known one.
2212       }
2213     }
2214     break;
2215   case ISD::SIGN_EXTEND_INREG: {
2216     EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
2217     unsigned EBits = EVT.getScalarType().getSizeInBits();
2218 
2219     // Sign extension.  Compute the demanded bits in the result that are not
2220     // present in the input.
2221     APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
2222 
2223     APInt InSignBit = APInt::getSignBit(EBits);
2224     APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
2225 
2226     // If the sign extended bits are demanded, we know that the sign
2227     // bit is demanded.
2228     InSignBit = InSignBit.zext(BitWidth);
2229     if (NewBits.getBoolValue())
2230       InputDemandedBits |= InSignBit;
2231 
2232     computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2233     KnownOne &= InputDemandedBits;
2234     KnownZero &= InputDemandedBits;
2235 
2236     // If the sign bit of the input is known set or clear, then we know the
2237     // top bits of the result.
2238     if (KnownZero.intersects(InSignBit)) {         // Input sign bit known clear
2239       KnownZero |= NewBits;
2240       KnownOne  &= ~NewBits;
2241     } else if (KnownOne.intersects(InSignBit)) {   // Input sign bit known set
2242       KnownOne  |= NewBits;
2243       KnownZero &= ~NewBits;
2244     } else {                              // Input sign bit unknown
2245       KnownZero &= ~NewBits;
2246       KnownOne  &= ~NewBits;
2247     }
2248     break;
2249   }
2250   case ISD::CTTZ:
2251   case ISD::CTTZ_ZERO_UNDEF:
2252   case ISD::CTLZ:
2253   case ISD::CTLZ_ZERO_UNDEF:
2254   case ISD::CTPOP: {
2255     unsigned LowBits = Log2_32(BitWidth)+1;
2256     KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
2257     KnownOne.clearAllBits();
2258     break;
2259   }
2260   case ISD::LOAD: {
2261     LoadSDNode *LD = cast<LoadSDNode>(Op);
2262     // If this is a ZEXTLoad and we are looking at the loaded value.
2263     if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
2264       EVT VT = LD->getMemoryVT();
2265       unsigned MemBits = VT.getScalarType().getSizeInBits();
2266       KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
2267     } else if (const MDNode *Ranges = LD->getRanges()) {
2268       if (LD->getExtensionType() == ISD::NON_EXTLOAD)
2269         computeKnownBitsFromRangeMetadata(*Ranges, KnownZero, KnownOne);
2270     }
2271     break;
2272   }
2273   case ISD::ZERO_EXTEND: {
2274     EVT InVT = Op.getOperand(0).getValueType();
2275     unsigned InBits = InVT.getScalarType().getSizeInBits();
2276     APInt NewBits   = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
2277     KnownZero = KnownZero.trunc(InBits);
2278     KnownOne = KnownOne.trunc(InBits);
2279     computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2280     KnownZero = KnownZero.zext(BitWidth);
2281     KnownOne = KnownOne.zext(BitWidth);
2282     KnownZero |= NewBits;
2283     break;
2284   }
2285   case ISD::SIGN_EXTEND: {
2286     EVT InVT = Op.getOperand(0).getValueType();
2287     unsigned InBits = InVT.getScalarType().getSizeInBits();
2288     APInt NewBits   = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
2289 
2290     KnownZero = KnownZero.trunc(InBits);
2291     KnownOne = KnownOne.trunc(InBits);
2292     computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2293 
2294     // Note if the sign bit is known to be zero or one.
2295     bool SignBitKnownZero = KnownZero.isNegative();
2296     bool SignBitKnownOne  = KnownOne.isNegative();
2297 
2298     KnownZero = KnownZero.zext(BitWidth);
2299     KnownOne = KnownOne.zext(BitWidth);
2300 
2301     // If the sign bit is known zero or one, the top bits match.
2302     if (SignBitKnownZero)
2303       KnownZero |= NewBits;
2304     else if (SignBitKnownOne)
2305       KnownOne  |= NewBits;
2306     break;
2307   }
2308   case ISD::ANY_EXTEND: {
2309     EVT InVT = Op.getOperand(0).getValueType();
2310     unsigned InBits = InVT.getScalarType().getSizeInBits();
2311     KnownZero = KnownZero.trunc(InBits);
2312     KnownOne = KnownOne.trunc(InBits);
2313     computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2314     KnownZero = KnownZero.zext(BitWidth);
2315     KnownOne = KnownOne.zext(BitWidth);
2316     break;
2317   }
2318   case ISD::TRUNCATE: {
2319     EVT InVT = Op.getOperand(0).getValueType();
2320     unsigned InBits = InVT.getScalarType().getSizeInBits();
2321     KnownZero = KnownZero.zext(InBits);
2322     KnownOne = KnownOne.zext(InBits);
2323     computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2324     KnownZero = KnownZero.trunc(BitWidth);
2325     KnownOne = KnownOne.trunc(BitWidth);
2326     break;
2327   }
2328   case ISD::AssertZext: {
2329     EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
2330     APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
2331     computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2332     KnownZero |= (~InMask);
2333     KnownOne  &= (~KnownZero);
2334     break;
2335   }
2336   case ISD::FGETSIGN:
2337     // All bits are zero except the low bit.
2338     KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2339     break;
2340 
2341   case ISD::SUB: {
2342     if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2343       // We know that the top bits of C-X are clear if X contains less bits
2344       // than C (i.e. no wrap-around can happen).  For example, 20-X is
2345       // positive if we can prove that X is >= 0 and < 16.
2346       if (CLHS->getAPIntValue().isNonNegative()) {
2347         unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2348         // NLZ can't be BitWidth with no sign bit
2349         APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
2350         computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
2351 
2352         // If all of the MaskV bits are known to be zero, then we know the
2353         // output top bits are zero, because we now know that the output is
2354         // from [0-C].
2355         if ((KnownZero2 & MaskV) == MaskV) {
2356           unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2357           // Top bits known zero.
2358           KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
2359         }
2360       }
2361     }
2362   }
2363   // fall through
2364   case ISD::ADD:
2365   case ISD::ADDE: {
2366     // Output known-0 bits are known if clear or set in both the low clear bits
2367     // common to both LHS & RHS.  For example, 8+(X<<3) is known to have the
2368     // low 3 bits clear.
2369     // Output known-0 bits are also known if the top bits of each input are
2370     // known to be clear. For example, if one input has the top 10 bits clear
2371     // and the other has the top 8 bits clear, we know the top 7 bits of the
2372     // output must be clear.
2373     computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
2374     unsigned KnownZeroHigh = KnownZero2.countLeadingOnes();
2375     unsigned KnownZeroLow = KnownZero2.countTrailingOnes();
2376 
2377     computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
2378     KnownZeroHigh = std::min(KnownZeroHigh,
2379                              KnownZero2.countLeadingOnes());
2380     KnownZeroLow = std::min(KnownZeroLow,
2381                             KnownZero2.countTrailingOnes());
2382 
2383     if (Op.getOpcode() == ISD::ADD) {
2384       KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroLow);
2385       if (KnownZeroHigh > 1)
2386         KnownZero |= APInt::getHighBitsSet(BitWidth, KnownZeroHigh - 1);
2387       break;
2388     }
2389 
2390     // With ADDE, a carry bit may be added in, so we can only use this
2391     // information if we know (at least) that the low two bits are clear.  We
2392     // then return to the caller that the low bit is unknown but that other bits
2393     // are known zero.
2394     if (KnownZeroLow >= 2) // ADDE
2395       KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroLow);
2396     break;
2397   }
2398   case ISD::SREM:
2399     if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2400       const APInt &RA = Rem->getAPIntValue().abs();
2401       if (RA.isPowerOf2()) {
2402         APInt LowBits = RA - 1;
2403         computeKnownBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
2404 
2405         // The low bits of the first operand are unchanged by the srem.
2406         KnownZero = KnownZero2 & LowBits;
2407         KnownOne = KnownOne2 & LowBits;
2408 
2409         // If the first operand is non-negative or has all low bits zero, then
2410         // the upper bits are all zero.
2411         if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2412           KnownZero |= ~LowBits;
2413 
2414         // If the first operand is negative and not all low bits are zero, then
2415         // the upper bits are all one.
2416         if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2417           KnownOne |= ~LowBits;
2418         assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2419       }
2420     }
2421     break;
2422   case ISD::UREM: {
2423     if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2424       const APInt &RA = Rem->getAPIntValue();
2425       if (RA.isPowerOf2()) {
2426         APInt LowBits = (RA - 1);
2427         computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth + 1);
2428 
2429         // The upper bits are all zero, the lower ones are unchanged.
2430         KnownZero = KnownZero2 | ~LowBits;
2431         KnownOne = KnownOne2 & LowBits;
2432         break;
2433       }
2434     }
2435 
2436     // Since the result is less than or equal to either operand, any leading
2437     // zero bits in either operand must also exist in the result.
2438     computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2439     computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
2440 
2441     uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2442                                 KnownZero2.countLeadingOnes());
2443     KnownOne.clearAllBits();
2444     KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
2445     break;
2446   }
2447   case ISD::EXTRACT_ELEMENT: {
2448     computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2449     const unsigned Index =
2450       cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2451     const unsigned BitWidth = Op.getValueType().getSizeInBits();
2452 
2453     // Remove low part of known bits mask
2454     KnownZero = KnownZero.getHiBits(KnownZero.getBitWidth() - Index * BitWidth);
2455     KnownOne = KnownOne.getHiBits(KnownOne.getBitWidth() - Index * BitWidth);
2456 
2457     // Remove high part of known bit mask
2458     KnownZero = KnownZero.trunc(BitWidth);
2459     KnownOne = KnownOne.trunc(BitWidth);
2460     break;
2461   }
2462   case ISD::SMIN:
2463   case ISD::SMAX:
2464   case ISD::UMIN:
2465   case ISD::UMAX: {
2466     APInt Op0Zero, Op0One;
2467     APInt Op1Zero, Op1One;
2468     computeKnownBits(Op.getOperand(0), Op0Zero, Op0One, Depth);
2469     computeKnownBits(Op.getOperand(1), Op1Zero, Op1One, Depth);
2470 
2471     KnownZero = Op0Zero & Op1Zero;
2472     KnownOne = Op0One & Op1One;
2473     break;
2474   }
2475   case ISD::FrameIndex:
2476   case ISD::TargetFrameIndex:
2477     if (unsigned Align = InferPtrAlignment(Op)) {
2478       // The low bits are known zero if the pointer is aligned.
2479       KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2480       break;
2481     }
2482     break;
2483 
2484   default:
2485     if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2486       break;
2487     // Fallthrough
2488   case ISD::INTRINSIC_WO_CHAIN:
2489   case ISD::INTRINSIC_W_CHAIN:
2490   case ISD::INTRINSIC_VOID:
2491     // Allow the target to implement this method for its nodes.
2492     TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
2493     break;
2494   }
2495 
2496   assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
2497 }
2498 
2499 /// ComputeNumSignBits - Return the number of times the sign bit of the
2500 /// register is replicated into the other bits.  We know that at least 1 bit
2501 /// is always equal to the sign bit (itself), but other cases can give us
2502 /// information.  For example, immediately after an "SRA X, 2", we know that
2503 /// the top 3 bits are all equal to each other, so we return 3.
2504 unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
2505   EVT VT = Op.getValueType();
2506   assert(VT.isInteger() && "Invalid VT!");
2507   unsigned VTBits = VT.getScalarType().getSizeInBits();
2508   unsigned Tmp, Tmp2;
2509   unsigned FirstAnswer = 1;
2510 
2511   if (Depth == 6)
2512     return 1;  // Limit search depth.
2513 
2514   switch (Op.getOpcode()) {
2515   default: break;
2516   case ISD::AssertSext:
2517     Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
2518     return VTBits-Tmp+1;
2519   case ISD::AssertZext:
2520     Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
2521     return VTBits-Tmp;
2522 
2523   case ISD::Constant: {
2524     const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
2525     return Val.getNumSignBits();
2526   }
2527 
2528   case ISD::SIGN_EXTEND:
2529     Tmp =
2530         VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
2531     return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
2532 
2533   case ISD::SIGN_EXTEND_INREG:
2534     // Max of the input and what this extends.
2535     Tmp =
2536       cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
2537     Tmp = VTBits-Tmp+1;
2538 
2539     Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2540     return std::max(Tmp, Tmp2);
2541 
2542   case ISD::SRA:
2543     Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2544     // SRA X, C   -> adds C sign bits.
2545     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2546       Tmp += C->getZExtValue();
2547       if (Tmp > VTBits) Tmp = VTBits;
2548     }
2549     return Tmp;
2550   case ISD::SHL:
2551     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2552       // shl destroys sign bits.
2553       Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2554       if (C->getZExtValue() >= VTBits ||      // Bad shift.
2555           C->getZExtValue() >= Tmp) break;    // Shifted all sign bits out.
2556       return Tmp - C->getZExtValue();
2557     }
2558     break;
2559   case ISD::AND:
2560   case ISD::OR:
2561   case ISD::XOR:    // NOT is handled here.
2562     // Logical binary ops preserve the number of sign bits at the worst.
2563     Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2564     if (Tmp != 1) {
2565       Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2566       FirstAnswer = std::min(Tmp, Tmp2);
2567       // We computed what we know about the sign bits as our first
2568       // answer. Now proceed to the generic code that uses
2569       // computeKnownBits, and pick whichever answer is better.
2570     }
2571     break;
2572 
2573   case ISD::SELECT:
2574     Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2575     if (Tmp == 1) return 1;  // Early out.
2576     Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
2577     return std::min(Tmp, Tmp2);
2578   case ISD::SELECT_CC:
2579     Tmp = ComputeNumSignBits(Op.getOperand(2), Depth+1);
2580     if (Tmp == 1) return 1;  // Early out.
2581     Tmp2 = ComputeNumSignBits(Op.getOperand(3), Depth+1);
2582     return std::min(Tmp, Tmp2);
2583   case ISD::SMIN:
2584   case ISD::SMAX:
2585   case ISD::UMIN:
2586   case ISD::UMAX:
2587     Tmp = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
2588     if (Tmp == 1)
2589       return 1;  // Early out.
2590     Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
2591     return std::min(Tmp, Tmp2);
2592   case ISD::SADDO:
2593   case ISD::UADDO:
2594   case ISD::SSUBO:
2595   case ISD::USUBO:
2596   case ISD::SMULO:
2597   case ISD::UMULO:
2598     if (Op.getResNo() != 1)
2599       break;
2600     // The boolean result conforms to getBooleanContents.  Fall through.
2601     // If setcc returns 0/-1, all bits are sign bits.
2602     // We know that we have an integer-based boolean since these operations
2603     // are only available for integer.
2604     if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2605         TargetLowering::ZeroOrNegativeOneBooleanContent)
2606       return VTBits;
2607     break;
2608   case ISD::SETCC:
2609     // If setcc returns 0/-1, all bits are sign bits.
2610     if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2611         TargetLowering::ZeroOrNegativeOneBooleanContent)
2612       return VTBits;
2613     break;
2614   case ISD::ROTL:
2615   case ISD::ROTR:
2616     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2617       unsigned RotAmt = C->getZExtValue() & (VTBits-1);
2618 
2619       // Handle rotate right by N like a rotate left by 32-N.
2620       if (Op.getOpcode() == ISD::ROTR)
2621         RotAmt = (VTBits-RotAmt) & (VTBits-1);
2622 
2623       // If we aren't rotating out all of the known-in sign bits, return the
2624       // number that are left.  This handles rotl(sext(x), 1) for example.
2625       Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2626       if (Tmp > RotAmt+1) return Tmp-RotAmt;
2627     }
2628     break;
2629   case ISD::ADD:
2630     // Add can have at most one carry bit.  Thus we know that the output
2631     // is, at worst, one more bit than the inputs.
2632     Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2633     if (Tmp == 1) return 1;  // Early out.
2634 
2635     // Special case decrementing a value (ADD X, -1):
2636     if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2637       if (CRHS->isAllOnesValue()) {
2638         APInt KnownZero, KnownOne;
2639         computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2640 
2641         // If the input is known to be 0 or 1, the output is 0/-1, which is all
2642         // sign bits set.
2643         if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
2644           return VTBits;
2645 
2646         // If we are subtracting one from a positive number, there is no carry
2647         // out of the result.
2648         if (KnownZero.isNegative())
2649           return Tmp;
2650       }
2651 
2652     Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2653     if (Tmp2 == 1) return 1;
2654     return std::min(Tmp, Tmp2)-1;
2655 
2656   case ISD::SUB:
2657     Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2658     if (Tmp2 == 1) return 1;
2659 
2660     // Handle NEG.
2661     if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
2662       if (CLHS->isNullValue()) {
2663         APInt KnownZero, KnownOne;
2664         computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2665         // If the input is known to be 0 or 1, the output is 0/-1, which is all
2666         // sign bits set.
2667         if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
2668           return VTBits;
2669 
2670         // If the input is known to be positive (the sign bit is known clear),
2671         // the output of the NEG has the same number of sign bits as the input.
2672         if (KnownZero.isNegative())
2673           return Tmp2;
2674 
2675         // Otherwise, we treat this like a SUB.
2676       }
2677 
2678     // Sub can have at most one carry bit.  Thus we know that the output
2679     // is, at worst, one more bit than the inputs.
2680     Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2681     if (Tmp == 1) return 1;  // Early out.
2682     return std::min(Tmp, Tmp2)-1;
2683   case ISD::TRUNCATE:
2684     // FIXME: it's tricky to do anything useful for this, but it is an important
2685     // case for targets like X86.
2686     break;
2687   case ISD::EXTRACT_ELEMENT: {
2688     const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2689     const int BitWidth = Op.getValueType().getSizeInBits();
2690     const int Items =
2691       Op.getOperand(0).getValueType().getSizeInBits() / BitWidth;
2692 
2693     // Get reverse index (starting from 1), Op1 value indexes elements from
2694     // little end. Sign starts at big end.
2695     const int rIndex = Items - 1 -
2696       cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2697 
2698     // If the sign portion ends in our element the subtraction gives correct
2699     // result. Otherwise it gives either negative or > bitwidth result
2700     return std::max(std::min(KnownSign - rIndex * BitWidth, BitWidth), 0);
2701   }
2702   }
2703 
2704   // If we are looking at the loaded value of the SDNode.
2705   if (Op.getResNo() == 0) {
2706     // Handle LOADX separately here. EXTLOAD case will fallthrough.
2707     if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2708       unsigned ExtType = LD->getExtensionType();
2709       switch (ExtType) {
2710         default: break;
2711         case ISD::SEXTLOAD:    // '17' bits known
2712           Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2713           return VTBits-Tmp+1;
2714         case ISD::ZEXTLOAD:    // '16' bits known
2715           Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2716           return VTBits-Tmp;
2717       }
2718     }
2719   }
2720 
2721   // Allow the target to implement this method for its nodes.
2722   if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
2723       Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
2724       Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2725       Op.getOpcode() == ISD::INTRINSIC_VOID) {
2726     unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
2727     if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
2728   }
2729 
2730   // Finally, if we can prove that the top bits of the result are 0's or 1's,
2731   // use this information.
2732   APInt KnownZero, KnownOne;
2733   computeKnownBits(Op, KnownZero, KnownOne, Depth);
2734 
2735   APInt Mask;
2736   if (KnownZero.isNegative()) {        // sign bit is 0
2737     Mask = KnownZero;
2738   } else if (KnownOne.isNegative()) {  // sign bit is 1;
2739     Mask = KnownOne;
2740   } else {
2741     // Nothing known.
2742     return FirstAnswer;
2743   }
2744 
2745   // Okay, we know that the sign bit in Mask is set.  Use CLZ to determine
2746   // the number of identical bits in the top of the input value.
2747   Mask = ~Mask;
2748   Mask <<= Mask.getBitWidth()-VTBits;
2749   // Return # leading zeros.  We use 'min' here in case Val was zero before
2750   // shifting.  We don't want to return '64' as for an i32 "0".
2751   return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
2752 }
2753 
2754 /// isBaseWithConstantOffset - Return true if the specified operand is an
2755 /// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2756 /// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2757 /// semantics as an ADD.  This handles the equivalence:
2758 ///     X|Cst == X+Cst iff X&Cst = 0.
2759 bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2760   if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2761       !isa<ConstantSDNode>(Op.getOperand(1)))
2762     return false;
2763 
2764   if (Op.getOpcode() == ISD::OR &&
2765       !MaskedValueIsZero(Op.getOperand(0),
2766                      cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2767     return false;
2768 
2769   return true;
2770 }
2771 
2772 
2773 bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2774   // If we're told that NaNs won't happen, assume they won't.
2775   if (getTarget().Options.NoNaNsFPMath)
2776     return true;
2777 
2778   // If the value is a constant, we can obviously see if it is a NaN or not.
2779   if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2780     return !C->getValueAPF().isNaN();
2781 
2782   // TODO: Recognize more cases here.
2783 
2784   return false;
2785 }
2786 
2787 bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2788   // If the value is a constant, we can obviously see if it is a zero or not.
2789   if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2790     return !C->isZero();
2791 
2792   // TODO: Recognize more cases here.
2793   switch (Op.getOpcode()) {
2794   default: break;
2795   case ISD::OR:
2796     if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2797       return !C->isNullValue();
2798     break;
2799   }
2800 
2801   return false;
2802 }
2803 
2804 bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2805   // Check the obvious case.
2806   if (A == B) return true;
2807 
2808   // For for negative and positive zero.
2809   if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2810     if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2811       if (CA->isZero() && CB->isZero()) return true;
2812 
2813   // Otherwise they may not be equal.
2814   return false;
2815 }
2816 
2817 bool SelectionDAG::haveNoCommonBitsSet(SDValue A, SDValue B) const {
2818   assert(A.getValueType() == B.getValueType() &&
2819          "Values must have the same type");
2820   APInt AZero, AOne;
2821   APInt BZero, BOne;
2822   computeKnownBits(A, AZero, AOne);
2823   computeKnownBits(B, BZero, BOne);
2824   return (AZero | BZero).isAllOnesValue();
2825 }
2826 
2827 static SDValue FoldCONCAT_VECTORS(SDLoc DL, EVT VT, ArrayRef<SDValue> Ops,
2828                                   llvm::SelectionDAG &DAG) {
2829   if (Ops.size() == 1)
2830     return Ops[0];
2831 
2832   // Concat of UNDEFs is UNDEF.
2833   if (std::all_of(Ops.begin(), Ops.end(),
2834                   [](SDValue Op) { return Op.isUndef(); }))
2835     return DAG.getUNDEF(VT);
2836 
2837   // A CONCAT_VECTOR with all UNDEF/BUILD_VECTOR operands can be
2838   // simplified to one big BUILD_VECTOR.
2839   // FIXME: Add support for SCALAR_TO_VECTOR as well.
2840   EVT SVT = VT.getScalarType();
2841   SmallVector<SDValue, 16> Elts;
2842   for (SDValue Op : Ops) {
2843     EVT OpVT = Op.getValueType();
2844     if (Op.isUndef())
2845       Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
2846     else if (Op.getOpcode() == ISD::BUILD_VECTOR)
2847       Elts.append(Op->op_begin(), Op->op_end());
2848     else
2849       return SDValue();
2850   }
2851 
2852   // BUILD_VECTOR requires all inputs to be of the same type, find the
2853   // maximum type and extend them all.
2854   for (SDValue Op : Elts)
2855     SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
2856 
2857   if (SVT.bitsGT(VT.getScalarType()))
2858     for (SDValue &Op : Elts)
2859       Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
2860                ? DAG.getZExtOrTrunc(Op, DL, SVT)
2861                : DAG.getSExtOrTrunc(Op, DL, SVT);
2862 
2863   return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
2864 }
2865 
2866 /// getNode - Gets or creates the specified node.
2867 ///
2868 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
2869   FoldingSetNodeID ID;
2870   AddNodeIDNode(ID, Opcode, getVTList(VT), None);
2871   void *IP = nullptr;
2872   if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP))
2873     return SDValue(E, 0);
2874 
2875   auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(),
2876                               getVTList(VT));
2877   CSEMap.InsertNode(N, IP);
2878 
2879   InsertNode(N);
2880   return SDValue(N, 0);
2881 }
2882 
2883 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
2884                               EVT VT, SDValue Operand) {
2885   // Constant fold unary operations with an integer constant operand. Even
2886   // opaque constant will be folded, because the folding of unary operations
2887   // doesn't create new constants with different values. Nevertheless, the
2888   // opaque flag is preserved during folding to prevent future folding with
2889   // other constants.
2890   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand)) {
2891     const APInt &Val = C->getAPIntValue();
2892     switch (Opcode) {
2893     default: break;
2894     case ISD::SIGN_EXTEND:
2895       return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
2896                          C->isTargetOpcode(), C->isOpaque());
2897     case ISD::ANY_EXTEND:
2898     case ISD::ZERO_EXTEND:
2899     case ISD::TRUNCATE:
2900       return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
2901                          C->isTargetOpcode(), C->isOpaque());
2902     case ISD::UINT_TO_FP:
2903     case ISD::SINT_TO_FP: {
2904       APFloat apf(EVTToAPFloatSemantics(VT),
2905                   APInt::getNullValue(VT.getSizeInBits()));
2906       (void)apf.convertFromAPInt(Val,
2907                                  Opcode==ISD::SINT_TO_FP,
2908                                  APFloat::rmNearestTiesToEven);
2909       return getConstantFP(apf, DL, VT);
2910     }
2911     case ISD::BITCAST:
2912       if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
2913         return getConstantFP(APFloat(APFloat::IEEEhalf, Val), DL, VT);
2914       if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
2915         return getConstantFP(APFloat(APFloat::IEEEsingle, Val), DL, VT);
2916       if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
2917         return getConstantFP(APFloat(APFloat::IEEEdouble, Val), DL, VT);
2918       if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
2919         return getConstantFP(APFloat(APFloat::IEEEquad, Val), DL, VT);
2920       break;
2921     case ISD::BSWAP:
2922       return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
2923                          C->isOpaque());
2924     case ISD::CTPOP:
2925       return getConstant(Val.countPopulation(), DL, VT, C->isTargetOpcode(),
2926                          C->isOpaque());
2927     case ISD::CTLZ:
2928     case ISD::CTLZ_ZERO_UNDEF:
2929       return getConstant(Val.countLeadingZeros(), DL, VT, C->isTargetOpcode(),
2930                          C->isOpaque());
2931     case ISD::CTTZ:
2932     case ISD::CTTZ_ZERO_UNDEF:
2933       return getConstant(Val.countTrailingZeros(), DL, VT, C->isTargetOpcode(),
2934                          C->isOpaque());
2935     }
2936   }
2937 
2938   // Constant fold unary operations with a floating point constant operand.
2939   if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand)) {
2940     APFloat V = C->getValueAPF();    // make copy
2941     switch (Opcode) {
2942     case ISD::FNEG:
2943       V.changeSign();
2944       return getConstantFP(V, DL, VT);
2945     case ISD::FABS:
2946       V.clearSign();
2947       return getConstantFP(V, DL, VT);
2948     case ISD::FCEIL: {
2949       APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2950       if (fs == APFloat::opOK || fs == APFloat::opInexact)
2951         return getConstantFP(V, DL, VT);
2952       break;
2953     }
2954     case ISD::FTRUNC: {
2955       APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2956       if (fs == APFloat::opOK || fs == APFloat::opInexact)
2957         return getConstantFP(V, DL, VT);
2958       break;
2959     }
2960     case ISD::FFLOOR: {
2961       APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2962       if (fs == APFloat::opOK || fs == APFloat::opInexact)
2963         return getConstantFP(V, DL, VT);
2964       break;
2965     }
2966     case ISD::FP_EXTEND: {
2967       bool ignored;
2968       // This can return overflow, underflow, or inexact; we don't care.
2969       // FIXME need to be more flexible about rounding mode.
2970       (void)V.convert(EVTToAPFloatSemantics(VT),
2971                       APFloat::rmNearestTiesToEven, &ignored);
2972       return getConstantFP(V, DL, VT);
2973     }
2974     case ISD::FP_TO_SINT:
2975     case ISD::FP_TO_UINT: {
2976       integerPart x[2];
2977       bool ignored;
2978       static_assert(integerPartWidth >= 64, "APFloat parts too small!");
2979       // FIXME need to be more flexible about rounding mode.
2980       APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2981                             Opcode==ISD::FP_TO_SINT,
2982                             APFloat::rmTowardZero, &ignored);
2983       if (s==APFloat::opInvalidOp)     // inexact is OK, in fact usual
2984         break;
2985       APInt api(VT.getSizeInBits(), x);
2986       return getConstant(api, DL, VT);
2987     }
2988     case ISD::BITCAST:
2989       if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
2990         return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
2991       else if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2992         return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
2993       else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2994         return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
2995       break;
2996     }
2997   }
2998 
2999   // Constant fold unary operations with a vector integer or float operand.
3000   if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Operand)) {
3001     if (BV->isConstant()) {
3002       switch (Opcode) {
3003       default:
3004         // FIXME: Entirely reasonable to perform folding of other unary
3005         // operations here as the need arises.
3006         break;
3007       case ISD::FNEG:
3008       case ISD::FABS:
3009       case ISD::FCEIL:
3010       case ISD::FTRUNC:
3011       case ISD::FFLOOR:
3012       case ISD::FP_EXTEND:
3013       case ISD::FP_TO_SINT:
3014       case ISD::FP_TO_UINT:
3015       case ISD::TRUNCATE:
3016       case ISD::UINT_TO_FP:
3017       case ISD::SINT_TO_FP:
3018       case ISD::BSWAP:
3019       case ISD::CTLZ:
3020       case ISD::CTLZ_ZERO_UNDEF:
3021       case ISD::CTTZ:
3022       case ISD::CTTZ_ZERO_UNDEF:
3023       case ISD::CTPOP: {
3024         SDValue Ops = { Operand };
3025         if (SDValue Fold = FoldConstantVectorArithmetic(Opcode, DL, VT, Ops))
3026           return Fold;
3027       }
3028       }
3029     }
3030   }
3031 
3032   unsigned OpOpcode = Operand.getNode()->getOpcode();
3033   switch (Opcode) {
3034   case ISD::TokenFactor:
3035   case ISD::MERGE_VALUES:
3036   case ISD::CONCAT_VECTORS:
3037     return Operand;         // Factor, merge or concat of one node?  No need.
3038   case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
3039   case ISD::FP_EXTEND:
3040     assert(VT.isFloatingPoint() &&
3041            Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
3042     if (Operand.getValueType() == VT) return Operand;  // noop conversion.
3043     assert((!VT.isVector() ||
3044             VT.getVectorNumElements() ==
3045             Operand.getValueType().getVectorNumElements()) &&
3046            "Vector element count mismatch!");
3047     assert(Operand.getValueType().bitsLT(VT) &&
3048            "Invalid fpext node, dst < src!");
3049     if (Operand.isUndef())
3050       return getUNDEF(VT);
3051     break;
3052   case ISD::SIGN_EXTEND:
3053     assert(VT.isInteger() && Operand.getValueType().isInteger() &&
3054            "Invalid SIGN_EXTEND!");
3055     if (Operand.getValueType() == VT) return Operand;   // noop extension
3056     assert((!VT.isVector() ||
3057             VT.getVectorNumElements() ==
3058             Operand.getValueType().getVectorNumElements()) &&
3059            "Vector element count mismatch!");
3060     assert(Operand.getValueType().bitsLT(VT) &&
3061            "Invalid sext node, dst < src!");
3062     if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
3063       return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
3064     else if (OpOpcode == ISD::UNDEF)
3065       // sext(undef) = 0, because the top bits will all be the same.
3066       return getConstant(0, DL, VT);
3067     break;
3068   case ISD::ZERO_EXTEND:
3069     assert(VT.isInteger() && Operand.getValueType().isInteger() &&
3070            "Invalid ZERO_EXTEND!");
3071     if (Operand.getValueType() == VT) return Operand;   // noop extension
3072     assert((!VT.isVector() ||
3073             VT.getVectorNumElements() ==
3074             Operand.getValueType().getVectorNumElements()) &&
3075            "Vector element count mismatch!");
3076     assert(Operand.getValueType().bitsLT(VT) &&
3077            "Invalid zext node, dst < src!");
3078     if (OpOpcode == ISD::ZERO_EXTEND)   // (zext (zext x)) -> (zext x)
3079       return getNode(ISD::ZERO_EXTEND, DL, VT,
3080                      Operand.getNode()->getOperand(0));
3081     else if (OpOpcode == ISD::UNDEF)
3082       // zext(undef) = 0, because the top bits will be zero.
3083       return getConstant(0, DL, VT);
3084     break;
3085   case ISD::ANY_EXTEND:
3086     assert(VT.isInteger() && Operand.getValueType().isInteger() &&
3087            "Invalid ANY_EXTEND!");
3088     if (Operand.getValueType() == VT) return Operand;   // noop extension
3089     assert((!VT.isVector() ||
3090             VT.getVectorNumElements() ==
3091             Operand.getValueType().getVectorNumElements()) &&
3092            "Vector element count mismatch!");
3093     assert(Operand.getValueType().bitsLT(VT) &&
3094            "Invalid anyext node, dst < src!");
3095 
3096     if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
3097         OpOpcode == ISD::ANY_EXTEND)
3098       // (ext (zext x)) -> (zext x)  and  (ext (sext x)) -> (sext x)
3099       return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
3100     else if (OpOpcode == ISD::UNDEF)
3101       return getUNDEF(VT);
3102 
3103     // (ext (trunx x)) -> x
3104     if (OpOpcode == ISD::TRUNCATE) {
3105       SDValue OpOp = Operand.getNode()->getOperand(0);
3106       if (OpOp.getValueType() == VT)
3107         return OpOp;
3108     }
3109     break;
3110   case ISD::TRUNCATE:
3111     assert(VT.isInteger() && Operand.getValueType().isInteger() &&
3112            "Invalid TRUNCATE!");
3113     if (Operand.getValueType() == VT) return Operand;   // noop truncate
3114     assert((!VT.isVector() ||
3115             VT.getVectorNumElements() ==
3116             Operand.getValueType().getVectorNumElements()) &&
3117            "Vector element count mismatch!");
3118     assert(Operand.getValueType().bitsGT(VT) &&
3119            "Invalid truncate node, src < dst!");
3120     if (OpOpcode == ISD::TRUNCATE)
3121       return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
3122     if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
3123         OpOpcode == ISD::ANY_EXTEND) {
3124       // If the source is smaller than the dest, we still need an extend.
3125       if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
3126             .bitsLT(VT.getScalarType()))
3127         return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
3128       if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
3129         return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
3130       return Operand.getNode()->getOperand(0);
3131     }
3132     if (OpOpcode == ISD::UNDEF)
3133       return getUNDEF(VT);
3134     break;
3135   case ISD::BSWAP:
3136     assert(VT.isInteger() && VT == Operand.getValueType() &&
3137            "Invalid BSWAP!");
3138     assert((VT.getScalarSizeInBits() % 16 == 0) &&
3139            "BSWAP types must be a multiple of 16 bits!");
3140     if (OpOpcode == ISD::UNDEF)
3141       return getUNDEF(VT);
3142     break;
3143   case ISD::BITCAST:
3144     // Basic sanity checking.
3145     assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
3146            && "Cannot BITCAST between types of different sizes!");
3147     if (VT == Operand.getValueType()) return Operand;  // noop conversion.
3148     if (OpOpcode == ISD::BITCAST)  // bitconv(bitconv(x)) -> bitconv(x)
3149       return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
3150     if (OpOpcode == ISD::UNDEF)
3151       return getUNDEF(VT);
3152     break;
3153   case ISD::SCALAR_TO_VECTOR:
3154     assert(VT.isVector() && !Operand.getValueType().isVector() &&
3155            (VT.getVectorElementType() == Operand.getValueType() ||
3156             (VT.getVectorElementType().isInteger() &&
3157              Operand.getValueType().isInteger() &&
3158              VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
3159            "Illegal SCALAR_TO_VECTOR node!");
3160     if (OpOpcode == ISD::UNDEF)
3161       return getUNDEF(VT);
3162     // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
3163     if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
3164         isa<ConstantSDNode>(Operand.getOperand(1)) &&
3165         Operand.getConstantOperandVal(1) == 0 &&
3166         Operand.getOperand(0).getValueType() == VT)
3167       return Operand.getOperand(0);
3168     break;
3169   case ISD::FNEG:
3170     // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
3171     if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
3172       // FIXME: FNEG has no fast-math-flags to propagate; use the FSUB's flags?
3173       return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
3174                        Operand.getNode()->getOperand(0),
3175                        &cast<BinaryWithFlagsSDNode>(Operand.getNode())->Flags);
3176     if (OpOpcode == ISD::FNEG)  // --X -> X
3177       return Operand.getNode()->getOperand(0);
3178     break;
3179   case ISD::FABS:
3180     if (OpOpcode == ISD::FNEG)  // abs(-X) -> abs(X)
3181       return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
3182     break;
3183   }
3184 
3185   SDNode *N;
3186   SDVTList VTs = getVTList(VT);
3187   SDValue Ops[] = {Operand};
3188   if (VT != MVT::Glue) { // Don't CSE flag producing nodes
3189     FoldingSetNodeID ID;
3190     AddNodeIDNode(ID, Opcode, VTs, Ops);
3191     void *IP = nullptr;
3192     if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP))
3193       return SDValue(E, 0);
3194 
3195     N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
3196     createOperands(N, Ops);
3197     CSEMap.InsertNode(N, IP);
3198   } else {
3199     N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
3200     createOperands(N, Ops);
3201   }
3202 
3203   InsertNode(N);
3204   return SDValue(N, 0);
3205 }
3206 
3207 static std::pair<APInt, bool> FoldValue(unsigned Opcode, const APInt &C1,
3208                                         const APInt &C2) {
3209   switch (Opcode) {
3210   case ISD::ADD:  return std::make_pair(C1 + C2, true);
3211   case ISD::SUB:  return std::make_pair(C1 - C2, true);
3212   case ISD::MUL:  return std::make_pair(C1 * C2, true);
3213   case ISD::AND:  return std::make_pair(C1 & C2, true);
3214   case ISD::OR:   return std::make_pair(C1 | C2, true);
3215   case ISD::XOR:  return std::make_pair(C1 ^ C2, true);
3216   case ISD::SHL:  return std::make_pair(C1 << C2, true);
3217   case ISD::SRL:  return std::make_pair(C1.lshr(C2), true);
3218   case ISD::SRA:  return std::make_pair(C1.ashr(C2), true);
3219   case ISD::ROTL: return std::make_pair(C1.rotl(C2), true);
3220   case ISD::ROTR: return std::make_pair(C1.rotr(C2), true);
3221   case ISD::SMIN: return std::make_pair(C1.sle(C2) ? C1 : C2, true);
3222   case ISD::SMAX: return std::make_pair(C1.sge(C2) ? C1 : C2, true);
3223   case ISD::UMIN: return std::make_pair(C1.ule(C2) ? C1 : C2, true);
3224   case ISD::UMAX: return std::make_pair(C1.uge(C2) ? C1 : C2, true);
3225   case ISD::UDIV:
3226     if (!C2.getBoolValue())
3227       break;
3228     return std::make_pair(C1.udiv(C2), true);
3229   case ISD::UREM:
3230     if (!C2.getBoolValue())
3231       break;
3232     return std::make_pair(C1.urem(C2), true);
3233   case ISD::SDIV:
3234     if (!C2.getBoolValue())
3235       break;
3236     return std::make_pair(C1.sdiv(C2), true);
3237   case ISD::SREM:
3238     if (!C2.getBoolValue())
3239       break;
3240     return std::make_pair(C1.srem(C2), true);
3241   }
3242   return std::make_pair(APInt(1, 0), false);
3243 }
3244 
3245 SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, SDLoc DL, EVT VT,
3246                                              const ConstantSDNode *Cst1,
3247                                              const ConstantSDNode *Cst2) {
3248   if (Cst1->isOpaque() || Cst2->isOpaque())
3249     return SDValue();
3250 
3251   std::pair<APInt, bool> Folded = FoldValue(Opcode, Cst1->getAPIntValue(),
3252                                             Cst2->getAPIntValue());
3253   if (!Folded.second)
3254     return SDValue();
3255   return getConstant(Folded.first, DL, VT);
3256 }
3257 
3258 SDValue SelectionDAG::FoldSymbolOffset(unsigned Opcode, EVT VT,
3259                                        const GlobalAddressSDNode *GA,
3260                                        const SDNode *N2) {
3261   if (GA->getOpcode() != ISD::GlobalAddress)
3262     return SDValue();
3263   if (!TLI->isOffsetFoldingLegal(GA))
3264     return SDValue();
3265   const ConstantSDNode *Cst2 = dyn_cast<ConstantSDNode>(N2);
3266   if (!Cst2)
3267     return SDValue();
3268   int64_t Offset = Cst2->getSExtValue();
3269   switch (Opcode) {
3270   case ISD::ADD: break;
3271   case ISD::SUB: Offset = -uint64_t(Offset); break;
3272   default: return SDValue();
3273   }
3274   return getGlobalAddress(GA->getGlobal(), SDLoc(Cst2), VT,
3275                           GA->getOffset() + uint64_t(Offset));
3276 }
3277 
3278 SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, SDLoc DL, EVT VT,
3279                                              SDNode *Cst1, SDNode *Cst2) {
3280   // If the opcode is a target-specific ISD node, there's nothing we can
3281   // do here and the operand rules may not line up with the below, so
3282   // bail early.
3283   if (Opcode >= ISD::BUILTIN_OP_END)
3284     return SDValue();
3285 
3286   // Handle the case of two scalars.
3287   if (const ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1)) {
3288     if (const ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2)) {
3289       SDValue Folded = FoldConstantArithmetic(Opcode, DL, VT, Scalar1, Scalar2);
3290       assert((!Folded || !VT.isVector()) &&
3291              "Can't fold vectors ops with scalar operands");
3292       return Folded;
3293     }
3294   }
3295 
3296   // fold (add Sym, c) -> Sym+c
3297   if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Cst1))
3298     return FoldSymbolOffset(Opcode, VT, GA, Cst2);
3299   if (isCommutativeBinOp(Opcode))
3300     if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Cst2))
3301       return FoldSymbolOffset(Opcode, VT, GA, Cst1);
3302 
3303   // For vectors extract each constant element into Inputs so we can constant
3304   // fold them individually.
3305   BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
3306   BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
3307   if (!BV1 || !BV2)
3308     return SDValue();
3309 
3310   assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
3311 
3312   EVT SVT = VT.getScalarType();
3313   SmallVector<SDValue, 4> Outputs;
3314   for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
3315     ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
3316     ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
3317     if (!V1 || !V2) // Not a constant, bail.
3318       return SDValue();
3319 
3320     if (V1->isOpaque() || V2->isOpaque())
3321       return SDValue();
3322 
3323     // Avoid BUILD_VECTOR nodes that perform implicit truncation.
3324     // FIXME: This is valid and could be handled by truncating the APInts.
3325     if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
3326       return SDValue();
3327 
3328     // Fold one vector element.
3329     std::pair<APInt, bool> Folded = FoldValue(Opcode, V1->getAPIntValue(),
3330                                               V2->getAPIntValue());
3331     if (!Folded.second)
3332       return SDValue();
3333     Outputs.push_back(getConstant(Folded.first, DL, SVT));
3334   }
3335 
3336   assert(VT.getVectorNumElements() == Outputs.size() &&
3337          "Vector size mismatch!");
3338 
3339   // We may have a vector type but a scalar result. Create a splat.
3340   Outputs.resize(VT.getVectorNumElements(), Outputs.back());
3341 
3342   // Build a big vector out of the scalar elements we generated.
3343   return getBuildVector(VT, SDLoc(), Outputs);
3344 }
3345 
3346 SDValue SelectionDAG::FoldConstantVectorArithmetic(unsigned Opcode, SDLoc DL,
3347                                                    EVT VT,
3348                                                    ArrayRef<SDValue> Ops,
3349                                                    const SDNodeFlags *Flags) {
3350   // If the opcode is a target-specific ISD node, there's nothing we can
3351   // do here and the operand rules may not line up with the below, so
3352   // bail early.
3353   if (Opcode >= ISD::BUILTIN_OP_END)
3354     return SDValue();
3355 
3356   // We can only fold vectors - maybe merge with FoldConstantArithmetic someday?
3357   if (!VT.isVector())
3358     return SDValue();
3359 
3360   unsigned NumElts = VT.getVectorNumElements();
3361 
3362   auto IsScalarOrSameVectorSize = [&](const SDValue &Op) {
3363     return !Op.getValueType().isVector() ||
3364            Op.getValueType().getVectorNumElements() == NumElts;
3365   };
3366 
3367   auto IsConstantBuildVectorOrUndef = [&](const SDValue &Op) {
3368     BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Op);
3369     return (Op.isUndef()) || (Op.getOpcode() == ISD::CONDCODE) ||
3370            (BV && BV->isConstant());
3371   };
3372 
3373   // All operands must be vector types with the same number of elements as
3374   // the result type and must be either UNDEF or a build vector of constant
3375   // or UNDEF scalars.
3376   if (!std::all_of(Ops.begin(), Ops.end(), IsConstantBuildVectorOrUndef) ||
3377       !std::all_of(Ops.begin(), Ops.end(), IsScalarOrSameVectorSize))
3378     return SDValue();
3379 
3380   // If we are comparing vectors, then the result needs to be a i1 boolean
3381   // that is then sign-extended back to the legal result type.
3382   EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
3383 
3384   // Find legal integer scalar type for constant promotion and
3385   // ensure that its scalar size is at least as large as source.
3386   EVT LegalSVT = VT.getScalarType();
3387   if (LegalSVT.isInteger()) {
3388     LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3389     if (LegalSVT.bitsLT(VT.getScalarType()))
3390       return SDValue();
3391   }
3392 
3393   // Constant fold each scalar lane separately.
3394   SmallVector<SDValue, 4> ScalarResults;
3395   for (unsigned i = 0; i != NumElts; i++) {
3396     SmallVector<SDValue, 4> ScalarOps;
3397     for (SDValue Op : Ops) {
3398       EVT InSVT = Op.getValueType().getScalarType();
3399       BuildVectorSDNode *InBV = dyn_cast<BuildVectorSDNode>(Op);
3400       if (!InBV) {
3401         // We've checked that this is UNDEF or a constant of some kind.
3402         if (Op.isUndef())
3403           ScalarOps.push_back(getUNDEF(InSVT));
3404         else
3405           ScalarOps.push_back(Op);
3406         continue;
3407       }
3408 
3409       SDValue ScalarOp = InBV->getOperand(i);
3410       EVT ScalarVT = ScalarOp.getValueType();
3411 
3412       // Build vector (integer) scalar operands may need implicit
3413       // truncation - do this before constant folding.
3414       if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT))
3415         ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
3416 
3417       ScalarOps.push_back(ScalarOp);
3418     }
3419 
3420     // Constant fold the scalar operands.
3421     SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
3422 
3423     // Legalize the (integer) scalar constant if necessary.
3424     if (LegalSVT != SVT)
3425       ScalarResult = getNode(ISD::SIGN_EXTEND, DL, LegalSVT, ScalarResult);
3426 
3427     // Scalar folding only succeeded if the result is a constant or UNDEF.
3428     if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
3429         ScalarResult.getOpcode() != ISD::ConstantFP)
3430       return SDValue();
3431     ScalarResults.push_back(ScalarResult);
3432   }
3433 
3434   return getBuildVector(VT, DL, ScalarResults);
3435 }
3436 
3437 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
3438                               SDValue N2, const SDNodeFlags *Flags) {
3439   ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3440   ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
3441   ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3442   ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3443 
3444   // Canonicalize constant to RHS if commutative.
3445   if (isCommutativeBinOp(Opcode)) {
3446     if (N1C && !N2C) {
3447       std::swap(N1C, N2C);
3448       std::swap(N1, N2);
3449     } else if (N1CFP && !N2CFP) {
3450       std::swap(N1CFP, N2CFP);
3451       std::swap(N1, N2);
3452     }
3453   }
3454 
3455   switch (Opcode) {
3456   default: break;
3457   case ISD::TokenFactor:
3458     assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
3459            N2.getValueType() == MVT::Other && "Invalid token factor!");
3460     // Fold trivial token factors.
3461     if (N1.getOpcode() == ISD::EntryToken) return N2;
3462     if (N2.getOpcode() == ISD::EntryToken) return N1;
3463     if (N1 == N2) return N1;
3464     break;
3465   case ISD::CONCAT_VECTORS: {
3466     // Attempt to fold CONCAT_VECTORS into BUILD_VECTOR or UNDEF.
3467     SDValue Ops[] = {N1, N2};
3468     if (SDValue V = FoldCONCAT_VECTORS(DL, VT, Ops, *this))
3469       return V;
3470     break;
3471   }
3472   case ISD::AND:
3473     assert(VT.isInteger() && "This operator does not apply to FP types!");
3474     assert(N1.getValueType() == N2.getValueType() &&
3475            N1.getValueType() == VT && "Binary operator types must match!");
3476     // (X & 0) -> 0.  This commonly occurs when legalizing i64 values, so it's
3477     // worth handling here.
3478     if (N2C && N2C->isNullValue())
3479       return N2;
3480     if (N2C && N2C->isAllOnesValue())  // X & -1 -> X
3481       return N1;
3482     break;
3483   case ISD::OR:
3484   case ISD::XOR:
3485   case ISD::ADD:
3486   case ISD::SUB:
3487     assert(VT.isInteger() && "This operator does not apply to FP types!");
3488     assert(N1.getValueType() == N2.getValueType() &&
3489            N1.getValueType() == VT && "Binary operator types must match!");
3490     // (X ^|+- 0) -> X.  This commonly occurs when legalizing i64 values, so
3491     // it's worth handling here.
3492     if (N2C && N2C->isNullValue())
3493       return N1;
3494     break;
3495   case ISD::UDIV:
3496   case ISD::UREM:
3497   case ISD::MULHU:
3498   case ISD::MULHS:
3499   case ISD::MUL:
3500   case ISD::SDIV:
3501   case ISD::SREM:
3502   case ISD::SMIN:
3503   case ISD::SMAX:
3504   case ISD::UMIN:
3505   case ISD::UMAX:
3506     assert(VT.isInteger() && "This operator does not apply to FP types!");
3507     assert(N1.getValueType() == N2.getValueType() &&
3508            N1.getValueType() == VT && "Binary operator types must match!");
3509     break;
3510   case ISD::FADD:
3511   case ISD::FSUB:
3512   case ISD::FMUL:
3513   case ISD::FDIV:
3514   case ISD::FREM:
3515     if (getTarget().Options.UnsafeFPMath) {
3516       if (Opcode == ISD::FADD) {
3517         // x+0 --> x
3518         if (N2CFP && N2CFP->getValueAPF().isZero())
3519           return N1;
3520       } else if (Opcode == ISD::FSUB) {
3521         // x-0 --> x
3522         if (N2CFP && N2CFP->getValueAPF().isZero())
3523           return N1;
3524       } else if (Opcode == ISD::FMUL) {
3525         // x*0 --> 0
3526         if (N2CFP && N2CFP->isZero())
3527           return N2;
3528         // x*1 --> x
3529         if (N2CFP && N2CFP->isExactlyValue(1.0))
3530           return N1;
3531       }
3532     }
3533     assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
3534     assert(N1.getValueType() == N2.getValueType() &&
3535            N1.getValueType() == VT && "Binary operator types must match!");
3536     break;
3537   case ISD::FCOPYSIGN:   // N1 and result must match.  N1/N2 need not match.
3538     assert(N1.getValueType() == VT &&
3539            N1.getValueType().isFloatingPoint() &&
3540            N2.getValueType().isFloatingPoint() &&
3541            "Invalid FCOPYSIGN!");
3542     break;
3543   case ISD::SHL:
3544   case ISD::SRA:
3545   case ISD::SRL:
3546   case ISD::ROTL:
3547   case ISD::ROTR:
3548     assert(VT == N1.getValueType() &&
3549            "Shift operators return type must be the same as their first arg");
3550     assert(VT.isInteger() && N2.getValueType().isInteger() &&
3551            "Shifts only work on integers");
3552     assert((!VT.isVector() || VT == N2.getValueType()) &&
3553            "Vector shift amounts must be in the same as their first arg");
3554     // Verify that the shift amount VT is bit enough to hold valid shift
3555     // amounts.  This catches things like trying to shift an i1024 value by an
3556     // i8, which is easy to fall into in generic code that uses
3557     // TLI.getShiftAmount().
3558     assert(N2.getValueType().getSizeInBits() >=
3559                    Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
3560            "Invalid use of small shift amount with oversized value!");
3561 
3562     // Always fold shifts of i1 values so the code generator doesn't need to
3563     // handle them.  Since we know the size of the shift has to be less than the
3564     // size of the value, the shift/rotate count is guaranteed to be zero.
3565     if (VT == MVT::i1)
3566       return N1;
3567     if (N2C && N2C->isNullValue())
3568       return N1;
3569     break;
3570   case ISD::FP_ROUND_INREG: {
3571     EVT EVT = cast<VTSDNode>(N2)->getVT();
3572     assert(VT == N1.getValueType() && "Not an inreg round!");
3573     assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
3574            "Cannot FP_ROUND_INREG integer types");
3575     assert(EVT.isVector() == VT.isVector() &&
3576            "FP_ROUND_INREG type should be vector iff the operand "
3577            "type is vector!");
3578     assert((!EVT.isVector() ||
3579             EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3580            "Vector element counts must match in FP_ROUND_INREG");
3581     assert(EVT.bitsLE(VT) && "Not rounding down!");
3582     (void)EVT;
3583     if (cast<VTSDNode>(N2)->getVT() == VT) return N1;  // Not actually rounding.
3584     break;
3585   }
3586   case ISD::FP_ROUND:
3587     assert(VT.isFloatingPoint() &&
3588            N1.getValueType().isFloatingPoint() &&
3589            VT.bitsLE(N1.getValueType()) &&
3590            N2C && "Invalid FP_ROUND!");
3591     if (N1.getValueType() == VT) return N1;  // noop conversion.
3592     break;
3593   case ISD::AssertSext:
3594   case ISD::AssertZext: {
3595     EVT EVT = cast<VTSDNode>(N2)->getVT();
3596     assert(VT == N1.getValueType() && "Not an inreg extend!");
3597     assert(VT.isInteger() && EVT.isInteger() &&
3598            "Cannot *_EXTEND_INREG FP types");
3599     assert(!EVT.isVector() &&
3600            "AssertSExt/AssertZExt type should be the vector element type "
3601            "rather than the vector type!");
3602     assert(EVT.bitsLE(VT) && "Not extending!");
3603     if (VT == EVT) return N1; // noop assertion.
3604     break;
3605   }
3606   case ISD::SIGN_EXTEND_INREG: {
3607     EVT EVT = cast<VTSDNode>(N2)->getVT();
3608     assert(VT == N1.getValueType() && "Not an inreg extend!");
3609     assert(VT.isInteger() && EVT.isInteger() &&
3610            "Cannot *_EXTEND_INREG FP types");
3611     assert(EVT.isVector() == VT.isVector() &&
3612            "SIGN_EXTEND_INREG type should be vector iff the operand "
3613            "type is vector!");
3614     assert((!EVT.isVector() ||
3615             EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3616            "Vector element counts must match in SIGN_EXTEND_INREG");
3617     assert(EVT.bitsLE(VT) && "Not extending!");
3618     if (EVT == VT) return N1;  // Not actually extending
3619 
3620     auto SignExtendInReg = [&](APInt Val) {
3621       unsigned FromBits = EVT.getScalarType().getSizeInBits();
3622       Val <<= Val.getBitWidth() - FromBits;
3623       Val = Val.ashr(Val.getBitWidth() - FromBits);
3624       return getConstant(Val, DL, VT.getScalarType());
3625     };
3626 
3627     if (N1C) {
3628       APInt Val = N1C->getAPIntValue();
3629       return SignExtendInReg(Val);
3630     }
3631     if (ISD::isBuildVectorOfConstantSDNodes(N1.getNode())) {
3632       SmallVector<SDValue, 8> Ops;
3633       for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
3634         SDValue Op = N1.getOperand(i);
3635         if (Op.isUndef()) {
3636           Ops.push_back(getUNDEF(VT.getScalarType()));
3637           continue;
3638         }
3639         if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3640           APInt Val = C->getAPIntValue();
3641           Val = Val.zextOrTrunc(VT.getScalarSizeInBits());
3642           Ops.push_back(SignExtendInReg(Val));
3643           continue;
3644         }
3645         break;
3646       }
3647       if (Ops.size() == VT.getVectorNumElements())
3648         return getBuildVector(VT, DL, Ops);
3649     }
3650     break;
3651   }
3652   case ISD::EXTRACT_VECTOR_ELT:
3653     // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3654     if (N1.isUndef())
3655       return getUNDEF(VT);
3656 
3657     // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF
3658     if (N2C && N2C->getZExtValue() >= N1.getValueType().getVectorNumElements())
3659       return getUNDEF(VT);
3660 
3661     // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3662     // expanding copies of large vectors from registers.
3663     if (N2C &&
3664         N1.getOpcode() == ISD::CONCAT_VECTORS &&
3665         N1.getNumOperands() > 0) {
3666       unsigned Factor =
3667         N1.getOperand(0).getValueType().getVectorNumElements();
3668       return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
3669                      N1.getOperand(N2C->getZExtValue() / Factor),
3670                      getConstant(N2C->getZExtValue() % Factor, DL,
3671                                  N2.getValueType()));
3672     }
3673 
3674     // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3675     // expanding large vector constants.
3676     if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3677       SDValue Elt = N1.getOperand(N2C->getZExtValue());
3678 
3679       if (VT != Elt.getValueType())
3680         // If the vector element type is not legal, the BUILD_VECTOR operands
3681         // are promoted and implicitly truncated, and the result implicitly
3682         // extended. Make that explicit here.
3683         Elt = getAnyExtOrTrunc(Elt, DL, VT);
3684 
3685       return Elt;
3686     }
3687 
3688     // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3689     // operations are lowered to scalars.
3690     if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
3691       // If the indices are the same, return the inserted element else
3692       // if the indices are known different, extract the element from
3693       // the original vector.
3694       SDValue N1Op2 = N1.getOperand(2);
3695       ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2);
3696 
3697       if (N1Op2C && N2C) {
3698         if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3699           if (VT == N1.getOperand(1).getValueType())
3700             return N1.getOperand(1);
3701           else
3702             return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3703         }
3704 
3705         return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
3706       }
3707     }
3708     break;
3709   case ISD::EXTRACT_ELEMENT:
3710     assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
3711     assert(!N1.getValueType().isVector() && !VT.isVector() &&
3712            (N1.getValueType().isInteger() == VT.isInteger()) &&
3713            N1.getValueType() != VT &&
3714            "Wrong types for EXTRACT_ELEMENT!");
3715 
3716     // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3717     // 64-bit integers into 32-bit parts.  Instead of building the extract of
3718     // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
3719     if (N1.getOpcode() == ISD::BUILD_PAIR)
3720       return N1.getOperand(N2C->getZExtValue());
3721 
3722     // EXTRACT_ELEMENT of a constant int is also very common.
3723     if (N1C) {
3724       unsigned ElementSize = VT.getSizeInBits();
3725       unsigned Shift = ElementSize * N2C->getZExtValue();
3726       APInt ShiftedVal = N1C->getAPIntValue().lshr(Shift);
3727       return getConstant(ShiftedVal.trunc(ElementSize), DL, VT);
3728     }
3729     break;
3730   case ISD::EXTRACT_SUBVECTOR:
3731     if (VT.isSimple() && N1.getValueType().isSimple()) {
3732       assert(VT.isVector() && N1.getValueType().isVector() &&
3733              "Extract subvector VTs must be a vectors!");
3734       assert(VT.getVectorElementType() ==
3735              N1.getValueType().getVectorElementType() &&
3736              "Extract subvector VTs must have the same element type!");
3737       assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
3738              "Extract subvector must be from larger vector to smaller vector!");
3739 
3740       if (N2C) {
3741         assert((VT.getVectorNumElements() + N2C->getZExtValue()
3742                 <= N1.getValueType().getVectorNumElements())
3743                && "Extract subvector overflow!");
3744       }
3745 
3746       // Trivial extraction.
3747       if (VT.getSimpleVT() == N1.getSimpleValueType())
3748         return N1;
3749     }
3750     break;
3751   }
3752 
3753   // Perform trivial constant folding.
3754   if (SDValue SV =
3755           FoldConstantArithmetic(Opcode, DL, VT, N1.getNode(), N2.getNode()))
3756     return SV;
3757 
3758   // Constant fold FP operations.
3759   bool HasFPExceptions = TLI->hasFloatingPointExceptions();
3760   if (N1CFP) {
3761     if (N2CFP) {
3762       APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3763       APFloat::opStatus s;
3764       switch (Opcode) {
3765       case ISD::FADD:
3766         s = V1.add(V2, APFloat::rmNearestTiesToEven);
3767         if (!HasFPExceptions || s != APFloat::opInvalidOp)
3768           return getConstantFP(V1, DL, VT);
3769         break;
3770       case ISD::FSUB:
3771         s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3772         if (!HasFPExceptions || s!=APFloat::opInvalidOp)
3773           return getConstantFP(V1, DL, VT);
3774         break;
3775       case ISD::FMUL:
3776         s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3777         if (!HasFPExceptions || s!=APFloat::opInvalidOp)
3778           return getConstantFP(V1, DL, VT);
3779         break;
3780       case ISD::FDIV:
3781         s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3782         if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3783                                  s!=APFloat::opDivByZero)) {
3784           return getConstantFP(V1, DL, VT);
3785         }
3786         break;
3787       case ISD::FREM :
3788         s = V1.mod(V2);
3789         if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3790                                  s!=APFloat::opDivByZero)) {
3791           return getConstantFP(V1, DL, VT);
3792         }
3793         break;
3794       case ISD::FCOPYSIGN:
3795         V1.copySign(V2);
3796         return getConstantFP(V1, DL, VT);
3797       default: break;
3798       }
3799     }
3800 
3801     if (Opcode == ISD::FP_ROUND) {
3802       APFloat V = N1CFP->getValueAPF();    // make copy
3803       bool ignored;
3804       // This can return overflow, underflow, or inexact; we don't care.
3805       // FIXME need to be more flexible about rounding mode.
3806       (void)V.convert(EVTToAPFloatSemantics(VT),
3807                       APFloat::rmNearestTiesToEven, &ignored);
3808       return getConstantFP(V, DL, VT);
3809     }
3810   }
3811 
3812   // Canonicalize an UNDEF to the RHS, even over a constant.
3813   if (N1.isUndef()) {
3814     if (isCommutativeBinOp(Opcode)) {
3815       std::swap(N1, N2);
3816     } else {
3817       switch (Opcode) {
3818       case ISD::FP_ROUND_INREG:
3819       case ISD::SIGN_EXTEND_INREG:
3820       case ISD::SUB:
3821       case ISD::FSUB:
3822       case ISD::FDIV:
3823       case ISD::FREM:
3824       case ISD::SRA:
3825         return N1;     // fold op(undef, arg2) -> undef
3826       case ISD::UDIV:
3827       case ISD::SDIV:
3828       case ISD::UREM:
3829       case ISD::SREM:
3830       case ISD::SRL:
3831       case ISD::SHL:
3832         if (!VT.isVector())
3833           return getConstant(0, DL, VT);    // fold op(undef, arg2) -> 0
3834         // For vectors, we can't easily build an all zero vector, just return
3835         // the LHS.
3836         return N2;
3837       }
3838     }
3839   }
3840 
3841   // Fold a bunch of operators when the RHS is undef.
3842   if (N2.isUndef()) {
3843     switch (Opcode) {
3844     case ISD::XOR:
3845       if (N1.isUndef())
3846         // Handle undef ^ undef -> 0 special case. This is a common
3847         // idiom (misuse).
3848         return getConstant(0, DL, VT);
3849       // fallthrough
3850     case ISD::ADD:
3851     case ISD::ADDC:
3852     case ISD::ADDE:
3853     case ISD::SUB:
3854     case ISD::UDIV:
3855     case ISD::SDIV:
3856     case ISD::UREM:
3857     case ISD::SREM:
3858       return N2;       // fold op(arg1, undef) -> undef
3859     case ISD::FADD:
3860     case ISD::FSUB:
3861     case ISD::FMUL:
3862     case ISD::FDIV:
3863     case ISD::FREM:
3864       if (getTarget().Options.UnsafeFPMath)
3865         return N2;
3866       break;
3867     case ISD::MUL:
3868     case ISD::AND:
3869     case ISD::SRL:
3870     case ISD::SHL:
3871       if (!VT.isVector())
3872         return getConstant(0, DL, VT);  // fold op(arg1, undef) -> 0
3873       // For vectors, we can't easily build an all zero vector, just return
3874       // the LHS.
3875       return N1;
3876     case ISD::OR:
3877       if (!VT.isVector())
3878         return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), DL, VT);
3879       // For vectors, we can't easily build an all one vector, just return
3880       // the LHS.
3881       return N1;
3882     case ISD::SRA:
3883       return N1;
3884     }
3885   }
3886 
3887   // Memoize this node if possible.
3888   SDNode *N;
3889   SDVTList VTs = getVTList(VT);
3890   if (VT != MVT::Glue) {
3891     SDValue Ops[] = {N1, N2};
3892     FoldingSetNodeID ID;
3893     AddNodeIDNode(ID, Opcode, VTs, Ops);
3894     void *IP = nullptr;
3895     if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP)) {
3896       if (Flags)
3897         E->intersectFlagsWith(Flags);
3898       return SDValue(E, 0);
3899     }
3900 
3901     N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, Flags);
3902     CSEMap.InsertNode(N, IP);
3903   } else {
3904     N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, Flags);
3905   }
3906 
3907   InsertNode(N);
3908   return SDValue(N, 0);
3909 }
3910 
3911 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
3912                               SDValue N1, SDValue N2, SDValue N3) {
3913   // Perform various simplifications.
3914   switch (Opcode) {
3915   case ISD::FMA: {
3916     ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3917     ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3918     ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3919     if (N1CFP && N2CFP && N3CFP) {
3920       APFloat  V1 = N1CFP->getValueAPF();
3921       const APFloat &V2 = N2CFP->getValueAPF();
3922       const APFloat &V3 = N3CFP->getValueAPF();
3923       APFloat::opStatus s =
3924         V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
3925       if (!TLI->hasFloatingPointExceptions() || s != APFloat::opInvalidOp)
3926         return getConstantFP(V1, DL, VT);
3927     }
3928     break;
3929   }
3930   case ISD::CONCAT_VECTORS: {
3931     // Attempt to fold CONCAT_VECTORS into BUILD_VECTOR or UNDEF.
3932     SDValue Ops[] = {N1, N2, N3};
3933     if (SDValue V = FoldCONCAT_VECTORS(DL, VT, Ops, *this))
3934       return V;
3935     break;
3936   }
3937   case ISD::SETCC: {
3938     // Use FoldSetCC to simplify SETCC's.
3939     if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
3940       return V;
3941     // Vector constant folding.
3942     SDValue Ops[] = {N1, N2, N3};
3943     if (SDValue V = FoldConstantVectorArithmetic(Opcode, DL, VT, Ops))
3944       return V;
3945     break;
3946   }
3947   case ISD::SELECT:
3948     if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1)) {
3949      if (N1C->getZExtValue())
3950        return N2;             // select true, X, Y -> X
3951      return N3;             // select false, X, Y -> Y
3952     }
3953 
3954     if (N2 == N3) return N2;   // select C, X, X -> X
3955     break;
3956   case ISD::VECTOR_SHUFFLE:
3957     llvm_unreachable("should use getVectorShuffle constructor!");
3958   case ISD::INSERT_SUBVECTOR: {
3959     SDValue Index = N3;
3960     if (VT.isSimple() && N1.getValueType().isSimple()
3961         && N2.getValueType().isSimple()) {
3962       assert(VT.isVector() && N1.getValueType().isVector() &&
3963              N2.getValueType().isVector() &&
3964              "Insert subvector VTs must be a vectors");
3965       assert(VT == N1.getValueType() &&
3966              "Dest and insert subvector source types must match!");
3967       assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
3968              "Insert subvector must be from smaller vector to larger vector!");
3969       if (isa<ConstantSDNode>(Index)) {
3970         assert((N2.getValueType().getVectorNumElements() +
3971                 cast<ConstantSDNode>(Index)->getZExtValue()
3972                 <= VT.getVectorNumElements())
3973                && "Insert subvector overflow!");
3974       }
3975 
3976       // Trivial insertion.
3977       if (VT.getSimpleVT() == N2.getSimpleValueType())
3978         return N2;
3979     }
3980     break;
3981   }
3982   case ISD::BITCAST:
3983     // Fold bit_convert nodes from a type to themselves.
3984     if (N1.getValueType() == VT)
3985       return N1;
3986     break;
3987   }
3988 
3989   // Memoize node if it doesn't produce a flag.
3990   SDNode *N;
3991   SDVTList VTs = getVTList(VT);
3992   SDValue Ops[] = {N1, N2, N3};
3993   if (VT != MVT::Glue) {
3994     FoldingSetNodeID ID;
3995     AddNodeIDNode(ID, Opcode, VTs, Ops);
3996     void *IP = nullptr;
3997     if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP))
3998       return SDValue(E, 0);
3999 
4000     N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
4001     createOperands(N, Ops);
4002     CSEMap.InsertNode(N, IP);
4003   } else {
4004     N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
4005     createOperands(N, Ops);
4006   }
4007 
4008   InsertNode(N);
4009   return SDValue(N, 0);
4010 }
4011 
4012 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
4013                               SDValue N1, SDValue N2, SDValue N3,
4014                               SDValue N4) {
4015   SDValue Ops[] = { N1, N2, N3, N4 };
4016   return getNode(Opcode, DL, VT, Ops);
4017 }
4018 
4019 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
4020                               SDValue N1, SDValue N2, SDValue N3,
4021                               SDValue N4, SDValue N5) {
4022   SDValue Ops[] = { N1, N2, N3, N4, N5 };
4023   return getNode(Opcode, DL, VT, Ops);
4024 }
4025 
4026 /// getStackArgumentTokenFactor - Compute a TokenFactor to force all
4027 /// the incoming stack arguments to be loaded from the stack.
4028 SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
4029   SmallVector<SDValue, 8> ArgChains;
4030 
4031   // Include the original chain at the beginning of the list. When this is
4032   // used by target LowerCall hooks, this helps legalize find the
4033   // CALLSEQ_BEGIN node.
4034   ArgChains.push_back(Chain);
4035 
4036   // Add a chain value for each stack argument.
4037   for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
4038        UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
4039     if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
4040       if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
4041         if (FI->getIndex() < 0)
4042           ArgChains.push_back(SDValue(L, 1));
4043 
4044   // Build a tokenfactor for all the chains.
4045   return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
4046 }
4047 
4048 /// getMemsetValue - Vectorized representation of the memset value
4049 /// operand.
4050 static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
4051                               SDLoc dl) {
4052   assert(!Value.isUndef());
4053 
4054   unsigned NumBits = VT.getScalarType().getSizeInBits();
4055   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
4056     assert(C->getAPIntValue().getBitWidth() == 8);
4057     APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
4058     if (VT.isInteger())
4059       return DAG.getConstant(Val, dl, VT);
4060     return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), dl,
4061                              VT);
4062   }
4063 
4064   assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
4065   EVT IntVT = VT.getScalarType();
4066   if (!IntVT.isInteger())
4067     IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
4068 
4069   Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
4070   if (NumBits > 8) {
4071     // Use a multiplication with 0x010101... to extend the input to the
4072     // required length.
4073     APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
4074     Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
4075                         DAG.getConstant(Magic, dl, IntVT));
4076   }
4077 
4078   if (VT != Value.getValueType() && !VT.isInteger())
4079     Value = DAG.getBitcast(VT.getScalarType(), Value);
4080   if (VT != Value.getValueType())
4081     Value = DAG.getSplatBuildVector(VT, dl, Value);
4082 
4083   return Value;
4084 }
4085 
4086 /// getMemsetStringVal - Similar to getMemsetValue. Except this is only
4087 /// used when a memcpy is turned into a memset when the source is a constant
4088 /// string ptr.
4089 static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
4090                                   const TargetLowering &TLI, StringRef Str) {
4091   // Handle vector with all elements zero.
4092   if (Str.empty()) {
4093     if (VT.isInteger())
4094       return DAG.getConstant(0, dl, VT);
4095     else if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
4096       return DAG.getConstantFP(0.0, dl, VT);
4097     else if (VT.isVector()) {
4098       unsigned NumElts = VT.getVectorNumElements();
4099       MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
4100       return DAG.getNode(ISD::BITCAST, dl, VT,
4101                          DAG.getConstant(0, dl,
4102                                          EVT::getVectorVT(*DAG.getContext(),
4103                                                           EltVT, NumElts)));
4104     } else
4105       llvm_unreachable("Expected type!");
4106   }
4107 
4108   assert(!VT.isVector() && "Can't handle vector type here!");
4109   unsigned NumVTBits = VT.getSizeInBits();
4110   unsigned NumVTBytes = NumVTBits / 8;
4111   unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
4112 
4113   APInt Val(NumVTBits, 0);
4114   if (DAG.getDataLayout().isLittleEndian()) {
4115     for (unsigned i = 0; i != NumBytes; ++i)
4116       Val |= (uint64_t)(unsigned char)Str[i] << i*8;
4117   } else {
4118     for (unsigned i = 0; i != NumBytes; ++i)
4119       Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
4120   }
4121 
4122   // If the "cost" of materializing the integer immediate is less than the cost
4123   // of a load, then it is cost effective to turn the load into the immediate.
4124   Type *Ty = VT.getTypeForEVT(*DAG.getContext());
4125   if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
4126     return DAG.getConstant(Val, dl, VT);
4127   return SDValue(nullptr, 0);
4128 }
4129 
4130 SDValue SelectionDAG::getMemBasePlusOffset(SDValue Base, unsigned Offset,
4131                                            SDLoc DL) {
4132   EVT VT = Base.getValueType();
4133   return getNode(ISD::ADD, DL, VT, Base, getConstant(Offset, DL, VT));
4134 }
4135 
4136 /// isMemSrcFromString - Returns true if memcpy source is a string constant.
4137 ///
4138 static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
4139   uint64_t SrcDelta = 0;
4140   GlobalAddressSDNode *G = nullptr;
4141   if (Src.getOpcode() == ISD::GlobalAddress)
4142     G = cast<GlobalAddressSDNode>(Src);
4143   else if (Src.getOpcode() == ISD::ADD &&
4144            Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
4145            Src.getOperand(1).getOpcode() == ISD::Constant) {
4146     G = cast<GlobalAddressSDNode>(Src.getOperand(0));
4147     SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
4148   }
4149   if (!G)
4150     return false;
4151 
4152   return getConstantStringInfo(G->getGlobal(), Str,
4153                                SrcDelta + G->getOffset(), false);
4154 }
4155 
4156 /// Determines the optimal series of memory ops to replace the memset / memcpy.
4157 /// Return true if the number of memory ops is below the threshold (Limit).
4158 /// It returns the types of the sequence of memory ops to perform
4159 /// memset / memcpy by reference.
4160 static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
4161                                      unsigned Limit, uint64_t Size,
4162                                      unsigned DstAlign, unsigned SrcAlign,
4163                                      bool IsMemset,
4164                                      bool ZeroMemset,
4165                                      bool MemcpyStrSrc,
4166                                      bool AllowOverlap,
4167                                      unsigned DstAS, unsigned SrcAS,
4168                                      SelectionDAG &DAG,
4169                                      const TargetLowering &TLI) {
4170   assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
4171          "Expecting memcpy / memset source to meet alignment requirement!");
4172   // If 'SrcAlign' is zero, that means the memory operation does not need to
4173   // load the value, i.e. memset or memcpy from constant string. Otherwise,
4174   // it's the inferred alignment of the source. 'DstAlign', on the other hand,
4175   // is the specified alignment of the memory operation. If it is zero, that
4176   // means it's possible to change the alignment of the destination.
4177   // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
4178   // not need to be loaded.
4179   EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
4180                                    IsMemset, ZeroMemset, MemcpyStrSrc,
4181                                    DAG.getMachineFunction());
4182 
4183   if (VT == MVT::Other) {
4184     if (DstAlign >= DAG.getDataLayout().getPointerPrefAlignment(DstAS) ||
4185         TLI.allowsMisalignedMemoryAccesses(VT, DstAS, DstAlign)) {
4186       VT = TLI.getPointerTy(DAG.getDataLayout(), DstAS);
4187     } else {
4188       switch (DstAlign & 7) {
4189       case 0:  VT = MVT::i64; break;
4190       case 4:  VT = MVT::i32; break;
4191       case 2:  VT = MVT::i16; break;
4192       default: VT = MVT::i8;  break;
4193       }
4194     }
4195 
4196     MVT LVT = MVT::i64;
4197     while (!TLI.isTypeLegal(LVT))
4198       LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
4199     assert(LVT.isInteger());
4200 
4201     if (VT.bitsGT(LVT))
4202       VT = LVT;
4203   }
4204 
4205   unsigned NumMemOps = 0;
4206   while (Size != 0) {
4207     unsigned VTSize = VT.getSizeInBits() / 8;
4208     while (VTSize > Size) {
4209       // For now, only use non-vector load / store's for the left-over pieces.
4210       EVT NewVT = VT;
4211       unsigned NewVTSize;
4212 
4213       bool Found = false;
4214       if (VT.isVector() || VT.isFloatingPoint()) {
4215         NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
4216         if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
4217             TLI.isSafeMemOpType(NewVT.getSimpleVT()))
4218           Found = true;
4219         else if (NewVT == MVT::i64 &&
4220                  TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
4221                  TLI.isSafeMemOpType(MVT::f64)) {
4222           // i64 is usually not legal on 32-bit targets, but f64 may be.
4223           NewVT = MVT::f64;
4224           Found = true;
4225         }
4226       }
4227 
4228       if (!Found) {
4229         do {
4230           NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
4231           if (NewVT == MVT::i8)
4232             break;
4233         } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
4234       }
4235       NewVTSize = NewVT.getSizeInBits() / 8;
4236 
4237       // If the new VT cannot cover all of the remaining bits, then consider
4238       // issuing a (or a pair of) unaligned and overlapping load / store.
4239       // FIXME: Only does this for 64-bit or more since we don't have proper
4240       // cost model for unaligned load / store.
4241       bool Fast;
4242       if (NumMemOps && AllowOverlap &&
4243           VTSize >= 8 && NewVTSize < Size &&
4244           TLI.allowsMisalignedMemoryAccesses(VT, DstAS, DstAlign, &Fast) && Fast)
4245         VTSize = Size;
4246       else {
4247         VT = NewVT;
4248         VTSize = NewVTSize;
4249       }
4250     }
4251 
4252     if (++NumMemOps > Limit)
4253       return false;
4254 
4255     MemOps.push_back(VT);
4256     Size -= VTSize;
4257   }
4258 
4259   return true;
4260 }
4261 
4262 static bool shouldLowerMemFuncForSize(const MachineFunction &MF) {
4263   // On Darwin, -Os means optimize for size without hurting performance, so
4264   // only really optimize for size when -Oz (MinSize) is used.
4265   if (MF.getTarget().getTargetTriple().isOSDarwin())
4266     return MF.getFunction()->optForMinSize();
4267   return MF.getFunction()->optForSize();
4268 }
4269 
4270 static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
4271                                        SDValue Chain, SDValue Dst,
4272                                        SDValue Src, uint64_t Size,
4273                                        unsigned Align, bool isVol,
4274                                        bool AlwaysInline,
4275                                        MachinePointerInfo DstPtrInfo,
4276                                        MachinePointerInfo SrcPtrInfo) {
4277   // Turn a memcpy of undef to nop.
4278   if (Src.isUndef())
4279     return Chain;
4280 
4281   // Expand memcpy to a series of load and store ops if the size operand falls
4282   // below a certain threshold.
4283   // TODO: In the AlwaysInline case, if the size is big then generate a loop
4284   // rather than maybe a humongous number of loads and stores.
4285   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4286   std::vector<EVT> MemOps;
4287   bool DstAlignCanChange = false;
4288   MachineFunction &MF = DAG.getMachineFunction();
4289   MachineFrameInfo *MFI = MF.getFrameInfo();
4290   bool OptSize = shouldLowerMemFuncForSize(MF);
4291   FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4292   if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4293     DstAlignCanChange = true;
4294   unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4295   if (Align > SrcAlign)
4296     SrcAlign = Align;
4297   StringRef Str;
4298   bool CopyFromStr = isMemSrcFromString(Src, Str);
4299   bool isZeroStr = CopyFromStr && Str.empty();
4300   unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
4301 
4302   if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
4303                                 (DstAlignCanChange ? 0 : Align),
4304                                 (isZeroStr ? 0 : SrcAlign),
4305                                 false, false, CopyFromStr, true,
4306                                 DstPtrInfo.getAddrSpace(),
4307                                 SrcPtrInfo.getAddrSpace(),
4308                                 DAG, TLI))
4309     return SDValue();
4310 
4311   if (DstAlignCanChange) {
4312     Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
4313     unsigned NewAlign = (unsigned)DAG.getDataLayout().getABITypeAlignment(Ty);
4314 
4315     // Don't promote to an alignment that would require dynamic stack
4316     // realignment.
4317     const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
4318     if (!TRI->needsStackRealignment(MF))
4319       while (NewAlign > Align &&
4320              DAG.getDataLayout().exceedsNaturalStackAlignment(NewAlign))
4321           NewAlign /= 2;
4322 
4323     if (NewAlign > Align) {
4324       // Give the stack frame object a larger alignment if needed.
4325       if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4326         MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4327       Align = NewAlign;
4328     }
4329   }
4330 
4331   SmallVector<SDValue, 8> OutChains;
4332   unsigned NumMemOps = MemOps.size();
4333   uint64_t SrcOff = 0, DstOff = 0;
4334   for (unsigned i = 0; i != NumMemOps; ++i) {
4335     EVT VT = MemOps[i];
4336     unsigned VTSize = VT.getSizeInBits() / 8;
4337     SDValue Value, Store;
4338 
4339     if (VTSize > Size) {
4340       // Issuing an unaligned load / store pair  that overlaps with the previous
4341       // pair. Adjust the offset accordingly.
4342       assert(i == NumMemOps-1 && i != 0);
4343       SrcOff -= VTSize - Size;
4344       DstOff -= VTSize - Size;
4345     }
4346 
4347     if (CopyFromStr &&
4348         (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
4349       // It's unlikely a store of a vector immediate can be done in a single
4350       // instruction. It would require a load from a constantpool first.
4351       // We only handle zero vectors here.
4352       // FIXME: Handle other cases where store of vector immediate is done in
4353       // a single instruction.
4354       Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
4355       if (Value.getNode())
4356         Store = DAG.getStore(Chain, dl, Value,
4357                              DAG.getMemBasePlusOffset(Dst, DstOff, dl),
4358                              DstPtrInfo.getWithOffset(DstOff), isVol,
4359                              false, Align);
4360     }
4361 
4362     if (!Store.getNode()) {
4363       // The type might not be legal for the target.  This should only happen
4364       // if the type is smaller than a legal type, as on PPC, so the right
4365       // thing to do is generate a LoadExt/StoreTrunc pair.  These simplify
4366       // to Load/Store if NVT==VT.
4367       // FIXME does the case above also need this?
4368       EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
4369       assert(NVT.bitsGE(VT));
4370       Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
4371                              DAG.getMemBasePlusOffset(Src, SrcOff, dl),
4372                              SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
4373                              false, MinAlign(SrcAlign, SrcOff));
4374       OutChains.push_back(Value.getValue(1));
4375       Store = DAG.getTruncStore(Chain, dl, Value,
4376                                 DAG.getMemBasePlusOffset(Dst, DstOff, dl),
4377                                 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
4378                                 false, Align);
4379     }
4380     OutChains.push_back(Store);
4381     SrcOff += VTSize;
4382     DstOff += VTSize;
4383     Size -= VTSize;
4384   }
4385 
4386   return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
4387 }
4388 
4389 static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
4390                                         SDValue Chain, SDValue Dst,
4391                                         SDValue Src, uint64_t Size,
4392                                         unsigned Align,  bool isVol,
4393                                         bool AlwaysInline,
4394                                         MachinePointerInfo DstPtrInfo,
4395                                         MachinePointerInfo SrcPtrInfo) {
4396   // Turn a memmove of undef to nop.
4397   if (Src.isUndef())
4398     return Chain;
4399 
4400   // Expand memmove to a series of load and store ops if the size operand falls
4401   // below a certain threshold.
4402   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4403   std::vector<EVT> MemOps;
4404   bool DstAlignCanChange = false;
4405   MachineFunction &MF = DAG.getMachineFunction();
4406   MachineFrameInfo *MFI = MF.getFrameInfo();
4407   bool OptSize = shouldLowerMemFuncForSize(MF);
4408   FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4409   if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4410     DstAlignCanChange = true;
4411   unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4412   if (Align > SrcAlign)
4413     SrcAlign = Align;
4414   unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
4415 
4416   if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
4417                                 (DstAlignCanChange ? 0 : Align), SrcAlign,
4418                                 false, false, false, false,
4419                                 DstPtrInfo.getAddrSpace(),
4420                                 SrcPtrInfo.getAddrSpace(),
4421                                 DAG, TLI))
4422     return SDValue();
4423 
4424   if (DstAlignCanChange) {
4425     Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
4426     unsigned NewAlign = (unsigned)DAG.getDataLayout().getABITypeAlignment(Ty);
4427     if (NewAlign > Align) {
4428       // Give the stack frame object a larger alignment if needed.
4429       if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4430         MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4431       Align = NewAlign;
4432     }
4433   }
4434 
4435   uint64_t SrcOff = 0, DstOff = 0;
4436   SmallVector<SDValue, 8> LoadValues;
4437   SmallVector<SDValue, 8> LoadChains;
4438   SmallVector<SDValue, 8> OutChains;
4439   unsigned NumMemOps = MemOps.size();
4440   for (unsigned i = 0; i < NumMemOps; i++) {
4441     EVT VT = MemOps[i];
4442     unsigned VTSize = VT.getSizeInBits() / 8;
4443     SDValue Value;
4444 
4445     Value = DAG.getLoad(VT, dl, Chain,
4446                         DAG.getMemBasePlusOffset(Src, SrcOff, dl),
4447                         SrcPtrInfo.getWithOffset(SrcOff), isVol,
4448                         false, false, SrcAlign);
4449     LoadValues.push_back(Value);
4450     LoadChains.push_back(Value.getValue(1));
4451     SrcOff += VTSize;
4452   }
4453   Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
4454   OutChains.clear();
4455   for (unsigned i = 0; i < NumMemOps; i++) {
4456     EVT VT = MemOps[i];
4457     unsigned VTSize = VT.getSizeInBits() / 8;
4458     SDValue Store;
4459 
4460     Store = DAG.getStore(Chain, dl, LoadValues[i],
4461                          DAG.getMemBasePlusOffset(Dst, DstOff, dl),
4462                          DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
4463     OutChains.push_back(Store);
4464     DstOff += VTSize;
4465   }
4466 
4467   return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
4468 }
4469 
4470 /// \brief Lower the call to 'memset' intrinsic function into a series of store
4471 /// operations.
4472 ///
4473 /// \param DAG Selection DAG where lowered code is placed.
4474 /// \param dl Link to corresponding IR location.
4475 /// \param Chain Control flow dependency.
4476 /// \param Dst Pointer to destination memory location.
4477 /// \param Src Value of byte to write into the memory.
4478 /// \param Size Number of bytes to write.
4479 /// \param Align Alignment of the destination in bytes.
4480 /// \param isVol True if destination is volatile.
4481 /// \param DstPtrInfo IR information on the memory pointer.
4482 /// \returns New head in the control flow, if lowering was successful, empty
4483 /// SDValue otherwise.
4484 ///
4485 /// The function tries to replace 'llvm.memset' intrinsic with several store
4486 /// operations and value calculation code. This is usually profitable for small
4487 /// memory size.
4488 static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
4489                                SDValue Chain, SDValue Dst,
4490                                SDValue Src, uint64_t Size,
4491                                unsigned Align, bool isVol,
4492                                MachinePointerInfo DstPtrInfo) {
4493   // Turn a memset of undef to nop.
4494   if (Src.isUndef())
4495     return Chain;
4496 
4497   // Expand memset to a series of load/store ops if the size operand
4498   // falls below a certain threshold.
4499   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4500   std::vector<EVT> MemOps;
4501   bool DstAlignCanChange = false;
4502   MachineFunction &MF = DAG.getMachineFunction();
4503   MachineFrameInfo *MFI = MF.getFrameInfo();
4504   bool OptSize = shouldLowerMemFuncForSize(MF);
4505   FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4506   if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4507     DstAlignCanChange = true;
4508   bool IsZeroVal =
4509     isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
4510   if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
4511                                 Size, (DstAlignCanChange ? 0 : Align), 0,
4512                                 true, IsZeroVal, false, true,
4513                                 DstPtrInfo.getAddrSpace(), ~0u,
4514                                 DAG, TLI))
4515     return SDValue();
4516 
4517   if (DstAlignCanChange) {
4518     Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
4519     unsigned NewAlign = (unsigned)DAG.getDataLayout().getABITypeAlignment(Ty);
4520     if (NewAlign > Align) {
4521       // Give the stack frame object a larger alignment if needed.
4522       if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4523         MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4524       Align = NewAlign;
4525     }
4526   }
4527 
4528   SmallVector<SDValue, 8> OutChains;
4529   uint64_t DstOff = 0;
4530   unsigned NumMemOps = MemOps.size();
4531 
4532   // Find the largest store and generate the bit pattern for it.
4533   EVT LargestVT = MemOps[0];
4534   for (unsigned i = 1; i < NumMemOps; i++)
4535     if (MemOps[i].bitsGT(LargestVT))
4536       LargestVT = MemOps[i];
4537   SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4538 
4539   for (unsigned i = 0; i < NumMemOps; i++) {
4540     EVT VT = MemOps[i];
4541     unsigned VTSize = VT.getSizeInBits() / 8;
4542     if (VTSize > Size) {
4543       // Issuing an unaligned load / store pair  that overlaps with the previous
4544       // pair. Adjust the offset accordingly.
4545       assert(i == NumMemOps-1 && i != 0);
4546       DstOff -= VTSize - Size;
4547     }
4548 
4549     // If this store is smaller than the largest store see whether we can get
4550     // the smaller value for free with a truncate.
4551     SDValue Value = MemSetValue;
4552     if (VT.bitsLT(LargestVT)) {
4553       if (!LargestVT.isVector() && !VT.isVector() &&
4554           TLI.isTruncateFree(LargestVT, VT))
4555         Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4556       else
4557         Value = getMemsetValue(Src, VT, DAG, dl);
4558     }
4559     assert(Value.getValueType() == VT && "Value with wrong type.");
4560     SDValue Store = DAG.getStore(Chain, dl, Value,
4561                                  DAG.getMemBasePlusOffset(Dst, DstOff, dl),
4562                                  DstPtrInfo.getWithOffset(DstOff),
4563                                  isVol, false, Align);
4564     OutChains.push_back(Store);
4565     DstOff += VT.getSizeInBits() / 8;
4566     Size -= VTSize;
4567   }
4568 
4569   return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
4570 }
4571 
4572 static void checkAddrSpaceIsValidForLibcall(const TargetLowering *TLI,
4573                                             unsigned AS) {
4574   // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
4575   // pointer operands can be losslessly bitcasted to pointers of address space 0
4576   if (AS != 0 && !TLI->isNoopAddrSpaceCast(AS, 0)) {
4577     report_fatal_error("cannot lower memory intrinsic in address space " +
4578                        Twine(AS));
4579   }
4580 }
4581 
4582 SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
4583                                 SDValue Src, SDValue Size,
4584                                 unsigned Align, bool isVol, bool AlwaysInline,
4585                                 bool isTailCall, MachinePointerInfo DstPtrInfo,
4586                                 MachinePointerInfo SrcPtrInfo) {
4587   assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
4588 
4589   // Check to see if we should lower the memcpy to loads and stores first.
4590   // For cases within the target-specified limits, this is the best choice.
4591   ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4592   if (ConstantSize) {
4593     // Memcpy with size zero? Just return the original chain.
4594     if (ConstantSize->isNullValue())
4595       return Chain;
4596 
4597     SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4598                                              ConstantSize->getZExtValue(),Align,
4599                                 isVol, false, DstPtrInfo, SrcPtrInfo);
4600     if (Result.getNode())
4601       return Result;
4602   }
4603 
4604   // Then check to see if we should lower the memcpy with target-specific
4605   // code. If the target chooses to do this, this is the next best.
4606   if (TSI) {
4607     SDValue Result = TSI->EmitTargetCodeForMemcpy(
4608         *this, dl, Chain, Dst, Src, Size, Align, isVol, AlwaysInline,
4609         DstPtrInfo, SrcPtrInfo);
4610     if (Result.getNode())
4611       return Result;
4612   }
4613 
4614   // If we really need inline code and the target declined to provide it,
4615   // use a (potentially long) sequence of loads and stores.
4616   if (AlwaysInline) {
4617     assert(ConstantSize && "AlwaysInline requires a constant size!");
4618     return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4619                                    ConstantSize->getZExtValue(), Align, isVol,
4620                                    true, DstPtrInfo, SrcPtrInfo);
4621   }
4622 
4623   checkAddrSpaceIsValidForLibcall(TLI, DstPtrInfo.getAddrSpace());
4624   checkAddrSpaceIsValidForLibcall(TLI, SrcPtrInfo.getAddrSpace());
4625 
4626   // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4627   // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4628   // respect volatile, so they may do things like read or write memory
4629   // beyond the given memory regions. But fixing this isn't easy, and most
4630   // people don't care.
4631 
4632   // Emit a library call.
4633   TargetLowering::ArgListTy Args;
4634   TargetLowering::ArgListEntry Entry;
4635   Entry.Ty = getDataLayout().getIntPtrType(*getContext());
4636   Entry.Node = Dst; Args.push_back(Entry);
4637   Entry.Node = Src; Args.push_back(Entry);
4638   Entry.Node = Size; Args.push_back(Entry);
4639   // FIXME: pass in SDLoc
4640   TargetLowering::CallLoweringInfo CLI(*this);
4641   CLI.setDebugLoc(dl)
4642       .setChain(Chain)
4643       .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
4644                  Dst.getValueType().getTypeForEVT(*getContext()),
4645                  getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
4646                                    TLI->getPointerTy(getDataLayout())),
4647                  std::move(Args), 0)
4648       .setDiscardResult()
4649       .setTailCall(isTailCall);
4650 
4651   std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
4652   return CallResult.second;
4653 }
4654 
4655 SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
4656                                  SDValue Src, SDValue Size,
4657                                  unsigned Align, bool isVol, bool isTailCall,
4658                                  MachinePointerInfo DstPtrInfo,
4659                                  MachinePointerInfo SrcPtrInfo) {
4660   assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
4661 
4662   // Check to see if we should lower the memmove to loads and stores first.
4663   // For cases within the target-specified limits, this is the best choice.
4664   ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4665   if (ConstantSize) {
4666     // Memmove with size zero? Just return the original chain.
4667     if (ConstantSize->isNullValue())
4668       return Chain;
4669 
4670     SDValue Result =
4671       getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
4672                                ConstantSize->getZExtValue(), Align, isVol,
4673                                false, DstPtrInfo, SrcPtrInfo);
4674     if (Result.getNode())
4675       return Result;
4676   }
4677 
4678   // Then check to see if we should lower the memmove with target-specific
4679   // code. If the target chooses to do this, this is the next best.
4680   if (TSI) {
4681     SDValue Result = TSI->EmitTargetCodeForMemmove(
4682         *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo, SrcPtrInfo);
4683     if (Result.getNode())
4684       return Result;
4685   }
4686 
4687   checkAddrSpaceIsValidForLibcall(TLI, DstPtrInfo.getAddrSpace());
4688   checkAddrSpaceIsValidForLibcall(TLI, SrcPtrInfo.getAddrSpace());
4689 
4690   // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4691   // not be safe.  See memcpy above for more details.
4692 
4693   // Emit a library call.
4694   TargetLowering::ArgListTy Args;
4695   TargetLowering::ArgListEntry Entry;
4696   Entry.Ty = getDataLayout().getIntPtrType(*getContext());
4697   Entry.Node = Dst; Args.push_back(Entry);
4698   Entry.Node = Src; Args.push_back(Entry);
4699   Entry.Node = Size; Args.push_back(Entry);
4700   // FIXME:  pass in SDLoc
4701   TargetLowering::CallLoweringInfo CLI(*this);
4702   CLI.setDebugLoc(dl)
4703       .setChain(Chain)
4704       .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
4705                  Dst.getValueType().getTypeForEVT(*getContext()),
4706                  getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
4707                                    TLI->getPointerTy(getDataLayout())),
4708                  std::move(Args), 0)
4709       .setDiscardResult()
4710       .setTailCall(isTailCall);
4711 
4712   std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
4713   return CallResult.second;
4714 }
4715 
4716 SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
4717                                 SDValue Src, SDValue Size,
4718                                 unsigned Align, bool isVol, bool isTailCall,
4719                                 MachinePointerInfo DstPtrInfo) {
4720   assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
4721 
4722   // Check to see if we should lower the memset to stores first.
4723   // For cases within the target-specified limits, this is the best choice.
4724   ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4725   if (ConstantSize) {
4726     // Memset with size zero? Just return the original chain.
4727     if (ConstantSize->isNullValue())
4728       return Chain;
4729 
4730     SDValue Result =
4731       getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
4732                       Align, isVol, DstPtrInfo);
4733 
4734     if (Result.getNode())
4735       return Result;
4736   }
4737 
4738   // Then check to see if we should lower the memset with target-specific
4739   // code. If the target chooses to do this, this is the next best.
4740   if (TSI) {
4741     SDValue Result = TSI->EmitTargetCodeForMemset(
4742         *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo);
4743     if (Result.getNode())
4744       return Result;
4745   }
4746 
4747   checkAddrSpaceIsValidForLibcall(TLI, DstPtrInfo.getAddrSpace());
4748 
4749   // Emit a library call.
4750   Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
4751   TargetLowering::ArgListTy Args;
4752   TargetLowering::ArgListEntry Entry;
4753   Entry.Node = Dst; Entry.Ty = IntPtrTy;
4754   Args.push_back(Entry);
4755   Entry.Node = Src;
4756   Entry.Ty = Src.getValueType().getTypeForEVT(*getContext());
4757   Args.push_back(Entry);
4758   Entry.Node = Size;
4759   Entry.Ty = IntPtrTy;
4760   Args.push_back(Entry);
4761 
4762   // FIXME: pass in SDLoc
4763   TargetLowering::CallLoweringInfo CLI(*this);
4764   CLI.setDebugLoc(dl)
4765       .setChain(Chain)
4766       .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
4767                  Dst.getValueType().getTypeForEVT(*getContext()),
4768                  getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
4769                                    TLI->getPointerTy(getDataLayout())),
4770                  std::move(Args), 0)
4771       .setDiscardResult()
4772       .setTailCall(isTailCall);
4773 
4774   std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
4775   return CallResult.second;
4776 }
4777 
4778 SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
4779                                 SDVTList VTList, ArrayRef<SDValue> Ops,
4780                                 MachineMemOperand *MMO,
4781                                 AtomicOrdering SuccessOrdering,
4782                                 AtomicOrdering FailureOrdering,
4783                                 SynchronizationScope SynchScope) {
4784   FoldingSetNodeID ID;
4785   ID.AddInteger(MemVT.getRawBits());
4786   AddNodeIDNode(ID, Opcode, VTList, Ops);
4787   ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
4788   void* IP = nullptr;
4789   if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
4790     cast<AtomicSDNode>(E)->refineAlignment(MMO);
4791     return SDValue(E, 0);
4792   }
4793 
4794   auto *N = newSDNode<AtomicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
4795                                     VTList, MemVT, MMO, SuccessOrdering,
4796                                     FailureOrdering, SynchScope);
4797   createOperands(N, Ops);
4798 
4799   CSEMap.InsertNode(N, IP);
4800   InsertNode(N);
4801   return SDValue(N, 0);
4802 }
4803 
4804 SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
4805                                 SDVTList VTList, ArrayRef<SDValue> Ops,
4806                                 MachineMemOperand *MMO,
4807                                 AtomicOrdering Ordering,
4808                                 SynchronizationScope SynchScope) {
4809   return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
4810                    Ordering, SynchScope);
4811 }
4812 
4813 SDValue SelectionDAG::getAtomicCmpSwap(
4814     unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTs, SDValue Chain,
4815     SDValue Ptr, SDValue Cmp, SDValue Swp, MachinePointerInfo PtrInfo,
4816     unsigned Alignment, AtomicOrdering SuccessOrdering,
4817     AtomicOrdering FailureOrdering, SynchronizationScope SynchScope) {
4818   assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4819          Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
4820   assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4821 
4822   if (Alignment == 0)  // Ensure that codegen never sees alignment 0
4823     Alignment = getEVTAlignment(MemVT);
4824 
4825   MachineFunction &MF = getMachineFunction();
4826 
4827   // FIXME: Volatile isn't really correct; we should keep track of atomic
4828   // orderings in the memoperand.
4829   unsigned Flags = MachineMemOperand::MOVolatile;
4830   Flags |= MachineMemOperand::MOLoad;
4831   Flags |= MachineMemOperand::MOStore;
4832 
4833   MachineMemOperand *MMO =
4834     MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
4835 
4836   return getAtomicCmpSwap(Opcode, dl, MemVT, VTs, Chain, Ptr, Cmp, Swp, MMO,
4837                           SuccessOrdering, FailureOrdering, SynchScope);
4838 }
4839 
4840 SDValue SelectionDAG::getAtomicCmpSwap(unsigned Opcode, SDLoc dl, EVT MemVT,
4841                                        SDVTList VTs, SDValue Chain, SDValue Ptr,
4842                                        SDValue Cmp, SDValue Swp,
4843                                        MachineMemOperand *MMO,
4844                                        AtomicOrdering SuccessOrdering,
4845                                        AtomicOrdering FailureOrdering,
4846                                        SynchronizationScope SynchScope) {
4847   assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4848          Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
4849   assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4850 
4851   SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
4852   return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO,
4853                    SuccessOrdering, FailureOrdering, SynchScope);
4854 }
4855 
4856 SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
4857                                 SDValue Chain,
4858                                 SDValue Ptr, SDValue Val,
4859                                 const Value* PtrVal,
4860                                 unsigned Alignment,
4861                                 AtomicOrdering Ordering,
4862                                 SynchronizationScope SynchScope) {
4863   if (Alignment == 0)  // Ensure that codegen never sees alignment 0
4864     Alignment = getEVTAlignment(MemVT);
4865 
4866   MachineFunction &MF = getMachineFunction();
4867   // An atomic store does not load. An atomic load does not store.
4868   // (An atomicrmw obviously both loads and stores.)
4869   // For now, atomics are considered to be volatile always, and they are
4870   // chained as such.
4871   // FIXME: Volatile isn't really correct; we should keep track of atomic
4872   // orderings in the memoperand.
4873   unsigned Flags = MachineMemOperand::MOVolatile;
4874   if (Opcode != ISD::ATOMIC_STORE)
4875     Flags |= MachineMemOperand::MOLoad;
4876   if (Opcode != ISD::ATOMIC_LOAD)
4877     Flags |= MachineMemOperand::MOStore;
4878 
4879   MachineMemOperand *MMO =
4880     MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
4881                             MemVT.getStoreSize(), Alignment);
4882 
4883   return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4884                    Ordering, SynchScope);
4885 }
4886 
4887 SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
4888                                 SDValue Chain,
4889                                 SDValue Ptr, SDValue Val,
4890                                 MachineMemOperand *MMO,
4891                                 AtomicOrdering Ordering,
4892                                 SynchronizationScope SynchScope) {
4893   assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4894           Opcode == ISD::ATOMIC_LOAD_SUB ||
4895           Opcode == ISD::ATOMIC_LOAD_AND ||
4896           Opcode == ISD::ATOMIC_LOAD_OR ||
4897           Opcode == ISD::ATOMIC_LOAD_XOR ||
4898           Opcode == ISD::ATOMIC_LOAD_NAND ||
4899           Opcode == ISD::ATOMIC_LOAD_MIN ||
4900           Opcode == ISD::ATOMIC_LOAD_MAX ||
4901           Opcode == ISD::ATOMIC_LOAD_UMIN ||
4902           Opcode == ISD::ATOMIC_LOAD_UMAX ||
4903           Opcode == ISD::ATOMIC_SWAP ||
4904           Opcode == ISD::ATOMIC_STORE) &&
4905          "Invalid Atomic Op");
4906 
4907   EVT VT = Val.getValueType();
4908 
4909   SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4910                                                getVTList(VT, MVT::Other);
4911   SDValue Ops[] = {Chain, Ptr, Val};
4912   return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
4913 }
4914 
4915 SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
4916                                 EVT VT, SDValue Chain,
4917                                 SDValue Ptr,
4918                                 MachineMemOperand *MMO,
4919                                 AtomicOrdering Ordering,
4920                                 SynchronizationScope SynchScope) {
4921   assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4922 
4923   SDVTList VTs = getVTList(VT, MVT::Other);
4924   SDValue Ops[] = {Chain, Ptr};
4925   return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
4926 }
4927 
4928 /// getMergeValues - Create a MERGE_VALUES node from the given operands.
4929 SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) {
4930   if (Ops.size() == 1)
4931     return Ops[0];
4932 
4933   SmallVector<EVT, 4> VTs;
4934   VTs.reserve(Ops.size());
4935   for (unsigned i = 0; i < Ops.size(); ++i)
4936     VTs.push_back(Ops[i].getValueType());
4937   return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
4938 }
4939 
4940 SDValue
4941 SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
4942                                   ArrayRef<SDValue> Ops,
4943                                   EVT MemVT, MachinePointerInfo PtrInfo,
4944                                   unsigned Align, bool Vol,
4945                                   bool ReadMem, bool WriteMem, unsigned Size) {
4946   if (Align == 0)  // Ensure that codegen never sees alignment 0
4947     Align = getEVTAlignment(MemVT);
4948 
4949   MachineFunction &MF = getMachineFunction();
4950   unsigned Flags = 0;
4951   if (WriteMem)
4952     Flags |= MachineMemOperand::MOStore;
4953   if (ReadMem)
4954     Flags |= MachineMemOperand::MOLoad;
4955   if (Vol)
4956     Flags |= MachineMemOperand::MOVolatile;
4957   if (!Size)
4958     Size = MemVT.getStoreSize();
4959   MachineMemOperand *MMO =
4960     MF.getMachineMemOperand(PtrInfo, Flags, Size, Align);
4961 
4962   return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
4963 }
4964 
4965 SDValue
4966 SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
4967                                   ArrayRef<SDValue> Ops, EVT MemVT,
4968                                   MachineMemOperand *MMO) {
4969   assert((Opcode == ISD::INTRINSIC_VOID ||
4970           Opcode == ISD::INTRINSIC_W_CHAIN ||
4971           Opcode == ISD::PREFETCH ||
4972           Opcode == ISD::LIFETIME_START ||
4973           Opcode == ISD::LIFETIME_END ||
4974           (Opcode <= INT_MAX &&
4975            (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4976          "Opcode is not a memory-accessing opcode!");
4977 
4978   // Memoize the node unless it returns a flag.
4979   MemIntrinsicSDNode *N;
4980   if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
4981     FoldingSetNodeID ID;
4982     AddNodeIDNode(ID, Opcode, VTList, Ops);
4983     ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
4984     void *IP = nullptr;
4985     if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
4986       cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
4987       return SDValue(E, 0);
4988     }
4989 
4990     N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
4991                                       VTList, MemVT, MMO);
4992     createOperands(N, Ops);
4993 
4994   CSEMap.InsertNode(N, IP);
4995   } else {
4996     N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
4997                                       VTList, MemVT, MMO);
4998     createOperands(N, Ops);
4999   }
5000   InsertNode(N);
5001   return SDValue(N, 0);
5002 }
5003 
5004 /// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
5005 /// MachinePointerInfo record from it.  This is particularly useful because the
5006 /// code generator has many cases where it doesn't bother passing in a
5007 /// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
5008 static MachinePointerInfo InferPointerInfo(SelectionDAG &DAG, SDValue Ptr,
5009                                            int64_t Offset = 0) {
5010   // If this is FI+Offset, we can model it.
5011   if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
5012     return MachinePointerInfo::getFixedStack(DAG.getMachineFunction(),
5013                                              FI->getIndex(), Offset);
5014 
5015   // If this is (FI+Offset1)+Offset2, we can model it.
5016   if (Ptr.getOpcode() != ISD::ADD ||
5017       !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
5018       !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
5019     return MachinePointerInfo();
5020 
5021   int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
5022   return MachinePointerInfo::getFixedStack(
5023       DAG.getMachineFunction(), FI,
5024       Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
5025 }
5026 
5027 /// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
5028 /// MachinePointerInfo record from it.  This is particularly useful because the
5029 /// code generator has many cases where it doesn't bother passing in a
5030 /// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
5031 static MachinePointerInfo InferPointerInfo(SelectionDAG &DAG, SDValue Ptr,
5032                                            SDValue OffsetOp) {
5033   // If the 'Offset' value isn't a constant, we can't handle this.
5034   if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
5035     return InferPointerInfo(DAG, Ptr, OffsetNode->getSExtValue());
5036   if (OffsetOp.isUndef())
5037     return InferPointerInfo(DAG, Ptr);
5038   return MachinePointerInfo();
5039 }
5040 
5041 
5042 SDValue
5043 SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
5044                       EVT VT, SDLoc dl, SDValue Chain,
5045                       SDValue Ptr, SDValue Offset,
5046                       MachinePointerInfo PtrInfo, EVT MemVT,
5047                       bool isVolatile, bool isNonTemporal, bool isInvariant,
5048                       unsigned Alignment, const AAMDNodes &AAInfo,
5049                       const MDNode *Ranges) {
5050   assert(Chain.getValueType() == MVT::Other &&
5051         "Invalid chain type");
5052   if (Alignment == 0)  // Ensure that codegen never sees alignment 0
5053     Alignment = getEVTAlignment(VT);
5054 
5055   unsigned Flags = MachineMemOperand::MOLoad;
5056   if (isVolatile)
5057     Flags |= MachineMemOperand::MOVolatile;
5058   if (isNonTemporal)
5059     Flags |= MachineMemOperand::MONonTemporal;
5060   if (isInvariant)
5061     Flags |= MachineMemOperand::MOInvariant;
5062 
5063   // If we don't have a PtrInfo, infer the trivial frame index case to simplify
5064   // clients.
5065   if (PtrInfo.V.isNull())
5066     PtrInfo = InferPointerInfo(*this, Ptr, Offset);
5067 
5068   MachineFunction &MF = getMachineFunction();
5069   MachineMemOperand *MMO =
5070     MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
5071                             AAInfo, Ranges);
5072   return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
5073 }
5074 
5075 SDValue
5076 SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
5077                       EVT VT, SDLoc dl, SDValue Chain,
5078                       SDValue Ptr, SDValue Offset, EVT MemVT,
5079                       MachineMemOperand *MMO) {
5080   if (VT == MemVT) {
5081     ExtType = ISD::NON_EXTLOAD;
5082   } else if (ExtType == ISD::NON_EXTLOAD) {
5083     assert(VT == MemVT && "Non-extending load from different memory type!");
5084   } else {
5085     // Extending load.
5086     assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
5087            "Should only be an extending load, not truncating!");
5088     assert(VT.isInteger() == MemVT.isInteger() &&
5089            "Cannot convert from FP to Int or Int -> FP!");
5090     assert(VT.isVector() == MemVT.isVector() &&
5091            "Cannot use an ext load to convert to or from a vector!");
5092     assert((!VT.isVector() ||
5093             VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
5094            "Cannot use an ext load to change the number of vector elements!");
5095   }
5096 
5097   bool Indexed = AM != ISD::UNINDEXED;
5098   assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
5099 
5100   SDVTList VTs = Indexed ?
5101     getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
5102   SDValue Ops[] = { Chain, Ptr, Offset };
5103   FoldingSetNodeID ID;
5104   AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
5105   ID.AddInteger(MemVT.getRawBits());
5106   ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
5107                                      MMO->isNonTemporal(),
5108                                      MMO->isInvariant()));
5109   ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5110   void *IP = nullptr;
5111   if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
5112     cast<LoadSDNode>(E)->refineAlignment(MMO);
5113     return SDValue(E, 0);
5114   }
5115   auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
5116                                   ExtType, MemVT, MMO);
5117   createOperands(N, Ops);
5118 
5119   CSEMap.InsertNode(N, IP);
5120   InsertNode(N);
5121   return SDValue(N, 0);
5122 }
5123 
5124 SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
5125                               SDValue Chain, SDValue Ptr,
5126                               MachinePointerInfo PtrInfo,
5127                               bool isVolatile, bool isNonTemporal,
5128                               bool isInvariant, unsigned Alignment,
5129                               const AAMDNodes &AAInfo,
5130                               const MDNode *Ranges) {
5131   SDValue Undef = getUNDEF(Ptr.getValueType());
5132   return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
5133                  PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
5134                  AAInfo, Ranges);
5135 }
5136 
5137 SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
5138                               SDValue Chain, SDValue Ptr,
5139                               MachineMemOperand *MMO) {
5140   SDValue Undef = getUNDEF(Ptr.getValueType());
5141   return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
5142                  VT, MMO);
5143 }
5144 
5145 SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
5146                                  SDValue Chain, SDValue Ptr,
5147                                  MachinePointerInfo PtrInfo, EVT MemVT,
5148                                  bool isVolatile, bool isNonTemporal,
5149                                  bool isInvariant, unsigned Alignment,
5150                                  const AAMDNodes &AAInfo) {
5151   SDValue Undef = getUNDEF(Ptr.getValueType());
5152   return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
5153                  PtrInfo, MemVT, isVolatile, isNonTemporal, isInvariant,
5154                  Alignment, AAInfo);
5155 }
5156 
5157 
5158 SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
5159                                  SDValue Chain, SDValue Ptr, EVT MemVT,
5160                                  MachineMemOperand *MMO) {
5161   SDValue Undef = getUNDEF(Ptr.getValueType());
5162   return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
5163                  MemVT, MMO);
5164 }
5165 
5166 SDValue
5167 SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
5168                              SDValue Offset, ISD::MemIndexedMode AM) {
5169   LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
5170   assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
5171   return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
5172                  LD->getChain(), Base, Offset, LD->getPointerInfo(),
5173                  LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
5174                  false, LD->getAlignment());
5175 }
5176 
5177 SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
5178                                SDValue Ptr, MachinePointerInfo PtrInfo,
5179                                bool isVolatile, bool isNonTemporal,
5180                                unsigned Alignment, const AAMDNodes &AAInfo) {
5181   assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
5182   if (Alignment == 0)  // Ensure that codegen never sees alignment 0
5183     Alignment = getEVTAlignment(Val.getValueType());
5184 
5185   unsigned Flags = MachineMemOperand::MOStore;
5186   if (isVolatile)
5187     Flags |= MachineMemOperand::MOVolatile;
5188   if (isNonTemporal)
5189     Flags |= MachineMemOperand::MONonTemporal;
5190 
5191   if (PtrInfo.V.isNull())
5192     PtrInfo = InferPointerInfo(*this, Ptr);
5193 
5194   MachineFunction &MF = getMachineFunction();
5195   MachineMemOperand *MMO =
5196     MF.getMachineMemOperand(PtrInfo, Flags,
5197                             Val.getValueType().getStoreSize(), Alignment,
5198                             AAInfo);
5199 
5200   return getStore(Chain, dl, Val, Ptr, MMO);
5201 }
5202 
5203 SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
5204                                SDValue Ptr, MachineMemOperand *MMO) {
5205   assert(Chain.getValueType() == MVT::Other &&
5206         "Invalid chain type");
5207   EVT VT = Val.getValueType();
5208   SDVTList VTs = getVTList(MVT::Other);
5209   SDValue Undef = getUNDEF(Ptr.getValueType());
5210   SDValue Ops[] = { Chain, Val, Ptr, Undef };
5211   FoldingSetNodeID ID;
5212   AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
5213   ID.AddInteger(VT.getRawBits());
5214   ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5215                                      MMO->isNonTemporal(), MMO->isInvariant()));
5216   ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5217   void *IP = nullptr;
5218   if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
5219     cast<StoreSDNode>(E)->refineAlignment(MMO);
5220     return SDValue(E, 0);
5221   }
5222   auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
5223                                    ISD::UNINDEXED, false, VT, MMO);
5224   createOperands(N, Ops);
5225 
5226   CSEMap.InsertNode(N, IP);
5227   InsertNode(N);
5228   return SDValue(N, 0);
5229 }
5230 
5231 SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
5232                                     SDValue Ptr, MachinePointerInfo PtrInfo,
5233                                     EVT SVT,bool isVolatile, bool isNonTemporal,
5234                                     unsigned Alignment,
5235                                     const AAMDNodes &AAInfo) {
5236   assert(Chain.getValueType() == MVT::Other &&
5237         "Invalid chain type");
5238   if (Alignment == 0)  // Ensure that codegen never sees alignment 0
5239     Alignment = getEVTAlignment(SVT);
5240 
5241   unsigned Flags = MachineMemOperand::MOStore;
5242   if (isVolatile)
5243     Flags |= MachineMemOperand::MOVolatile;
5244   if (isNonTemporal)
5245     Flags |= MachineMemOperand::MONonTemporal;
5246 
5247   if (PtrInfo.V.isNull())
5248     PtrInfo = InferPointerInfo(*this, Ptr);
5249 
5250   MachineFunction &MF = getMachineFunction();
5251   MachineMemOperand *MMO =
5252     MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
5253                             AAInfo);
5254 
5255   return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
5256 }
5257 
5258 SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
5259                                     SDValue Ptr, EVT SVT,
5260                                     MachineMemOperand *MMO) {
5261   EVT VT = Val.getValueType();
5262 
5263   assert(Chain.getValueType() == MVT::Other &&
5264         "Invalid chain type");
5265   if (VT == SVT)
5266     return getStore(Chain, dl, Val, Ptr, MMO);
5267 
5268   assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
5269          "Should only be a truncating store, not extending!");
5270   assert(VT.isInteger() == SVT.isInteger() &&
5271          "Can't do FP-INT conversion!");
5272   assert(VT.isVector() == SVT.isVector() &&
5273          "Cannot use trunc store to convert to or from a vector!");
5274   assert((!VT.isVector() ||
5275           VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
5276          "Cannot use trunc store to change the number of vector elements!");
5277 
5278   SDVTList VTs = getVTList(MVT::Other);
5279   SDValue Undef = getUNDEF(Ptr.getValueType());
5280   SDValue Ops[] = { Chain, Val, Ptr, Undef };
5281   FoldingSetNodeID ID;
5282   AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
5283   ID.AddInteger(SVT.getRawBits());
5284   ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
5285                                      MMO->isNonTemporal(), MMO->isInvariant()));
5286   ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5287   void *IP = nullptr;
5288   if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
5289     cast<StoreSDNode>(E)->refineAlignment(MMO);
5290     return SDValue(E, 0);
5291   }
5292   auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
5293                                    ISD::UNINDEXED, true, SVT, MMO);
5294   createOperands(N, Ops);
5295 
5296   CSEMap.InsertNode(N, IP);
5297   InsertNode(N);
5298   return SDValue(N, 0);
5299 }
5300 
5301 SDValue
5302 SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
5303                               SDValue Offset, ISD::MemIndexedMode AM) {
5304   StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
5305   assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
5306   SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
5307   SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
5308   FoldingSetNodeID ID;
5309   AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
5310   ID.AddInteger(ST->getMemoryVT().getRawBits());
5311   ID.AddInteger(ST->getRawSubclassData());
5312   ID.AddInteger(ST->getPointerInfo().getAddrSpace());
5313   void *IP = nullptr;
5314   if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP))
5315     return SDValue(E, 0);
5316 
5317   auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
5318                                    ST->isTruncatingStore(), ST->getMemoryVT(),
5319                                    ST->getMemOperand());
5320   createOperands(N, Ops);
5321 
5322   CSEMap.InsertNode(N, IP);
5323   InsertNode(N);
5324   return SDValue(N, 0);
5325 }
5326 
5327 SDValue
5328 SelectionDAG::getMaskedLoad(EVT VT, SDLoc dl, SDValue Chain,
5329                             SDValue Ptr, SDValue Mask, SDValue Src0, EVT MemVT,
5330                             MachineMemOperand *MMO, ISD::LoadExtType ExtTy) {
5331 
5332   SDVTList VTs = getVTList(VT, MVT::Other);
5333   SDValue Ops[] = { Chain, Ptr, Mask, Src0 };
5334   FoldingSetNodeID ID;
5335   AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
5336   ID.AddInteger(VT.getRawBits());
5337   ID.AddInteger(encodeMemSDNodeFlags(ExtTy, ISD::UNINDEXED,
5338                                      MMO->isVolatile(),
5339                                      MMO->isNonTemporal(),
5340                                      MMO->isInvariant()));
5341   ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5342   void *IP = nullptr;
5343   if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
5344     cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
5345     return SDValue(E, 0);
5346   }
5347   auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
5348                                         ExtTy, MemVT, MMO);
5349   createOperands(N, Ops);
5350 
5351   CSEMap.InsertNode(N, IP);
5352   InsertNode(N);
5353   return SDValue(N, 0);
5354 }
5355 
5356 SDValue SelectionDAG::getMaskedStore(SDValue Chain, SDLoc dl, SDValue Val,
5357                                      SDValue Ptr, SDValue Mask, EVT MemVT,
5358                                      MachineMemOperand *MMO, bool isTrunc) {
5359   assert(Chain.getValueType() == MVT::Other &&
5360         "Invalid chain type");
5361   EVT VT = Val.getValueType();
5362   SDVTList VTs = getVTList(MVT::Other);
5363   SDValue Ops[] = { Chain, Ptr, Mask, Val };
5364   FoldingSetNodeID ID;
5365   AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
5366   ID.AddInteger(VT.getRawBits());
5367   ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5368                                      MMO->isNonTemporal(), MMO->isInvariant()));
5369   ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5370   void *IP = nullptr;
5371   if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
5372     cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
5373     return SDValue(E, 0);
5374   }
5375   auto *N = newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
5376                                          isTrunc, MemVT, MMO);
5377   createOperands(N, Ops);
5378 
5379   CSEMap.InsertNode(N, IP);
5380   InsertNode(N);
5381   return SDValue(N, 0);
5382 }
5383 
5384 SDValue
5385 SelectionDAG::getMaskedGather(SDVTList VTs, EVT VT, SDLoc dl,
5386                               ArrayRef<SDValue> Ops,
5387                               MachineMemOperand *MMO) {
5388   assert(Ops.size() == 5 && "Incompatible number of operands");
5389 
5390   FoldingSetNodeID ID;
5391   AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
5392   ID.AddInteger(VT.getRawBits());
5393   ID.AddInteger(encodeMemSDNodeFlags(ISD::NON_EXTLOAD, ISD::UNINDEXED,
5394                                      MMO->isVolatile(),
5395                                      MMO->isNonTemporal(),
5396                                      MMO->isInvariant()));
5397   ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5398   void *IP = nullptr;
5399   if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
5400     cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
5401     return SDValue(E, 0);
5402   }
5403 
5404   auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
5405                                           VTs, VT, MMO);
5406   createOperands(N, Ops);
5407 
5408   assert(N->getValue().getValueType() == N->getValueType(0) &&
5409          "Incompatible type of the PassThru value in MaskedGatherSDNode");
5410   assert(N->getMask().getValueType().getVectorNumElements() ==
5411              N->getValueType(0).getVectorNumElements() &&
5412          "Vector width mismatch between mask and data");
5413   assert(N->getIndex().getValueType().getVectorNumElements() ==
5414              N->getValueType(0).getVectorNumElements() &&
5415          "Vector width mismatch between index and data");
5416 
5417   CSEMap.InsertNode(N, IP);
5418   InsertNode(N);
5419   return SDValue(N, 0);
5420 }
5421 
5422 SDValue SelectionDAG::getMaskedScatter(SDVTList VTs, EVT VT, SDLoc dl,
5423                                        ArrayRef<SDValue> Ops,
5424                                        MachineMemOperand *MMO) {
5425   assert(Ops.size() == 5 && "Incompatible number of operands");
5426 
5427   FoldingSetNodeID ID;
5428   AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
5429   ID.AddInteger(VT.getRawBits());
5430   ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5431                                      MMO->isNonTemporal(),
5432                                      MMO->isInvariant()));
5433   ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5434   void *IP = nullptr;
5435   if (SDNode *E = FindNodeOrInsertPos(ID, dl.getDebugLoc(), IP)) {
5436     cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
5437     return SDValue(E, 0);
5438   }
5439   auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
5440                                            VTs, VT, MMO);
5441   createOperands(N, Ops);
5442 
5443   assert(N->getMask().getValueType().getVectorNumElements() ==
5444              N->getValue().getValueType().getVectorNumElements() &&
5445          "Vector width mismatch between mask and data");
5446   assert(N->getIndex().getValueType().getVectorNumElements() ==
5447              N->getValue().getValueType().getVectorNumElements() &&
5448          "Vector width mismatch between index and data");
5449 
5450   CSEMap.InsertNode(N, IP);
5451   InsertNode(N);
5452   return SDValue(N, 0);
5453 }
5454 
5455 SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
5456                                SDValue Chain, SDValue Ptr,
5457                                SDValue SV,
5458                                unsigned Align) {
5459   SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
5460   return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
5461 }
5462 
5463 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
5464                               ArrayRef<SDUse> Ops) {
5465   switch (Ops.size()) {
5466   case 0: return getNode(Opcode, DL, VT);
5467   case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
5468   case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
5469   case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
5470   default: break;
5471   }
5472 
5473   // Copy from an SDUse array into an SDValue array for use with
5474   // the regular getNode logic.
5475   SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
5476   return getNode(Opcode, DL, VT, NewOps);
5477 }
5478 
5479 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
5480                               ArrayRef<SDValue> Ops, const SDNodeFlags *Flags) {
5481   unsigned NumOps = Ops.size();
5482   switch (NumOps) {
5483   case 0: return getNode(Opcode, DL, VT);
5484   case 1: return getNode(Opcode, DL, VT, Ops[0]);
5485   case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
5486   case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
5487   default: break;
5488   }
5489 
5490   switch (Opcode) {
5491   default: break;
5492   case ISD::CONCAT_VECTORS: {
5493     // Attempt to fold CONCAT_VECTORS into BUILD_VECTOR or UNDEF.
5494     if (SDValue V = FoldCONCAT_VECTORS(DL, VT, Ops, *this))
5495       return V;
5496     break;
5497   }
5498   case ISD::SELECT_CC: {
5499     assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
5500     assert(Ops[0].getValueType() == Ops[1].getValueType() &&
5501            "LHS and RHS of condition must have same type!");
5502     assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5503            "True and False arms of SelectCC must have same type!");
5504     assert(Ops[2].getValueType() == VT &&
5505            "select_cc node must be of same type as true and false value!");
5506     break;
5507   }
5508   case ISD::BR_CC: {
5509     assert(NumOps == 5 && "BR_CC takes 5 operands!");
5510     assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5511            "LHS/RHS of comparison should match types!");
5512     break;
5513   }
5514   }
5515 
5516   // Memoize nodes.
5517   SDNode *N;
5518   SDVTList VTs = getVTList(VT);
5519 
5520   if (VT != MVT::Glue) {
5521     FoldingSetNodeID ID;
5522     AddNodeIDNode(ID, Opcode, VTs, Ops);
5523     void *IP = nullptr;
5524 
5525     if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP))
5526       return SDValue(E, 0);
5527 
5528     N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
5529     createOperands(N, Ops);
5530 
5531     CSEMap.InsertNode(N, IP);
5532   } else {
5533     N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
5534     createOperands(N, Ops);
5535   }
5536 
5537   InsertNode(N);
5538   return SDValue(N, 0);
5539 }
5540 
5541 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
5542                               ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
5543   return getNode(Opcode, DL, getVTList(ResultTys), Ops);
5544 }
5545 
5546 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
5547                               ArrayRef<SDValue> Ops) {
5548   if (VTList.NumVTs == 1)
5549     return getNode(Opcode, DL, VTList.VTs[0], Ops);
5550 
5551 #if 0
5552   switch (Opcode) {
5553   // FIXME: figure out how to safely handle things like
5554   // int foo(int x) { return 1 << (x & 255); }
5555   // int bar() { return foo(256); }
5556   case ISD::SRA_PARTS:
5557   case ISD::SRL_PARTS:
5558   case ISD::SHL_PARTS:
5559     if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
5560         cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
5561       return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
5562     else if (N3.getOpcode() == ISD::AND)
5563       if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
5564         // If the and is only masking out bits that cannot effect the shift,
5565         // eliminate the and.
5566         unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
5567         if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
5568           return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
5569       }
5570     break;
5571   }
5572 #endif
5573 
5574   // Memoize the node unless it returns a flag.
5575   SDNode *N;
5576   if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
5577     FoldingSetNodeID ID;
5578     AddNodeIDNode(ID, Opcode, VTList, Ops);
5579     void *IP = nullptr;
5580     if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP))
5581       return SDValue(E, 0);
5582 
5583     N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
5584     createOperands(N, Ops);
5585     CSEMap.InsertNode(N, IP);
5586   } else {
5587     N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
5588     createOperands(N, Ops);
5589   }
5590   InsertNode(N);
5591   return SDValue(N, 0);
5592 }
5593 
5594 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
5595   return getNode(Opcode, DL, VTList, None);
5596 }
5597 
5598 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
5599                               SDValue N1) {
5600   SDValue Ops[] = { N1 };
5601   return getNode(Opcode, DL, VTList, Ops);
5602 }
5603 
5604 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
5605                               SDValue N1, SDValue N2) {
5606   SDValue Ops[] = { N1, N2 };
5607   return getNode(Opcode, DL, VTList, Ops);
5608 }
5609 
5610 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
5611                               SDValue N1, SDValue N2, SDValue N3) {
5612   SDValue Ops[] = { N1, N2, N3 };
5613   return getNode(Opcode, DL, VTList, Ops);
5614 }
5615 
5616 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
5617                               SDValue N1, SDValue N2, SDValue N3,
5618                               SDValue N4) {
5619   SDValue Ops[] = { N1, N2, N3, N4 };
5620   return getNode(Opcode, DL, VTList, Ops);
5621 }
5622 
5623 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
5624                               SDValue N1, SDValue N2, SDValue N3,
5625                               SDValue N4, SDValue N5) {
5626   SDValue Ops[] = { N1, N2, N3, N4, N5 };
5627   return getNode(Opcode, DL, VTList, Ops);
5628 }
5629 
5630 SDVTList SelectionDAG::getVTList(EVT VT) {
5631   return makeVTList(SDNode::getValueTypeList(VT), 1);
5632 }
5633 
5634 SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
5635   FoldingSetNodeID ID;
5636   ID.AddInteger(2U);
5637   ID.AddInteger(VT1.getRawBits());
5638   ID.AddInteger(VT2.getRawBits());
5639 
5640   void *IP = nullptr;
5641   SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5642   if (!Result) {
5643     EVT *Array = Allocator.Allocate<EVT>(2);
5644     Array[0] = VT1;
5645     Array[1] = VT2;
5646     Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5647     VTListMap.InsertNode(Result, IP);
5648   }
5649   return Result->getSDVTList();
5650 }
5651 
5652 SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
5653   FoldingSetNodeID ID;
5654   ID.AddInteger(3U);
5655   ID.AddInteger(VT1.getRawBits());
5656   ID.AddInteger(VT2.getRawBits());
5657   ID.AddInteger(VT3.getRawBits());
5658 
5659   void *IP = nullptr;
5660   SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5661   if (!Result) {
5662     EVT *Array = Allocator.Allocate<EVT>(3);
5663     Array[0] = VT1;
5664     Array[1] = VT2;
5665     Array[2] = VT3;
5666     Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5667     VTListMap.InsertNode(Result, IP);
5668   }
5669   return Result->getSDVTList();
5670 }
5671 
5672 SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
5673   FoldingSetNodeID ID;
5674   ID.AddInteger(4U);
5675   ID.AddInteger(VT1.getRawBits());
5676   ID.AddInteger(VT2.getRawBits());
5677   ID.AddInteger(VT3.getRawBits());
5678   ID.AddInteger(VT4.getRawBits());
5679 
5680   void *IP = nullptr;
5681   SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5682   if (!Result) {
5683     EVT *Array = Allocator.Allocate<EVT>(4);
5684     Array[0] = VT1;
5685     Array[1] = VT2;
5686     Array[2] = VT3;
5687     Array[3] = VT4;
5688     Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5689     VTListMap.InsertNode(Result, IP);
5690   }
5691   return Result->getSDVTList();
5692 }
5693 
5694 SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5695   unsigned NumVTs = VTs.size();
5696   FoldingSetNodeID ID;
5697   ID.AddInteger(NumVTs);
5698   for (unsigned index = 0; index < NumVTs; index++) {
5699     ID.AddInteger(VTs[index].getRawBits());
5700   }
5701 
5702   void *IP = nullptr;
5703   SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5704   if (!Result) {
5705     EVT *Array = Allocator.Allocate<EVT>(NumVTs);
5706     std::copy(VTs.begin(), VTs.end(), Array);
5707     Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5708     VTListMap.InsertNode(Result, IP);
5709   }
5710   return Result->getSDVTList();
5711 }
5712 
5713 
5714 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5715 /// specified operands.  If the resultant node already exists in the DAG,
5716 /// this does not modify the specified node, instead it returns the node that
5717 /// already exists.  If the resultant node does not exist in the DAG, the
5718 /// input node is returned.  As a degenerate case, if you specify the same
5719 /// input operands as the node already has, the input node is returned.
5720 SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
5721   assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
5722 
5723   // Check to see if there is no change.
5724   if (Op == N->getOperand(0)) return N;
5725 
5726   // See if the modified node already exists.
5727   void *InsertPos = nullptr;
5728   if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
5729     return Existing;
5730 
5731   // Nope it doesn't.  Remove the node from its current place in the maps.
5732   if (InsertPos)
5733     if (!RemoveNodeFromCSEMaps(N))
5734       InsertPos = nullptr;
5735 
5736   // Now we update the operands.
5737   N->OperandList[0].set(Op);
5738 
5739   // If this gets put into a CSE map, add it.
5740   if (InsertPos) CSEMap.InsertNode(N, InsertPos);
5741   return N;
5742 }
5743 
5744 SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
5745   assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
5746 
5747   // Check to see if there is no change.
5748   if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
5749     return N;   // No operands changed, just return the input node.
5750 
5751   // See if the modified node already exists.
5752   void *InsertPos = nullptr;
5753   if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
5754     return Existing;
5755 
5756   // Nope it doesn't.  Remove the node from its current place in the maps.
5757   if (InsertPos)
5758     if (!RemoveNodeFromCSEMaps(N))
5759       InsertPos = nullptr;
5760 
5761   // Now we update the operands.
5762   if (N->OperandList[0] != Op1)
5763     N->OperandList[0].set(Op1);
5764   if (N->OperandList[1] != Op2)
5765     N->OperandList[1].set(Op2);
5766 
5767   // If this gets put into a CSE map, add it.
5768   if (InsertPos) CSEMap.InsertNode(N, InsertPos);
5769   return N;
5770 }
5771 
5772 SDNode *SelectionDAG::
5773 UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
5774   SDValue Ops[] = { Op1, Op2, Op3 };
5775   return UpdateNodeOperands(N, Ops);
5776 }
5777 
5778 SDNode *SelectionDAG::
5779 UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
5780                    SDValue Op3, SDValue Op4) {
5781   SDValue Ops[] = { Op1, Op2, Op3, Op4 };
5782   return UpdateNodeOperands(N, Ops);
5783 }
5784 
5785 SDNode *SelectionDAG::
5786 UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
5787                    SDValue Op3, SDValue Op4, SDValue Op5) {
5788   SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
5789   return UpdateNodeOperands(N, Ops);
5790 }
5791 
5792 SDNode *SelectionDAG::
5793 UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5794   unsigned NumOps = Ops.size();
5795   assert(N->getNumOperands() == NumOps &&
5796          "Update with wrong number of operands");
5797 
5798   // If no operands changed just return the input node.
5799   if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
5800     return N;
5801 
5802   // See if the modified node already exists.
5803   void *InsertPos = nullptr;
5804   if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
5805     return Existing;
5806 
5807   // Nope it doesn't.  Remove the node from its current place in the maps.
5808   if (InsertPos)
5809     if (!RemoveNodeFromCSEMaps(N))
5810       InsertPos = nullptr;
5811 
5812   // Now we update the operands.
5813   for (unsigned i = 0; i != NumOps; ++i)
5814     if (N->OperandList[i] != Ops[i])
5815       N->OperandList[i].set(Ops[i]);
5816 
5817   // If this gets put into a CSE map, add it.
5818   if (InsertPos) CSEMap.InsertNode(N, InsertPos);
5819   return N;
5820 }
5821 
5822 /// DropOperands - Release the operands and set this node to have
5823 /// zero operands.
5824 void SDNode::DropOperands() {
5825   // Unlike the code in MorphNodeTo that does this, we don't need to
5826   // watch for dead nodes here.
5827   for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5828     SDUse &Use = *I++;
5829     Use.set(SDValue());
5830   }
5831 }
5832 
5833 /// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5834 /// machine opcode.
5835 ///
5836 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5837                                    EVT VT) {
5838   SDVTList VTs = getVTList(VT);
5839   return SelectNodeTo(N, MachineOpc, VTs, None);
5840 }
5841 
5842 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5843                                    EVT VT, SDValue Op1) {
5844   SDVTList VTs = getVTList(VT);
5845   SDValue Ops[] = { Op1 };
5846   return SelectNodeTo(N, MachineOpc, VTs, Ops);
5847 }
5848 
5849 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5850                                    EVT VT, SDValue Op1,
5851                                    SDValue Op2) {
5852   SDVTList VTs = getVTList(VT);
5853   SDValue Ops[] = { Op1, Op2 };
5854   return SelectNodeTo(N, MachineOpc, VTs, Ops);
5855 }
5856 
5857 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5858                                    EVT VT, SDValue Op1,
5859                                    SDValue Op2, SDValue Op3) {
5860   SDVTList VTs = getVTList(VT);
5861   SDValue Ops[] = { Op1, Op2, Op3 };
5862   return SelectNodeTo(N, MachineOpc, VTs, Ops);
5863 }
5864 
5865 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5866                                    EVT VT, ArrayRef<SDValue> Ops) {
5867   SDVTList VTs = getVTList(VT);
5868   return SelectNodeTo(N, MachineOpc, VTs, Ops);
5869 }
5870 
5871 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5872                                    EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
5873   SDVTList VTs = getVTList(VT1, VT2);
5874   return SelectNodeTo(N, MachineOpc, VTs, Ops);
5875 }
5876 
5877 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5878                                    EVT VT1, EVT VT2) {
5879   SDVTList VTs = getVTList(VT1, VT2);
5880   return SelectNodeTo(N, MachineOpc, VTs, None);
5881 }
5882 
5883 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5884                                    EVT VT1, EVT VT2, EVT VT3,
5885                                    ArrayRef<SDValue> Ops) {
5886   SDVTList VTs = getVTList(VT1, VT2, VT3);
5887   return SelectNodeTo(N, MachineOpc, VTs, Ops);
5888 }
5889 
5890 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5891                                    EVT VT1, EVT VT2, EVT VT3, EVT VT4,
5892                                    ArrayRef<SDValue> Ops) {
5893   SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
5894   return SelectNodeTo(N, MachineOpc, VTs, Ops);
5895 }
5896 
5897 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5898                                    EVT VT1, EVT VT2,
5899                                    SDValue Op1) {
5900   SDVTList VTs = getVTList(VT1, VT2);
5901   SDValue Ops[] = { Op1 };
5902   return SelectNodeTo(N, MachineOpc, VTs, Ops);
5903 }
5904 
5905 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5906                                    EVT VT1, EVT VT2,
5907                                    SDValue Op1, SDValue Op2) {
5908   SDVTList VTs = getVTList(VT1, VT2);
5909   SDValue Ops[] = { Op1, Op2 };
5910   return SelectNodeTo(N, MachineOpc, VTs, Ops);
5911 }
5912 
5913 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5914                                    EVT VT1, EVT VT2,
5915                                    SDValue Op1, SDValue Op2,
5916                                    SDValue Op3) {
5917   SDVTList VTs = getVTList(VT1, VT2);
5918   SDValue Ops[] = { Op1, Op2, Op3 };
5919   return SelectNodeTo(N, MachineOpc, VTs, Ops);
5920 }
5921 
5922 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5923                                    EVT VT1, EVT VT2, EVT VT3,
5924                                    SDValue Op1, SDValue Op2,
5925                                    SDValue Op3) {
5926   SDVTList VTs = getVTList(VT1, VT2, VT3);
5927   SDValue Ops[] = { Op1, Op2, Op3 };
5928   return SelectNodeTo(N, MachineOpc, VTs, Ops);
5929 }
5930 
5931 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5932                                    SDVTList VTs,ArrayRef<SDValue> Ops) {
5933   N = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
5934   // Reset the NodeID to -1.
5935   N->setNodeId(-1);
5936   return N;
5937 }
5938 
5939 /// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
5940 /// the line number information on the merged node since it is not possible to
5941 /// preserve the information that operation is associated with multiple lines.
5942 /// This will make the debugger working better at -O0, were there is a higher
5943 /// probability having other instructions associated with that line.
5944 ///
5945 /// For IROrder, we keep the smaller of the two
5946 SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
5947   DebugLoc NLoc = N->getDebugLoc();
5948   if (NLoc && OptLevel == CodeGenOpt::None && OLoc.getDebugLoc() != NLoc) {
5949     N->setDebugLoc(DebugLoc());
5950   }
5951   unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5952   N->setIROrder(Order);
5953   return N;
5954 }
5955 
5956 /// MorphNodeTo - This *mutates* the specified node to have the specified
5957 /// return type, opcode, and operands.
5958 ///
5959 /// Note that MorphNodeTo returns the resultant node.  If there is already a
5960 /// node of the specified opcode and operands, it returns that node instead of
5961 /// the current one.  Note that the SDLoc need not be the same.
5962 ///
5963 /// Using MorphNodeTo is faster than creating a new node and swapping it in
5964 /// with ReplaceAllUsesWith both because it often avoids allocating a new
5965 /// node, and because it doesn't require CSE recalculation for any of
5966 /// the node's users.
5967 ///
5968 /// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
5969 /// As a consequence it isn't appropriate to use from within the DAG combiner or
5970 /// the legalizer which maintain worklists that would need to be updated when
5971 /// deleting things.
5972 SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
5973                                   SDVTList VTs, ArrayRef<SDValue> Ops) {
5974   // If an identical node already exists, use it.
5975   void *IP = nullptr;
5976   if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
5977     FoldingSetNodeID ID;
5978     AddNodeIDNode(ID, Opc, VTs, Ops);
5979     if (SDNode *ON = FindNodeOrInsertPos(ID, N->getDebugLoc(), IP))
5980       return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
5981   }
5982 
5983   if (!RemoveNodeFromCSEMaps(N))
5984     IP = nullptr;
5985 
5986   // Start the morphing.
5987   N->NodeType = Opc;
5988   N->ValueList = VTs.VTs;
5989   N->NumValues = VTs.NumVTs;
5990 
5991   // Clear the operands list, updating used nodes to remove this from their
5992   // use list.  Keep track of any operands that become dead as a result.
5993   SmallPtrSet<SDNode*, 16> DeadNodeSet;
5994   for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5995     SDUse &Use = *I++;
5996     SDNode *Used = Use.getNode();
5997     Use.set(SDValue());
5998     if (Used->use_empty())
5999       DeadNodeSet.insert(Used);
6000   }
6001 
6002   // For MachineNode, initialize the memory references information.
6003   if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N))
6004     MN->setMemRefs(nullptr, nullptr);
6005 
6006   // Swap for an appropriately sized array from the recycler.
6007   removeOperands(N);
6008   createOperands(N, Ops);
6009 
6010   // Delete any nodes that are still dead after adding the uses for the
6011   // new operands.
6012   if (!DeadNodeSet.empty()) {
6013     SmallVector<SDNode *, 16> DeadNodes;
6014     for (SDNode *N : DeadNodeSet)
6015       if (N->use_empty())
6016         DeadNodes.push_back(N);
6017     RemoveDeadNodes(DeadNodes);
6018   }
6019 
6020   if (IP)
6021     CSEMap.InsertNode(N, IP);   // Memoize the new node.
6022   return N;
6023 }
6024 
6025 
6026 /// getMachineNode - These are used for target selectors to create a new node
6027 /// with specified return type(s), MachineInstr opcode, and operands.
6028 ///
6029 /// Note that getMachineNode returns the resultant node.  If there is already a
6030 /// node of the specified opcode and operands, it returns that node instead of
6031 /// the current one.
6032 MachineSDNode *
6033 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
6034   SDVTList VTs = getVTList(VT);
6035   return getMachineNode(Opcode, dl, VTs, None);
6036 }
6037 
6038 MachineSDNode *
6039 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
6040   SDVTList VTs = getVTList(VT);
6041   SDValue Ops[] = { Op1 };
6042   return getMachineNode(Opcode, dl, VTs, Ops);
6043 }
6044 
6045 MachineSDNode *
6046 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
6047                              SDValue Op1, SDValue Op2) {
6048   SDVTList VTs = getVTList(VT);
6049   SDValue Ops[] = { Op1, Op2 };
6050   return getMachineNode(Opcode, dl, VTs, Ops);
6051 }
6052 
6053 MachineSDNode *
6054 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
6055                              SDValue Op1, SDValue Op2, SDValue Op3) {
6056   SDVTList VTs = getVTList(VT);
6057   SDValue Ops[] = { Op1, Op2, Op3 };
6058   return getMachineNode(Opcode, dl, VTs, Ops);
6059 }
6060 
6061 MachineSDNode *
6062 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
6063                              ArrayRef<SDValue> Ops) {
6064   SDVTList VTs = getVTList(VT);
6065   return getMachineNode(Opcode, dl, VTs, Ops);
6066 }
6067 
6068 MachineSDNode *
6069 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
6070   SDVTList VTs = getVTList(VT1, VT2);
6071   return getMachineNode(Opcode, dl, VTs, None);
6072 }
6073 
6074 MachineSDNode *
6075 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
6076                              EVT VT1, EVT VT2, SDValue Op1) {
6077   SDVTList VTs = getVTList(VT1, VT2);
6078   SDValue Ops[] = { Op1 };
6079   return getMachineNode(Opcode, dl, VTs, Ops);
6080 }
6081 
6082 MachineSDNode *
6083 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
6084                              EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
6085   SDVTList VTs = getVTList(VT1, VT2);
6086   SDValue Ops[] = { Op1, Op2 };
6087   return getMachineNode(Opcode, dl, VTs, Ops);
6088 }
6089 
6090 MachineSDNode *
6091 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
6092                              EVT VT1, EVT VT2, SDValue Op1,
6093                              SDValue Op2, SDValue Op3) {
6094   SDVTList VTs = getVTList(VT1, VT2);
6095   SDValue Ops[] = { Op1, Op2, Op3 };
6096   return getMachineNode(Opcode, dl, VTs, Ops);
6097 }
6098 
6099 MachineSDNode *
6100 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
6101                              EVT VT1, EVT VT2,
6102                              ArrayRef<SDValue> Ops) {
6103   SDVTList VTs = getVTList(VT1, VT2);
6104   return getMachineNode(Opcode, dl, VTs, Ops);
6105 }
6106 
6107 MachineSDNode *
6108 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
6109                              EVT VT1, EVT VT2, EVT VT3,
6110                              SDValue Op1, SDValue Op2) {
6111   SDVTList VTs = getVTList(VT1, VT2, VT3);
6112   SDValue Ops[] = { Op1, Op2 };
6113   return getMachineNode(Opcode, dl, VTs, Ops);
6114 }
6115 
6116 MachineSDNode *
6117 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
6118                              EVT VT1, EVT VT2, EVT VT3,
6119                              SDValue Op1, SDValue Op2, SDValue Op3) {
6120   SDVTList VTs = getVTList(VT1, VT2, VT3);
6121   SDValue Ops[] = { Op1, Op2, Op3 };
6122   return getMachineNode(Opcode, dl, VTs, Ops);
6123 }
6124 
6125 MachineSDNode *
6126 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
6127                              EVT VT1, EVT VT2, EVT VT3,
6128                              ArrayRef<SDValue> Ops) {
6129   SDVTList VTs = getVTList(VT1, VT2, VT3);
6130   return getMachineNode(Opcode, dl, VTs, Ops);
6131 }
6132 
6133 MachineSDNode *
6134 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
6135                              EVT VT2, EVT VT3, EVT VT4,
6136                              ArrayRef<SDValue> Ops) {
6137   SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
6138   return getMachineNode(Opcode, dl, VTs, Ops);
6139 }
6140 
6141 MachineSDNode *
6142 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
6143                              ArrayRef<EVT> ResultTys,
6144                              ArrayRef<SDValue> Ops) {
6145   SDVTList VTs = getVTList(ResultTys);
6146   return getMachineNode(Opcode, dl, VTs, Ops);
6147 }
6148 
6149 MachineSDNode *
6150 SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
6151                              ArrayRef<SDValue> Ops) {
6152   bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
6153   MachineSDNode *N;
6154   void *IP = nullptr;
6155 
6156   if (DoCSE) {
6157     FoldingSetNodeID ID;
6158     AddNodeIDNode(ID, ~Opcode, VTs, Ops);
6159     IP = nullptr;
6160     if (SDNode *E = FindNodeOrInsertPos(ID, DL.getDebugLoc(), IP)) {
6161       return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
6162     }
6163   }
6164 
6165   // Allocate a new MachineSDNode.
6166   N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6167   createOperands(N, Ops);
6168 
6169   if (DoCSE)
6170     CSEMap.InsertNode(N, IP);
6171 
6172   InsertNode(N);
6173   return N;
6174 }
6175 
6176 /// getTargetExtractSubreg - A convenience function for creating
6177 /// TargetOpcode::EXTRACT_SUBREG nodes.
6178 SDValue
6179 SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
6180                                      SDValue Operand) {
6181   SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
6182   SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
6183                                   VT, Operand, SRIdxVal);
6184   return SDValue(Subreg, 0);
6185 }
6186 
6187 /// getTargetInsertSubreg - A convenience function for creating
6188 /// TargetOpcode::INSERT_SUBREG nodes.
6189 SDValue
6190 SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
6191                                     SDValue Operand, SDValue Subreg) {
6192   SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
6193   SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
6194                                   VT, Operand, Subreg, SRIdxVal);
6195   return SDValue(Result, 0);
6196 }
6197 
6198 /// getNodeIfExists - Get the specified node if it's already available, or
6199 /// else return NULL.
6200 SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
6201                                       ArrayRef<SDValue> Ops,
6202                                       const SDNodeFlags *Flags) {
6203   if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
6204     FoldingSetNodeID ID;
6205     AddNodeIDNode(ID, Opcode, VTList, Ops);
6206     void *IP = nullptr;
6207     if (SDNode *E = FindNodeOrInsertPos(ID, DebugLoc(), IP)) {
6208       if (Flags)
6209         E->intersectFlagsWith(Flags);
6210       return E;
6211     }
6212   }
6213   return nullptr;
6214 }
6215 
6216 /// getDbgValue - Creates a SDDbgValue node.
6217 ///
6218 /// SDNode
6219 SDDbgValue *SelectionDAG::getDbgValue(MDNode *Var, MDNode *Expr, SDNode *N,
6220                                       unsigned R, bool IsIndirect, uint64_t Off,
6221                                       DebugLoc DL, unsigned O) {
6222   assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
6223          "Expected inlined-at fields to agree");
6224   return new (DbgInfo->getAlloc())
6225       SDDbgValue(Var, Expr, N, R, IsIndirect, Off, DL, O);
6226 }
6227 
6228 /// Constant
6229 SDDbgValue *SelectionDAG::getConstantDbgValue(MDNode *Var, MDNode *Expr,
6230                                               const Value *C, uint64_t Off,
6231                                               DebugLoc DL, unsigned O) {
6232   assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
6233          "Expected inlined-at fields to agree");
6234   return new (DbgInfo->getAlloc()) SDDbgValue(Var, Expr, C, Off, DL, O);
6235 }
6236 
6237 /// FrameIndex
6238 SDDbgValue *SelectionDAG::getFrameIndexDbgValue(MDNode *Var, MDNode *Expr,
6239                                                 unsigned FI, uint64_t Off,
6240                                                 DebugLoc DL, unsigned O) {
6241   assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
6242          "Expected inlined-at fields to agree");
6243   return new (DbgInfo->getAlloc()) SDDbgValue(Var, Expr, FI, Off, DL, O);
6244 }
6245 
6246 namespace {
6247 
6248 /// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
6249 /// pointed to by a use iterator is deleted, increment the use iterator
6250 /// so that it doesn't dangle.
6251 ///
6252 class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
6253   SDNode::use_iterator &UI;
6254   SDNode::use_iterator &UE;
6255 
6256   void NodeDeleted(SDNode *N, SDNode *E) override {
6257     // Increment the iterator as needed.
6258     while (UI != UE && N == *UI)
6259       ++UI;
6260   }
6261 
6262 public:
6263   RAUWUpdateListener(SelectionDAG &d,
6264                      SDNode::use_iterator &ui,
6265                      SDNode::use_iterator &ue)
6266     : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
6267 };
6268 
6269 }
6270 
6271 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6272 /// This can cause recursive merging of nodes in the DAG.
6273 ///
6274 /// This version assumes From has a single result value.
6275 ///
6276 void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
6277   SDNode *From = FromN.getNode();
6278   assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
6279          "Cannot replace with this method!");
6280   assert(From != To.getNode() && "Cannot replace uses of with self");
6281 
6282   // Iterate over all the existing uses of From. New uses will be added
6283   // to the beginning of the use list, which we avoid visiting.
6284   // This specifically avoids visiting uses of From that arise while the
6285   // replacement is happening, because any such uses would be the result
6286   // of CSE: If an existing node looks like From after one of its operands
6287   // is replaced by To, we don't want to replace of all its users with To
6288   // too. See PR3018 for more info.
6289   SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
6290   RAUWUpdateListener Listener(*this, UI, UE);
6291   while (UI != UE) {
6292     SDNode *User = *UI;
6293 
6294     // This node is about to morph, remove its old self from the CSE maps.
6295     RemoveNodeFromCSEMaps(User);
6296 
6297     // A user can appear in a use list multiple times, and when this
6298     // happens the uses are usually next to each other in the list.
6299     // To help reduce the number of CSE recomputations, process all
6300     // the uses of this user that we can find this way.
6301     do {
6302       SDUse &Use = UI.getUse();
6303       ++UI;
6304       Use.set(To);
6305     } while (UI != UE && *UI == User);
6306 
6307     // Now that we have modified User, add it back to the CSE maps.  If it
6308     // already exists there, recursively merge the results together.
6309     AddModifiedNodeToCSEMaps(User);
6310   }
6311 
6312   // If we just RAUW'd the root, take note.
6313   if (FromN == getRoot())
6314     setRoot(To);
6315 }
6316 
6317 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6318 /// This can cause recursive merging of nodes in the DAG.
6319 ///
6320 /// This version assumes that for each value of From, there is a
6321 /// corresponding value in To in the same position with the same type.
6322 ///
6323 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
6324 #ifndef NDEBUG
6325   for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
6326     assert((!From->hasAnyUseOfValue(i) ||
6327             From->getValueType(i) == To->getValueType(i)) &&
6328            "Cannot use this version of ReplaceAllUsesWith!");
6329 #endif
6330 
6331   // Handle the trivial case.
6332   if (From == To)
6333     return;
6334 
6335   // Iterate over just the existing users of From. See the comments in
6336   // the ReplaceAllUsesWith above.
6337   SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
6338   RAUWUpdateListener Listener(*this, UI, UE);
6339   while (UI != UE) {
6340     SDNode *User = *UI;
6341 
6342     // This node is about to morph, remove its old self from the CSE maps.
6343     RemoveNodeFromCSEMaps(User);
6344 
6345     // A user can appear in a use list multiple times, and when this
6346     // happens the uses are usually next to each other in the list.
6347     // To help reduce the number of CSE recomputations, process all
6348     // the uses of this user that we can find this way.
6349     do {
6350       SDUse &Use = UI.getUse();
6351       ++UI;
6352       Use.setNode(To);
6353     } while (UI != UE && *UI == User);
6354 
6355     // Now that we have modified User, add it back to the CSE maps.  If it
6356     // already exists there, recursively merge the results together.
6357     AddModifiedNodeToCSEMaps(User);
6358   }
6359 
6360   // If we just RAUW'd the root, take note.
6361   if (From == getRoot().getNode())
6362     setRoot(SDValue(To, getRoot().getResNo()));
6363 }
6364 
6365 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6366 /// This can cause recursive merging of nodes in the DAG.
6367 ///
6368 /// This version can replace From with any result values.  To must match the
6369 /// number and types of values returned by From.
6370 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
6371   if (From->getNumValues() == 1)  // Handle the simple case efficiently.
6372     return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
6373 
6374   // Iterate over just the existing users of From. See the comments in
6375   // the ReplaceAllUsesWith above.
6376   SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
6377   RAUWUpdateListener Listener(*this, UI, UE);
6378   while (UI != UE) {
6379     SDNode *User = *UI;
6380 
6381     // This node is about to morph, remove its old self from the CSE maps.
6382     RemoveNodeFromCSEMaps(User);
6383 
6384     // A user can appear in a use list multiple times, and when this
6385     // happens the uses are usually next to each other in the list.
6386     // To help reduce the number of CSE recomputations, process all
6387     // the uses of this user that we can find this way.
6388     do {
6389       SDUse &Use = UI.getUse();
6390       const SDValue &ToOp = To[Use.getResNo()];
6391       ++UI;
6392       Use.set(ToOp);
6393     } while (UI != UE && *UI == User);
6394 
6395     // Now that we have modified User, add it back to the CSE maps.  If it
6396     // already exists there, recursively merge the results together.
6397     AddModifiedNodeToCSEMaps(User);
6398   }
6399 
6400   // If we just RAUW'd the root, take note.
6401   if (From == getRoot().getNode())
6402     setRoot(SDValue(To[getRoot().getResNo()]));
6403 }
6404 
6405 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
6406 /// uses of other values produced by From.getNode() alone.  The Deleted
6407 /// vector is handled the same way as for ReplaceAllUsesWith.
6408 void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
6409   // Handle the really simple, really trivial case efficiently.
6410   if (From == To) return;
6411 
6412   // Handle the simple, trivial, case efficiently.
6413   if (From.getNode()->getNumValues() == 1) {
6414     ReplaceAllUsesWith(From, To);
6415     return;
6416   }
6417 
6418   // Iterate over just the existing users of From. See the comments in
6419   // the ReplaceAllUsesWith above.
6420   SDNode::use_iterator UI = From.getNode()->use_begin(),
6421                        UE = From.getNode()->use_end();
6422   RAUWUpdateListener Listener(*this, UI, UE);
6423   while (UI != UE) {
6424     SDNode *User = *UI;
6425     bool UserRemovedFromCSEMaps = false;
6426 
6427     // A user can appear in a use list multiple times, and when this
6428     // happens the uses are usually next to each other in the list.
6429     // To help reduce the number of CSE recomputations, process all
6430     // the uses of this user that we can find this way.
6431     do {
6432       SDUse &Use = UI.getUse();
6433 
6434       // Skip uses of different values from the same node.
6435       if (Use.getResNo() != From.getResNo()) {
6436         ++UI;
6437         continue;
6438       }
6439 
6440       // If this node hasn't been modified yet, it's still in the CSE maps,
6441       // so remove its old self from the CSE maps.
6442       if (!UserRemovedFromCSEMaps) {
6443         RemoveNodeFromCSEMaps(User);
6444         UserRemovedFromCSEMaps = true;
6445       }
6446 
6447       ++UI;
6448       Use.set(To);
6449     } while (UI != UE && *UI == User);
6450 
6451     // We are iterating over all uses of the From node, so if a use
6452     // doesn't use the specific value, no changes are made.
6453     if (!UserRemovedFromCSEMaps)
6454       continue;
6455 
6456     // Now that we have modified User, add it back to the CSE maps.  If it
6457     // already exists there, recursively merge the results together.
6458     AddModifiedNodeToCSEMaps(User);
6459   }
6460 
6461   // If we just RAUW'd the root, take note.
6462   if (From == getRoot())
6463     setRoot(To);
6464 }
6465 
6466 namespace {
6467   /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
6468   /// to record information about a use.
6469   struct UseMemo {
6470     SDNode *User;
6471     unsigned Index;
6472     SDUse *Use;
6473   };
6474 
6475   /// operator< - Sort Memos by User.
6476   bool operator<(const UseMemo &L, const UseMemo &R) {
6477     return (intptr_t)L.User < (intptr_t)R.User;
6478   }
6479 }
6480 
6481 /// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
6482 /// uses of other values produced by From.getNode() alone.  The same value
6483 /// may appear in both the From and To list.  The Deleted vector is
6484 /// handled the same way as for ReplaceAllUsesWith.
6485 void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
6486                                               const SDValue *To,
6487                                               unsigned Num){
6488   // Handle the simple, trivial case efficiently.
6489   if (Num == 1)
6490     return ReplaceAllUsesOfValueWith(*From, *To);
6491 
6492   // Read up all the uses and make records of them. This helps
6493   // processing new uses that are introduced during the
6494   // replacement process.
6495   SmallVector<UseMemo, 4> Uses;
6496   for (unsigned i = 0; i != Num; ++i) {
6497     unsigned FromResNo = From[i].getResNo();
6498     SDNode *FromNode = From[i].getNode();
6499     for (SDNode::use_iterator UI = FromNode->use_begin(),
6500          E = FromNode->use_end(); UI != E; ++UI) {
6501       SDUse &Use = UI.getUse();
6502       if (Use.getResNo() == FromResNo) {
6503         UseMemo Memo = { *UI, i, &Use };
6504         Uses.push_back(Memo);
6505       }
6506     }
6507   }
6508 
6509   // Sort the uses, so that all the uses from a given User are together.
6510   std::sort(Uses.begin(), Uses.end());
6511 
6512   for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
6513        UseIndex != UseIndexEnd; ) {
6514     // We know that this user uses some value of From.  If it is the right
6515     // value, update it.
6516     SDNode *User = Uses[UseIndex].User;
6517 
6518     // This node is about to morph, remove its old self from the CSE maps.
6519     RemoveNodeFromCSEMaps(User);
6520 
6521     // The Uses array is sorted, so all the uses for a given User
6522     // are next to each other in the list.
6523     // To help reduce the number of CSE recomputations, process all
6524     // the uses of this user that we can find this way.
6525     do {
6526       unsigned i = Uses[UseIndex].Index;
6527       SDUse &Use = *Uses[UseIndex].Use;
6528       ++UseIndex;
6529 
6530       Use.set(To[i]);
6531     } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
6532 
6533     // Now that we have modified User, add it back to the CSE maps.  If it
6534     // already exists there, recursively merge the results together.
6535     AddModifiedNodeToCSEMaps(User);
6536   }
6537 }
6538 
6539 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
6540 /// based on their topological order. It returns the maximum id and a vector
6541 /// of the SDNodes* in assigned order by reference.
6542 unsigned SelectionDAG::AssignTopologicalOrder() {
6543 
6544   unsigned DAGSize = 0;
6545 
6546   // SortedPos tracks the progress of the algorithm. Nodes before it are
6547   // sorted, nodes after it are unsorted. When the algorithm completes
6548   // it is at the end of the list.
6549   allnodes_iterator SortedPos = allnodes_begin();
6550 
6551   // Visit all the nodes. Move nodes with no operands to the front of
6552   // the list immediately. Annotate nodes that do have operands with their
6553   // operand count. Before we do this, the Node Id fields of the nodes
6554   // may contain arbitrary values. After, the Node Id fields for nodes
6555   // before SortedPos will contain the topological sort index, and the
6556   // Node Id fields for nodes At SortedPos and after will contain the
6557   // count of outstanding operands.
6558   for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
6559     SDNode *N = &*I++;
6560     checkForCycles(N, this);
6561     unsigned Degree = N->getNumOperands();
6562     if (Degree == 0) {
6563       // A node with no uses, add it to the result array immediately.
6564       N->setNodeId(DAGSize++);
6565       allnodes_iterator Q(N);
6566       if (Q != SortedPos)
6567         SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
6568       assert(SortedPos != AllNodes.end() && "Overran node list");
6569       ++SortedPos;
6570     } else {
6571       // Temporarily use the Node Id as scratch space for the degree count.
6572       N->setNodeId(Degree);
6573     }
6574   }
6575 
6576   // Visit all the nodes. As we iterate, move nodes into sorted order,
6577   // such that by the time the end is reached all nodes will be sorted.
6578   for (SDNode &Node : allnodes()) {
6579     SDNode *N = &Node;
6580     checkForCycles(N, this);
6581     // N is in sorted position, so all its uses have one less operand
6582     // that needs to be sorted.
6583     for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
6584          UI != UE; ++UI) {
6585       SDNode *P = *UI;
6586       unsigned Degree = P->getNodeId();
6587       assert(Degree != 0 && "Invalid node degree");
6588       --Degree;
6589       if (Degree == 0) {
6590         // All of P's operands are sorted, so P may sorted now.
6591         P->setNodeId(DAGSize++);
6592         if (P->getIterator() != SortedPos)
6593           SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
6594         assert(SortedPos != AllNodes.end() && "Overran node list");
6595         ++SortedPos;
6596       } else {
6597         // Update P's outstanding operand count.
6598         P->setNodeId(Degree);
6599       }
6600     }
6601     if (Node.getIterator() == SortedPos) {
6602 #ifndef NDEBUG
6603       allnodes_iterator I(N);
6604       SDNode *S = &*++I;
6605       dbgs() << "Overran sorted position:\n";
6606       S->dumprFull(this); dbgs() << "\n";
6607       dbgs() << "Checking if this is due to cycles\n";
6608       checkForCycles(this, true);
6609 #endif
6610       llvm_unreachable(nullptr);
6611     }
6612   }
6613 
6614   assert(SortedPos == AllNodes.end() &&
6615          "Topological sort incomplete!");
6616   assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6617          "First node in topological sort is not the entry token!");
6618   assert(AllNodes.front().getNodeId() == 0 &&
6619          "First node in topological sort has non-zero id!");
6620   assert(AllNodes.front().getNumOperands() == 0 &&
6621          "First node in topological sort has operands!");
6622   assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6623          "Last node in topologic sort has unexpected id!");
6624   assert(AllNodes.back().use_empty() &&
6625          "Last node in topologic sort has users!");
6626   assert(DAGSize == allnodes_size() && "Node count mismatch!");
6627   return DAGSize;
6628 }
6629 
6630 /// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6631 /// value is produced by SD.
6632 void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
6633   if (SD) {
6634     assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
6635     SD->setHasDebugValue(true);
6636   }
6637   DbgInfo->add(DB, SD, isParameter);
6638 }
6639 
6640 /// TransferDbgValues - Transfer SDDbgValues.
6641 void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6642   if (From == To || !From.getNode()->getHasDebugValue())
6643     return;
6644   SDNode *FromNode = From.getNode();
6645   SDNode *ToNode = To.getNode();
6646   ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
6647   SmallVector<SDDbgValue *, 2> ClonedDVs;
6648   for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
6649        I != E; ++I) {
6650     SDDbgValue *Dbg = *I;
6651     if (Dbg->getKind() == SDDbgValue::SDNODE) {
6652       SDDbgValue *Clone =
6653           getDbgValue(Dbg->getVariable(), Dbg->getExpression(), ToNode,
6654                       To.getResNo(), Dbg->isIndirect(), Dbg->getOffset(),
6655                       Dbg->getDebugLoc(), Dbg->getOrder());
6656       ClonedDVs.push_back(Clone);
6657     }
6658   }
6659   for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
6660          E = ClonedDVs.end(); I != E; ++I)
6661     AddDbgValue(*I, ToNode, false);
6662 }
6663 
6664 //===----------------------------------------------------------------------===//
6665 //                              SDNode Class
6666 //===----------------------------------------------------------------------===//
6667 
6668 bool llvm::isNullConstant(SDValue V) {
6669   ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
6670   return Const != nullptr && Const->isNullValue();
6671 }
6672 
6673 bool llvm::isNullFPConstant(SDValue V) {
6674   ConstantFPSDNode *Const = dyn_cast<ConstantFPSDNode>(V);
6675   return Const != nullptr && Const->isZero() && !Const->isNegative();
6676 }
6677 
6678 bool llvm::isAllOnesConstant(SDValue V) {
6679   ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
6680   return Const != nullptr && Const->isAllOnesValue();
6681 }
6682 
6683 bool llvm::isOneConstant(SDValue V) {
6684   ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
6685   return Const != nullptr && Const->isOne();
6686 }
6687 
6688 bool llvm::isBitwiseNot(SDValue V) {
6689   return V.getOpcode() == ISD::XOR && isAllOnesConstant(V.getOperand(1));
6690 }
6691 
6692 HandleSDNode::~HandleSDNode() {
6693   DropOperands();
6694 }
6695 
6696 GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6697                                          DebugLoc DL, const GlobalValue *GA,
6698                                          EVT VT, int64_t o, unsigned char TF)
6699   : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
6700   TheGlobal = GA;
6701 }
6702 
6703 AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6704                                          unsigned SrcAS, unsigned DestAS)
6705     : SDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT)),
6706       SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6707 
6708 MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6709                      EVT memvt, MachineMemOperand *mmo)
6710  : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
6711   SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
6712                                       MMO->isNonTemporal(), MMO->isInvariant());
6713   assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
6714   assert(isNonTemporal() == MMO->isNonTemporal() &&
6715          "Non-temporal encoding error!");
6716   // We check here that the size of the memory operand fits within the size of
6717   // the MMO. This is because the MMO might indicate only a possible address
6718   // range instead of specifying the affected memory addresses precisely.
6719   assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
6720 }
6721 
6722 /// Profile - Gather unique data for the node.
6723 ///
6724 void SDNode::Profile(FoldingSetNodeID &ID) const {
6725   AddNodeIDNode(ID, this);
6726 }
6727 
6728 namespace {
6729   struct EVTArray {
6730     std::vector<EVT> VTs;
6731 
6732     EVTArray() {
6733       VTs.reserve(MVT::LAST_VALUETYPE);
6734       for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6735         VTs.push_back(MVT((MVT::SimpleValueType)i));
6736     }
6737   };
6738 }
6739 
6740 static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
6741 static ManagedStatic<EVTArray> SimpleVTArray;
6742 static ManagedStatic<sys::SmartMutex<true> > VTMutex;
6743 
6744 /// getValueTypeList - Return a pointer to the specified value type.
6745 ///
6746 const EVT *SDNode::getValueTypeList(EVT VT) {
6747   if (VT.isExtended()) {
6748     sys::SmartScopedLock<true> Lock(*VTMutex);
6749     return &(*EVTs->insert(VT).first);
6750   } else {
6751     assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
6752            "Value type out of range!");
6753     return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
6754   }
6755 }
6756 
6757 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6758 /// indicated value.  This method ignores uses of other values defined by this
6759 /// operation.
6760 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
6761   assert(Value < getNumValues() && "Bad value!");
6762 
6763   // TODO: Only iterate over uses of a given value of the node
6764   for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
6765     if (UI.getUse().getResNo() == Value) {
6766       if (NUses == 0)
6767         return false;
6768       --NUses;
6769     }
6770   }
6771 
6772   // Found exactly the right number of uses?
6773   return NUses == 0;
6774 }
6775 
6776 
6777 /// hasAnyUseOfValue - Return true if there are any use of the indicated
6778 /// value. This method ignores uses of other values defined by this operation.
6779 bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6780   assert(Value < getNumValues() && "Bad value!");
6781 
6782   for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
6783     if (UI.getUse().getResNo() == Value)
6784       return true;
6785 
6786   return false;
6787 }
6788 
6789 
6790 /// isOnlyUserOf - Return true if this node is the only use of N.
6791 ///
6792 bool SDNode::isOnlyUserOf(const SDNode *N) const {
6793   bool Seen = false;
6794   for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
6795     SDNode *User = *I;
6796     if (User == this)
6797       Seen = true;
6798     else
6799       return false;
6800   }
6801 
6802   return Seen;
6803 }
6804 
6805 /// isOperand - Return true if this node is an operand of N.
6806 ///
6807 bool SDValue::isOperandOf(const SDNode *N) const {
6808   for (const SDValue &Op : N->op_values())
6809     if (*this == Op)
6810       return true;
6811   return false;
6812 }
6813 
6814 bool SDNode::isOperandOf(const SDNode *N) const {
6815   for (const SDValue &Op : N->op_values())
6816     if (this == Op.getNode())
6817       return true;
6818   return false;
6819 }
6820 
6821 /// reachesChainWithoutSideEffects - Return true if this operand (which must
6822 /// be a chain) reaches the specified operand without crossing any
6823 /// side-effecting instructions on any chain path.  In practice, this looks
6824 /// through token factors and non-volatile loads.  In order to remain efficient,
6825 /// this only looks a couple of nodes in, it does not do an exhaustive search.
6826 bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
6827                                                unsigned Depth) const {
6828   if (*this == Dest) return true;
6829 
6830   // Don't search too deeply, we just want to be able to see through
6831   // TokenFactor's etc.
6832   if (Depth == 0) return false;
6833 
6834   // If this is a token factor, all inputs to the TF happen in parallel.  If any
6835   // of the operands of the TF does not reach dest, then we cannot do the xform.
6836   if (getOpcode() == ISD::TokenFactor) {
6837     for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
6838       if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6839         return false;
6840     return true;
6841   }
6842 
6843   // Loads don't have side effects, look through them.
6844   if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6845     if (!Ld->isVolatile())
6846       return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6847   }
6848   return false;
6849 }
6850 
6851 bool SDNode::hasPredecessor(const SDNode *N) const {
6852   SmallPtrSet<const SDNode *, 32> Visited;
6853   SmallVector<const SDNode *, 16> Worklist;
6854   Worklist.push_back(this);
6855   return hasPredecessorHelper(N, Visited, Worklist);
6856 }
6857 
6858 uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6859   assert(Num < NumOperands && "Invalid child # of SDNode!");
6860   return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
6861 }
6862 
6863 const SDNodeFlags *SDNode::getFlags() const {
6864   if (auto *FlagsNode = dyn_cast<BinaryWithFlagsSDNode>(this))
6865     return &FlagsNode->Flags;
6866   return nullptr;
6867 }
6868 
6869 void SDNode::intersectFlagsWith(const SDNodeFlags *Flags) {
6870   if (auto *FlagsNode = dyn_cast<BinaryWithFlagsSDNode>(this))
6871     FlagsNode->Flags.intersectWith(Flags);
6872 }
6873 
6874 SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6875   assert(N->getNumValues() == 1 &&
6876          "Can't unroll a vector with multiple results!");
6877 
6878   EVT VT = N->getValueType(0);
6879   unsigned NE = VT.getVectorNumElements();
6880   EVT EltVT = VT.getVectorElementType();
6881   SDLoc dl(N);
6882 
6883   SmallVector<SDValue, 8> Scalars;
6884   SmallVector<SDValue, 4> Operands(N->getNumOperands());
6885 
6886   // If ResNE is 0, fully unroll the vector op.
6887   if (ResNE == 0)
6888     ResNE = NE;
6889   else if (NE > ResNE)
6890     NE = ResNE;
6891 
6892   unsigned i;
6893   for (i= 0; i != NE; ++i) {
6894     for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
6895       SDValue Operand = N->getOperand(j);
6896       EVT OperandVT = Operand.getValueType();
6897       if (OperandVT.isVector()) {
6898         // A vector operand; extract a single element.
6899         EVT OperandEltVT = OperandVT.getVectorElementType();
6900         Operands[j] =
6901             getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT, Operand,
6902                     getConstant(i, dl, TLI->getVectorIdxTy(getDataLayout())));
6903       } else {
6904         // A scalar operand; just use it as is.
6905         Operands[j] = Operand;
6906       }
6907     }
6908 
6909     switch (N->getOpcode()) {
6910     default: {
6911       Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
6912                                 N->getFlags()));
6913       break;
6914     }
6915     case ISD::VSELECT:
6916       Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
6917       break;
6918     case ISD::SHL:
6919     case ISD::SRA:
6920     case ISD::SRL:
6921     case ISD::ROTL:
6922     case ISD::ROTR:
6923       Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
6924                                getShiftAmountOperand(Operands[0].getValueType(),
6925                                                      Operands[1])));
6926       break;
6927     case ISD::SIGN_EXTEND_INREG:
6928     case ISD::FP_ROUND_INREG: {
6929       EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6930       Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6931                                 Operands[0],
6932                                 getValueType(ExtVT)));
6933     }
6934     }
6935   }
6936 
6937   for (; i < ResNE; ++i)
6938     Scalars.push_back(getUNDEF(EltVT));
6939 
6940   return getNode(ISD::BUILD_VECTOR, dl,
6941                  EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
6942 }
6943 
6944 bool SelectionDAG::areNonVolatileConsecutiveLoads(LoadSDNode *LD,
6945                                                   LoadSDNode *Base,
6946                                                   unsigned Bytes,
6947                                                   int Dist) const {
6948   if (LD->isVolatile() || Base->isVolatile())
6949     return false;
6950   if (LD->isIndexed() || Base->isIndexed())
6951     return false;
6952   if (LD->getChain() != Base->getChain())
6953     return false;
6954   EVT VT = LD->getValueType(0);
6955   if (VT.getSizeInBits() / 8 != Bytes)
6956     return false;
6957 
6958   SDValue Loc = LD->getOperand(1);
6959   SDValue BaseLoc = Base->getOperand(1);
6960   if (Loc.getOpcode() == ISD::FrameIndex) {
6961     if (BaseLoc.getOpcode() != ISD::FrameIndex)
6962       return false;
6963     const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6964     int FI  = cast<FrameIndexSDNode>(Loc)->getIndex();
6965     int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6966     int FS  = MFI->getObjectSize(FI);
6967     int BFS = MFI->getObjectSize(BFI);
6968     if (FS != BFS || FS != (int)Bytes) return false;
6969     return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6970   }
6971 
6972   // Handle X + C.
6973   if (isBaseWithConstantOffset(Loc)) {
6974     int64_t LocOffset = cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue();
6975     if (Loc.getOperand(0) == BaseLoc) {
6976       // If the base location is a simple address with no offset itself, then
6977       // the second load's first add operand should be the base address.
6978       if (LocOffset == Dist * (int)Bytes)
6979         return true;
6980     } else if (isBaseWithConstantOffset(BaseLoc)) {
6981       // The base location itself has an offset, so subtract that value from the
6982       // second load's offset before comparing to distance * size.
6983       int64_t BOffset =
6984         cast<ConstantSDNode>(BaseLoc.getOperand(1))->getSExtValue();
6985       if (Loc.getOperand(0) == BaseLoc.getOperand(0)) {
6986         if ((LocOffset - BOffset) == Dist * (int)Bytes)
6987           return true;
6988       }
6989     }
6990   }
6991   const GlobalValue *GV1 = nullptr;
6992   const GlobalValue *GV2 = nullptr;
6993   int64_t Offset1 = 0;
6994   int64_t Offset2 = 0;
6995   bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6996   bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
6997   if (isGA1 && isGA2 && GV1 == GV2)
6998     return Offset1 == (Offset2 + Dist*Bytes);
6999   return false;
7000 }
7001 
7002 
7003 /// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
7004 /// it cannot be inferred.
7005 unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
7006   // If this is a GlobalAddress + cst, return the alignment.
7007   const GlobalValue *GV;
7008   int64_t GVOffset = 0;
7009   if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
7010     unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
7011     APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
7012     llvm::computeKnownBits(const_cast<GlobalValue *>(GV), KnownZero, KnownOne,
7013                            getDataLayout());
7014     unsigned AlignBits = KnownZero.countTrailingOnes();
7015     unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
7016     if (Align)
7017       return MinAlign(Align, GVOffset);
7018   }
7019 
7020   // If this is a direct reference to a stack slot, use information about the
7021   // stack slot's alignment.
7022   int FrameIdx = 1 << 31;
7023   int64_t FrameOffset = 0;
7024   if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
7025     FrameIdx = FI->getIndex();
7026   } else if (isBaseWithConstantOffset(Ptr) &&
7027              isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
7028     // Handle FI+Cst
7029     FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
7030     FrameOffset = Ptr.getConstantOperandVal(1);
7031   }
7032 
7033   if (FrameIdx != (1 << 31)) {
7034     const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
7035     unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
7036                                     FrameOffset);
7037     return FIInfoAlign;
7038   }
7039 
7040   return 0;
7041 }
7042 
7043 /// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
7044 /// which is split (or expanded) into two not necessarily identical pieces.
7045 std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
7046   // Currently all types are split in half.
7047   EVT LoVT, HiVT;
7048   if (!VT.isVector()) {
7049     LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
7050   } else {
7051     unsigned NumElements = VT.getVectorNumElements();
7052     assert(!(NumElements & 1) && "Splitting vector, but not in half!");
7053     LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
7054                                    NumElements/2);
7055   }
7056   return std::make_pair(LoVT, HiVT);
7057 }
7058 
7059 /// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
7060 /// low/high part.
7061 std::pair<SDValue, SDValue>
7062 SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
7063                           const EVT &HiVT) {
7064   assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
7065          N.getValueType().getVectorNumElements() &&
7066          "More vector elements requested than available!");
7067   SDValue Lo, Hi;
7068   Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
7069                getConstant(0, DL, TLI->getVectorIdxTy(getDataLayout())));
7070   Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
7071                getConstant(LoVT.getVectorNumElements(), DL,
7072                            TLI->getVectorIdxTy(getDataLayout())));
7073   return std::make_pair(Lo, Hi);
7074 }
7075 
7076 void SelectionDAG::ExtractVectorElements(SDValue Op,
7077                                          SmallVectorImpl<SDValue> &Args,
7078                                          unsigned Start, unsigned Count) {
7079   EVT VT = Op.getValueType();
7080   if (Count == 0)
7081     Count = VT.getVectorNumElements();
7082 
7083   EVT EltVT = VT.getVectorElementType();
7084   EVT IdxTy = TLI->getVectorIdxTy(getDataLayout());
7085   SDLoc SL(Op);
7086   for (unsigned i = Start, e = Start + Count; i != e; ++i) {
7087     Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
7088                            Op, getConstant(i, SL, IdxTy)));
7089   }
7090 }
7091 
7092 // getAddressSpace - Return the address space this GlobalAddress belongs to.
7093 unsigned GlobalAddressSDNode::getAddressSpace() const {
7094   return getGlobal()->getType()->getAddressSpace();
7095 }
7096 
7097 
7098 Type *ConstantPoolSDNode::getType() const {
7099   if (isMachineConstantPoolEntry())
7100     return Val.MachineCPVal->getType();
7101   return Val.ConstVal->getType();
7102 }
7103 
7104 bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
7105                                         APInt &SplatUndef,
7106                                         unsigned &SplatBitSize,
7107                                         bool &HasAnyUndefs,
7108                                         unsigned MinSplatBits,
7109                                         bool isBigEndian) const {
7110   EVT VT = getValueType(0);
7111   assert(VT.isVector() && "Expected a vector type");
7112   unsigned sz = VT.getSizeInBits();
7113   if (MinSplatBits > sz)
7114     return false;
7115 
7116   SplatValue = APInt(sz, 0);
7117   SplatUndef = APInt(sz, 0);
7118 
7119   // Get the bits.  Bits with undefined values (when the corresponding element
7120   // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
7121   // in SplatValue.  If any of the values are not constant, give up and return
7122   // false.
7123   unsigned int nOps = getNumOperands();
7124   assert(nOps > 0 && "isConstantSplat has 0-size build vector");
7125   unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
7126 
7127   for (unsigned j = 0; j < nOps; ++j) {
7128     unsigned i = isBigEndian ? nOps-1-j : j;
7129     SDValue OpVal = getOperand(i);
7130     unsigned BitPos = j * EltBitSize;
7131 
7132     if (OpVal.isUndef())
7133       SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
7134     else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
7135       SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
7136                     zextOrTrunc(sz) << BitPos;
7137     else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
7138       SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
7139      else
7140       return false;
7141   }
7142 
7143   // The build_vector is all constants or undefs.  Find the smallest element
7144   // size that splats the vector.
7145 
7146   HasAnyUndefs = (SplatUndef != 0);
7147   while (sz > 8) {
7148 
7149     unsigned HalfSize = sz / 2;
7150     APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
7151     APInt LowValue = SplatValue.trunc(HalfSize);
7152     APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
7153     APInt LowUndef = SplatUndef.trunc(HalfSize);
7154 
7155     // If the two halves do not match (ignoring undef bits), stop here.
7156     if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
7157         MinSplatBits > HalfSize)
7158       break;
7159 
7160     SplatValue = HighValue | LowValue;
7161     SplatUndef = HighUndef & LowUndef;
7162 
7163     sz = HalfSize;
7164   }
7165 
7166   SplatBitSize = sz;
7167   return true;
7168 }
7169 
7170 SDValue BuildVectorSDNode::getSplatValue(BitVector *UndefElements) const {
7171   if (UndefElements) {
7172     UndefElements->clear();
7173     UndefElements->resize(getNumOperands());
7174   }
7175   SDValue Splatted;
7176   for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
7177     SDValue Op = getOperand(i);
7178     if (Op.isUndef()) {
7179       if (UndefElements)
7180         (*UndefElements)[i] = true;
7181     } else if (!Splatted) {
7182       Splatted = Op;
7183     } else if (Splatted != Op) {
7184       return SDValue();
7185     }
7186   }
7187 
7188   if (!Splatted) {
7189     assert(getOperand(0).isUndef() &&
7190            "Can only have a splat without a constant for all undefs.");
7191     return getOperand(0);
7192   }
7193 
7194   return Splatted;
7195 }
7196 
7197 ConstantSDNode *
7198 BuildVectorSDNode::getConstantSplatNode(BitVector *UndefElements) const {
7199   return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
7200 }
7201 
7202 ConstantFPSDNode *
7203 BuildVectorSDNode::getConstantFPSplatNode(BitVector *UndefElements) const {
7204   return dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements));
7205 }
7206 
7207 int32_t
7208 BuildVectorSDNode::getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements,
7209                                                    uint32_t BitWidth) const {
7210   if (ConstantFPSDNode *CN =
7211           dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements))) {
7212     bool IsExact;
7213     APSInt IntVal(BitWidth);
7214     APFloat APF = CN->getValueAPF();
7215     if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
7216             APFloat::opOK ||
7217         !IsExact)
7218       return -1;
7219 
7220     return IntVal.exactLogBase2();
7221   }
7222   return -1;
7223 }
7224 
7225 bool BuildVectorSDNode::isConstant() const {
7226   for (const SDValue &Op : op_values()) {
7227     unsigned Opc = Op.getOpcode();
7228     if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
7229       return false;
7230   }
7231   return true;
7232 }
7233 
7234 bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
7235   // Find the first non-undef value in the shuffle mask.
7236   unsigned i, e;
7237   for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
7238     /* search */;
7239 
7240   assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
7241 
7242   // Make sure all remaining elements are either undef or the same as the first
7243   // non-undef value.
7244   for (int Idx = Mask[i]; i != e; ++i)
7245     if (Mask[i] >= 0 && Mask[i] != Idx)
7246       return false;
7247   return true;
7248 }
7249 
7250 // \brief Returns the SDNode if it is a constant integer BuildVector
7251 // or constant integer.
7252 SDNode *SelectionDAG::isConstantIntBuildVectorOrConstantInt(SDValue N) {
7253   if (isa<ConstantSDNode>(N))
7254     return N.getNode();
7255   if (ISD::isBuildVectorOfConstantSDNodes(N.getNode()))
7256     return N.getNode();
7257   // Treat a GlobalAddress supporting constant offset folding as a
7258   // constant integer.
7259   if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N))
7260     if (GA->getOpcode() == ISD::GlobalAddress &&
7261         TLI->isOffsetFoldingLegal(GA))
7262       return GA;
7263   return nullptr;
7264 }
7265 
7266 #ifndef NDEBUG
7267 static void checkForCyclesHelper(const SDNode *N,
7268                                  SmallPtrSetImpl<const SDNode*> &Visited,
7269                                  SmallPtrSetImpl<const SDNode*> &Checked,
7270                                  const llvm::SelectionDAG *DAG) {
7271   // If this node has already been checked, don't check it again.
7272   if (Checked.count(N))
7273     return;
7274 
7275   // If a node has already been visited on this depth-first walk, reject it as
7276   // a cycle.
7277   if (!Visited.insert(N).second) {
7278     errs() << "Detected cycle in SelectionDAG\n";
7279     dbgs() << "Offending node:\n";
7280     N->dumprFull(DAG); dbgs() << "\n";
7281     abort();
7282   }
7283 
7284   for (const SDValue &Op : N->op_values())
7285     checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
7286 
7287   Checked.insert(N);
7288   Visited.erase(N);
7289 }
7290 #endif
7291 
7292 void llvm::checkForCycles(const llvm::SDNode *N,
7293                           const llvm::SelectionDAG *DAG,
7294                           bool force) {
7295 #ifndef NDEBUG
7296   bool check = force;
7297 #ifdef XDEBUG
7298   check = true;
7299 #endif  // XDEBUG
7300   if (check) {
7301     assert(N && "Checking nonexistent SDNode");
7302     SmallPtrSet<const SDNode*, 32> visited;
7303     SmallPtrSet<const SDNode*, 32> checked;
7304     checkForCyclesHelper(N, visited, checked, DAG);
7305   }
7306 #endif  // !NDEBUG
7307 }
7308 
7309 void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
7310   checkForCycles(DAG->getRoot().getNode(), DAG, force);
7311 }
7312