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