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