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