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