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