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