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