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