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