1 //===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements the SelectionDAG class.
12 //===----------------------------------------------------------------------===//
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"
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};
62 // Default null implementations of the callbacks.
63 void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
64 void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
66 //===----------------------------------------------------------------------===//
67 // ConstantFPSDNode Class
68 //===----------------------------------------------------------------------===//
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);
78 bool ConstantFPSDNode::isValueValidForType(EVT VT,
80 assert(VT.isFloatingPoint() && "Can only convert between FP types");
82 // convert modifies in place, so make a copy.
83 APFloat Val2 = APFloat(Val);
85 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
86 APFloat::rmNearestTiesToEven,
91 //===----------------------------------------------------------------------===//
93 //===----------------------------------------------------------------------===//
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();
102 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
104 unsigned i = 0, e = N->getNumOperands();
106 // Skip over all of the undef values.
107 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
110 // Do not accept an all-undef vector.
111 if (i == e) return false;
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
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)
126 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
127 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
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)
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();
150 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
152 bool IsAllUndef = true;
153 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i) {
154 if (N->getOperand(i).getOpcode() == ISD::UNDEF)
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
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)
170 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Zero)) {
171 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingZeros() < EltSize)
177 // Do not accept an all-undef vector.
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)
189 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
190 SDValue Op = N->getOperand(i);
191 if (Op.getOpcode() == ISD::UNDEF)
193 if (!isa<ConstantSDNode>(Op))
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)
205 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
206 SDValue Op = N->getOperand(i);
207 if (Op.getOpcode() == ISD::UNDEF)
209 if (!isa<ConstantFPSDNode>(Op))
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)
222 if (N->getOpcode() != ISD::BUILD_VECTOR)
224 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
226 unsigned NumElems = N->getNumOperands();
229 for (unsigned i = 1; i < NumElems; ++i) {
230 SDValue V = N->getOperand(i);
231 if (V.getOpcode() != ISD::UNDEF)
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)
246 for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i)
247 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
253 ISD::NodeType ISD::getExtForLoadExtType(bool IsFP, ISD::LoadExtType ExtType) {
256 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
258 return ISD::SIGN_EXTEND;
260 return ISD::ZERO_EXTEND;
265 llvm_unreachable("Invalid LoadExtType");
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
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.
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;
285 Operation ^= 7; // Flip L, G, E bits, but not U.
287 Operation ^= 15; // Flip all of the condition bits.
289 if (Operation > ISD::SETTRUE2)
290 Operation &= ~8; // Don't let N and U bits get set.
292 return ISD::CondCode(Operation);
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) {
301 default: llvm_unreachable("Illegal integer setcc operation!");
303 case ISD::SETNE: return 0;
307 case ISD::SETGE: return 1;
311 case ISD::SETUGE: return 2;
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
319 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
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;
325 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
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.
332 // Canonicalize illegal integer setcc's.
333 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
336 return ISD::CondCode(Op);
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
343 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
345 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
346 // Cannot fold a signed setcc with an unsigned setcc.
347 return ISD::SETCC_INVALID;
349 // Combine all of the condition bits.
350 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
352 // Canonicalize illegal integer setcc's.
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
367 //===----------------------------------------------------------------------===//
368 // SDNode Profile Support
369 //===----------------------------------------------------------------------===//
371 /// AddNodeIDOpcode - Add the node opcode to the NodeID data.
373 static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
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);
383 /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
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());
393 /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
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());
403 static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, bool nuw, bool nsw,
407 ID.AddBoolean(exact);
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);
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);
424 /// AddNodeIDCustom - If this is an SDNode with special info, add this info to
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());
439 case ISD::TargetConstantFP:
440 case ISD::ConstantFP: {
441 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
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());
455 case ISD::BasicBlock:
456 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
459 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
461 case ISD::RegisterMask:
462 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
465 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
467 case ISD::FrameIndex:
468 case ISD::TargetFrameIndex:
469 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
472 case ISD::TargetJumpTable:
473 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
474 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
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);
484 ID.AddPointer(CP->getConstVal());
485 ID.AddInteger(CP->getTargetFlags());
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());
496 const LoadSDNode *LD = cast<LoadSDNode>(N);
497 ID.AddInteger(LD->getMemoryVT().getRawBits());
498 ID.AddInteger(LD->getRawSubclassData());
499 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
503 const StoreSDNode *ST = cast<StoreSDNode>(N);
504 ID.AddInteger(ST->getMemoryVT().getRawBits());
505 ID.AddInteger(ST->getRawSubclassData());
506 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
517 const BinaryWithFlagsSDNode *BinNode = cast<BinaryWithFlagsSDNode>(N);
518 AddBinaryNodeIDCustom(
519 ID, N->getOpcode(), BinNode->Flags.hasNoUnsignedWrap(),
520 BinNode->Flags.hasNoSignedWrap(), BinNode->Flags.hasExact());
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());
544 case ISD::PREFETCH: {
545 const MemSDNode *PF = cast<MemSDNode>(N);
546 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
549 case ISD::VECTOR_SHUFFLE: {
550 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
551 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
553 ID.AddInteger(SVN->getMaskElt(i));
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());
564 } // end switch (N->getOpcode())
566 // Target specific memory nodes could also have address spaces to check.
567 if (N->isTargetMemoryOpcode())
568 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
571 /// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
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());
580 // Handle SDNode leafs with special info.
581 AddNodeIDCustom(ID, N);
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.
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!");
598 (isNonTemporal << 6) |
602 //===----------------------------------------------------------------------===//
603 // SelectionDAG Class
604 //===----------------------------------------------------------------------===//
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.
611 switch (N->getOpcode()) {
613 case ISD::HANDLENODE:
615 return true; // Never CSE these nodes.
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.
626 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
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());
633 SmallVector<SDNode*, 128> DeadNodes;
635 // Add all obviously-dead nodes to the DeadNodes worklist.
636 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
638 DeadNodes.push_back(I);
640 RemoveDeadNodes(DeadNodes);
642 // If the root changed (e.g. it was a dead load, update the root).
643 setRoot(Dummy.getValue());
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) {
650 // Process the worklist, deleting the nodes and adding their uses to the
652 while (!DeadNodes.empty()) {
653 SDNode *N = DeadNodes.pop_back_val();
655 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
656 DUL->NodeDeleted(N, nullptr);
658 // Take the node out of the appropriate CSE map.
659 RemoveNodeFromCSEMaps(N);
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; ) {
665 SDNode *Operand = Use.getNode();
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);
677 void SelectionDAG::RemoveDeadNode(SDNode *N){
678 SmallVector<SDNode*, 16> DeadNodes(1, N);
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
683 HandleSDNode Dummy(getRoot());
685 RemoveDeadNodes(DeadNodes);
688 void SelectionDAG::DeleteNode(SDNode *N) {
689 // First take this out of the appropriate CSE map.
690 RemoveNodeFromCSEMaps(N);
692 // Finally, remove uses due to operands of this node, remove from the
693 // AllNodes list, and delete the node.
694 DeleteNodeNotInCSEMaps(N);
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!");
701 // Drop all of the operands and decrement used node's use counts.
707 void SDDbgInfo::erase(const SDNode *Node) {
708 DbgValMapType::iterator I = DbgValMap.find(Node);
709 if (I == DbgValMap.end())
711 for (auto &Val: I->second)
712 Val->setIsInvalidated();
716 void SelectionDAG::DeallocateNode(SDNode *N) {
717 if (N->OperandsNeedDelete)
718 delete[] N->OperandList;
720 // Set the opcode to DELETED_NODE to help catch bugs when node
721 // memory is reallocated.
722 N->NodeType = ISD::DELETED_NODE;
724 NodeAllocator.Deallocate(AllNodes.remove(N));
726 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
727 // them and forget about that node.
732 /// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
733 static void VerifySDNode(SDNode *N) {
734 switch (N->getOpcode()) {
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");
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");
771 /// \brief Insert a newly allocated node into the DAG.
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);
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) {
788 switch (N->getOpcode()) {
789 case ISD::HANDLENODE: return false; // noop.
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;
796 case ISD::ExternalSymbol:
797 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
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()));
806 case ISD::VALUETYPE: {
807 EVT VT = cast<VTSDNode>(N)->getVT();
808 if (VT.isExtended()) {
809 Erased = ExtendedValueTypeNodes.erase(VT);
811 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
812 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
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);
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)) {
831 llvm_unreachable("Node is not in map!");
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.
843 SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
844 // For node types that aren't CSE'd, just act as if no identical node
847 SDNode *Existing = CSEMap.GetOrInsertNode(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);
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);
862 // If the node doesn't already exist, we updated it. Inform listeners.
863 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
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,
876 SDValue Ops[] = { Op };
878 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
879 AddNodeIDCustom(ID, N);
880 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
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,
894 SDValue Ops[] = { Op1, Op2 };
896 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
897 AddNodeIDCustom(ID, N);
898 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
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,
913 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
914 AddNodeIDCustom(ID, N);
915 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
919 /// getEVTAlignment - Compute the default alignment value for the
922 unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
923 Type *Ty = VT == MVT::iPTR ?
924 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
925 VT.getTypeForEVT(*getContext());
927 return TLI->getDataLayout()->getABITypeAlignment(Ty);
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();
940 void SelectionDAG::init(MachineFunction &mf) {
942 TLI = getSubtarget().getTargetLowering();
943 TSI = getSubtarget().getSelectionDAGInfo();
944 Context = &mf.getFunction()->getContext();
947 SelectionDAG::~SelectionDAG() {
948 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
953 void SelectionDAG::allnodes_clear() {
954 assert(&*AllNodes.begin() == &EntryNode);
955 AllNodes.remove(AllNodes.begin());
956 while (!AllNodes.empty())
957 DeallocateNode(AllNodes.begin());
960 BinarySDNode *SelectionDAG::GetBinarySDNode(unsigned Opcode, SDLoc DL,
961 SDVTList VTs, SDValue N1,
962 SDValue N2, bool nuw, bool nsw,
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);
974 BinarySDNode *N = new (NodeAllocator)
975 BinarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
979 void SelectionDAG::clear() {
981 OperandAllocator.Reset();
984 ExtendedValueTypeNodes.clear();
985 ExternalSymbols.clear();
986 TargetExternalSymbols.clear();
987 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
988 static_cast<CondCodeSDNode*>(nullptr));
989 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
990 static_cast<SDNode*>(nullptr));
992 EntryNode.UseList = nullptr;
993 AllNodes.push_back(&EntryNode);
994 Root = getEntryNode();
998 SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
999 return VT.bitsGT(Op.getValueType()) ?
1000 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1001 getNode(ISD::TRUNCATE, DL, VT, Op);
1004 SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
1005 return VT.bitsGT(Op.getValueType()) ?
1006 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1007 getNode(ISD::TRUNCATE, DL, VT, Op);
1010 SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
1011 return VT.bitsGT(Op.getValueType()) ?
1012 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1013 getNode(ISD::TRUNCATE, DL, VT, Op);
1016 SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, SDLoc SL, EVT VT,
1018 if (VT.bitsLE(Op.getValueType()))
1019 return getNode(ISD::TRUNCATE, SL, VT, Op);
1021 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
1022 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1025 SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
1026 assert(!VT.isVector() &&
1027 "getZeroExtendInReg should use the vector element type instead of "
1028 "the vector type!");
1029 if (Op.getValueType() == VT) return Op;
1030 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
1031 APInt Imm = APInt::getLowBitsSet(BitWidth,
1032 VT.getSizeInBits());
1033 return getNode(ISD::AND, DL, Op.getValueType(), Op,
1034 getConstant(Imm, DL, Op.getValueType()));
1037 SDValue SelectionDAG::getAnyExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1038 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1039 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1040 "The sizes of the input and result must match in order to perform the "
1041 "extend in-register.");
1042 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1043 "The destination vector type must have fewer lanes than the input.");
1044 return getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Op);
1047 SDValue SelectionDAG::getSignExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1048 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1049 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1050 "The sizes of the input and result must match in order to perform the "
1051 "extend in-register.");
1052 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1053 "The destination vector type must have fewer lanes than the input.");
1054 return getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, VT, Op);
1057 SDValue SelectionDAG::getZeroExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1058 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1059 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1060 "The sizes of the input and result must match in order to perform the "
1061 "extend in-register.");
1062 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1063 "The destination vector type must have fewer lanes than the input.");
1064 return getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, VT, Op);
1067 /// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1069 SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
1070 EVT EltVT = VT.getScalarType();
1072 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL, VT);
1073 return getNode(ISD::XOR, DL, VT, Val, NegOne);
1076 SDValue SelectionDAG::getLogicalNOT(SDLoc DL, SDValue Val, EVT VT) {
1077 EVT EltVT = VT.getScalarType();
1079 switch (TLI->getBooleanContents(VT)) {
1080 case TargetLowering::ZeroOrOneBooleanContent:
1081 case TargetLowering::UndefinedBooleanContent:
1082 TrueValue = getConstant(1, DL, VT);
1084 case TargetLowering::ZeroOrNegativeOneBooleanContent:
1085 TrueValue = getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL,
1089 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1092 SDValue SelectionDAG::getConstant(uint64_t Val, SDLoc DL, EVT VT, bool isT,
1094 EVT EltVT = VT.getScalarType();
1095 assert((EltVT.getSizeInBits() >= 64 ||
1096 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1097 "getConstant with a uint64_t value that doesn't fit in the type!");
1098 return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO);
1101 SDValue SelectionDAG::getConstant(const APInt &Val, SDLoc DL, EVT VT, bool isT,
1104 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1107 SDValue SelectionDAG::getConstant(const ConstantInt &Val, SDLoc DL, EVT VT,
1108 bool isT, bool isO) {
1109 assert(VT.isInteger() && "Cannot create FP integer constant!");
1111 EVT EltVT = VT.getScalarType();
1112 const ConstantInt *Elt = &Val;
1114 // In some cases the vector type is legal but the element type is illegal and
1115 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1116 // inserted value (the type does not need to match the vector element type).
1117 // Any extra bits introduced will be truncated away.
1118 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1119 TargetLowering::TypePromoteInteger) {
1120 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1121 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1122 Elt = ConstantInt::get(*getContext(), NewVal);
1124 // In other cases the element type is illegal and needs to be expanded, for
1125 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1126 // the value into n parts and use a vector type with n-times the elements.
1127 // Then bitcast to the type requested.
1128 // Legalizing constants too early makes the DAGCombiner's job harder so we
1129 // only legalize if the DAG tells us we must produce legal types.
1130 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1131 TLI->getTypeAction(*getContext(), EltVT) ==
1132 TargetLowering::TypeExpandInteger) {
1133 APInt NewVal = Elt->getValue();
1134 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1135 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1136 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1137 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1139 // Check the temporary vector is the correct size. If this fails then
1140 // getTypeToTransformTo() probably returned a type whose size (in bits)
1141 // isn't a power-of-2 factor of the requested type size.
1142 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1144 SmallVector<SDValue, 2> EltParts;
1145 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1146 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1147 .trunc(ViaEltSizeInBits), DL,
1148 ViaEltVT, isT, isO));
1151 // EltParts is currently in little endian order. If we actually want
1152 // big-endian order then reverse it now.
1153 if (TLI->isBigEndian())
1154 std::reverse(EltParts.begin(), EltParts.end());
1156 // The elements must be reversed when the element order is different
1157 // to the endianness of the elements (because the BITCAST is itself a
1158 // vector shuffle in this situation). However, we do not need any code to
1159 // perform this reversal because getConstant() is producing a vector
1161 // This situation occurs in MIPS MSA.
1163 SmallVector<SDValue, 8> Ops;
1164 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1165 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1167 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1168 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
1173 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1174 "APInt size does not match type size!");
1175 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1176 FoldingSetNodeID ID;
1177 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
1181 SDNode *N = nullptr;
1182 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
1184 return SDValue(N, 0);
1187 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, DL.getDebugLoc(),
1189 CSEMap.InsertNode(N, IP);
1193 SDValue Result(N, 0);
1194 if (VT.isVector()) {
1195 SmallVector<SDValue, 8> Ops;
1196 Ops.assign(VT.getVectorNumElements(), Result);
1197 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
1202 SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, SDLoc DL, bool isTarget) {
1203 return getConstant(Val, DL, TLI->getPointerTy(), isTarget);
1206 SDValue SelectionDAG::getConstantFP(const APFloat& V, SDLoc DL, EVT VT,
1208 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1211 SDValue SelectionDAG::getConstantFP(const ConstantFP& V, SDLoc DL, EVT VT,
1213 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1215 EVT EltVT = VT.getScalarType();
1217 // Do the map lookup using the actual bit pattern for the floating point
1218 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1219 // we don't have issues with SNANs.
1220 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1221 FoldingSetNodeID ID;
1222 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
1225 SDNode *N = nullptr;
1226 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
1228 return SDValue(N, 0);
1231 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
1232 CSEMap.InsertNode(N, IP);
1236 SDValue Result(N, 0);
1237 if (VT.isVector()) {
1238 SmallVector<SDValue, 8> Ops;
1239 Ops.assign(VT.getVectorNumElements(), Result);
1240 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
1245 SDValue SelectionDAG::getConstantFP(double Val, SDLoc DL, EVT VT,
1247 EVT EltVT = VT.getScalarType();
1248 if (EltVT==MVT::f32)
1249 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1250 else if (EltVT==MVT::f64)
1251 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1252 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1255 APFloat apf = APFloat(Val);
1256 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
1258 return getConstantFP(apf, DL, VT, isTarget);
1260 llvm_unreachable("Unsupported type in getConstantFP");
1263 SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
1264 EVT VT, int64_t Offset,
1266 unsigned char TargetFlags) {
1267 assert((TargetFlags == 0 || isTargetGA) &&
1268 "Cannot set target flags on target-independent globals");
1270 // Truncate (with sign-extension) the offset value to the pointer size.
1271 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
1273 Offset = SignExtend64(Offset, BitWidth);
1276 if (GV->isThreadLocal())
1277 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1279 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
1281 FoldingSetNodeID ID;
1282 AddNodeIDNode(ID, Opc, getVTList(VT), None);
1284 ID.AddInteger(Offset);
1285 ID.AddInteger(TargetFlags);
1286 ID.AddInteger(GV->getType()->getAddressSpace());
1288 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1289 return SDValue(E, 0);
1291 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1292 DL.getDebugLoc(), GV, VT,
1293 Offset, TargetFlags);
1294 CSEMap.InsertNode(N, IP);
1296 return SDValue(N, 0);
1299 SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1300 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1301 FoldingSetNodeID ID;
1302 AddNodeIDNode(ID, Opc, getVTList(VT), None);
1305 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1306 return SDValue(E, 0);
1308 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
1309 CSEMap.InsertNode(N, IP);
1311 return SDValue(N, 0);
1314 SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1315 unsigned char TargetFlags) {
1316 assert((TargetFlags == 0 || isTarget) &&
1317 "Cannot set target flags on target-independent jump tables");
1318 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1319 FoldingSetNodeID ID;
1320 AddNodeIDNode(ID, Opc, getVTList(VT), None);
1322 ID.AddInteger(TargetFlags);
1324 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1325 return SDValue(E, 0);
1327 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1329 CSEMap.InsertNode(N, IP);
1331 return SDValue(N, 0);
1334 SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
1335 unsigned Alignment, int Offset,
1337 unsigned char TargetFlags) {
1338 assert((TargetFlags == 0 || isTarget) &&
1339 "Cannot set target flags on target-independent globals");
1341 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType());
1342 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1343 FoldingSetNodeID ID;
1344 AddNodeIDNode(ID, Opc, getVTList(VT), None);
1345 ID.AddInteger(Alignment);
1346 ID.AddInteger(Offset);
1348 ID.AddInteger(TargetFlags);
1350 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1351 return SDValue(E, 0);
1353 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1354 Alignment, TargetFlags);
1355 CSEMap.InsertNode(N, IP);
1357 return SDValue(N, 0);
1361 SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
1362 unsigned Alignment, int Offset,
1364 unsigned char TargetFlags) {
1365 assert((TargetFlags == 0 || isTarget) &&
1366 "Cannot set target flags on target-independent globals");
1368 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType());
1369 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1370 FoldingSetNodeID ID;
1371 AddNodeIDNode(ID, Opc, getVTList(VT), None);
1372 ID.AddInteger(Alignment);
1373 ID.AddInteger(Offset);
1374 C->addSelectionDAGCSEId(ID);
1375 ID.AddInteger(TargetFlags);
1377 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1378 return SDValue(E, 0);
1380 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1381 Alignment, TargetFlags);
1382 CSEMap.InsertNode(N, IP);
1384 return SDValue(N, 0);
1387 SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1388 unsigned char TargetFlags) {
1389 FoldingSetNodeID ID;
1390 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
1391 ID.AddInteger(Index);
1392 ID.AddInteger(Offset);
1393 ID.AddInteger(TargetFlags);
1395 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1396 return SDValue(E, 0);
1398 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1400 CSEMap.InsertNode(N, IP);
1402 return SDValue(N, 0);
1405 SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
1406 FoldingSetNodeID ID;
1407 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
1410 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1411 return SDValue(E, 0);
1413 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
1414 CSEMap.InsertNode(N, IP);
1416 return SDValue(N, 0);
1419 SDValue SelectionDAG::getValueType(EVT VT) {
1420 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1421 ValueTypeNodes.size())
1422 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
1424 SDNode *&N = VT.isExtended() ?
1425 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
1427 if (N) return SDValue(N, 0);
1428 N = new (NodeAllocator) VTSDNode(VT);
1430 return SDValue(N, 0);
1433 SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
1434 SDNode *&N = ExternalSymbols[Sym];
1435 if (N) return SDValue(N, 0);
1436 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
1438 return SDValue(N, 0);
1441 SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
1442 unsigned char TargetFlags) {
1444 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1446 if (N) return SDValue(N, 0);
1447 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
1449 return SDValue(N, 0);
1452 SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
1453 if ((unsigned)Cond >= CondCodeNodes.size())
1454 CondCodeNodes.resize(Cond+1);
1456 if (!CondCodeNodes[Cond]) {
1457 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
1458 CondCodeNodes[Cond] = N;
1462 return SDValue(CondCodeNodes[Cond], 0);
1465 // commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1466 // the shuffle mask M that point at N1 to point at N2, and indices that point
1467 // N2 to point at N1.
1468 static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1470 ShuffleVectorSDNode::commuteMask(M);
1473 SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
1474 SDValue N2, const int *Mask) {
1475 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1476 "Invalid VECTOR_SHUFFLE");
1478 // Canonicalize shuffle undef, undef -> undef
1479 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
1480 return getUNDEF(VT);
1482 // Validate that all indices in Mask are within the range of the elements
1483 // input to the shuffle.
1484 unsigned NElts = VT.getVectorNumElements();
1485 SmallVector<int, 8> MaskVec;
1486 for (unsigned i = 0; i != NElts; ++i) {
1487 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
1488 MaskVec.push_back(Mask[i]);
1491 // Canonicalize shuffle v, v -> v, undef
1494 for (unsigned i = 0; i != NElts; ++i)
1495 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
1498 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1499 if (N1.getOpcode() == ISD::UNDEF)
1500 commuteShuffle(N1, N2, MaskVec);
1502 // If shuffling a splat, try to blend the splat instead. We do this here so
1503 // that even when this arises during lowering we don't have to re-handle it.
1504 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
1505 BitVector UndefElements;
1506 SDValue Splat = BV->getSplatValue(&UndefElements);
1510 for (int i = 0; i < (int)NElts; ++i) {
1511 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + (int)NElts))
1514 // If this input comes from undef, mark it as such.
1515 if (UndefElements[MaskVec[i] - Offset]) {
1520 // If we can blend a non-undef lane, use that instead.
1521 if (!UndefElements[i])
1522 MaskVec[i] = i + Offset;
1525 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
1526 BlendSplat(N1BV, 0);
1527 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
1528 BlendSplat(N2BV, NElts);
1530 // Canonicalize all index into lhs, -> shuffle lhs, undef
1531 // Canonicalize all index into rhs, -> shuffle rhs, undef
1532 bool AllLHS = true, AllRHS = true;
1533 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
1534 for (unsigned i = 0; i != NElts; ++i) {
1535 if (MaskVec[i] >= (int)NElts) {
1540 } else if (MaskVec[i] >= 0) {
1544 if (AllLHS && AllRHS)
1545 return getUNDEF(VT);
1546 if (AllLHS && !N2Undef)
1550 commuteShuffle(N1, N2, MaskVec);
1552 // Reset our undef status after accounting for the mask.
1553 N2Undef = N2.getOpcode() == ISD::UNDEF;
1554 // Re-check whether both sides ended up undef.
1555 if (N1.getOpcode() == ISD::UNDEF && N2Undef)
1556 return getUNDEF(VT);
1558 // If Identity shuffle return that node.
1559 bool Identity = true, AllSame = true;
1560 for (unsigned i = 0; i != NElts; ++i) {
1561 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
1562 if (MaskVec[i] != MaskVec[0]) AllSame = false;
1564 if (Identity && NElts)
1567 // Shuffling a constant splat doesn't change the result.
1571 // Look through any bitcasts. We check that these don't change the number
1572 // (and size) of elements and just changes their types.
1573 while (V.getOpcode() == ISD::BITCAST)
1574 V = V->getOperand(0);
1576 // A splat should always show up as a build vector node.
1577 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
1578 BitVector UndefElements;
1579 SDValue Splat = BV->getSplatValue(&UndefElements);
1580 // If this is a splat of an undef, shuffling it is also undef.
1581 if (Splat && Splat.getOpcode() == ISD::UNDEF)
1582 return getUNDEF(VT);
1585 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
1587 // We only have a splat which can skip shuffles if there is a splatted
1588 // value and no undef lanes rearranged by the shuffle.
1589 if (Splat && UndefElements.none()) {
1590 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
1591 // number of elements match or the value splatted is a zero constant.
1594 if (auto *C = dyn_cast<ConstantSDNode>(Splat))
1595 if (C->isNullValue())
1599 // If the shuffle itself creates a splat, build the vector directly.
1600 if (AllSame && SameNumElts) {
1601 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
1602 SmallVector<SDValue, 8> Ops(NElts, Splatted);
1604 EVT BuildVT = BV->getValueType(0);
1605 SDValue NewBV = getNode(ISD::BUILD_VECTOR, dl, BuildVT, Ops);
1607 // We may have jumped through bitcasts, so the type of the
1608 // BUILD_VECTOR may not match the type of the shuffle.
1610 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
1616 FoldingSetNodeID ID;
1617 SDValue Ops[2] = { N1, N2 };
1618 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
1619 for (unsigned i = 0; i != NElts; ++i)
1620 ID.AddInteger(MaskVec[i]);
1623 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1624 return SDValue(E, 0);
1626 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1627 // SDNode doesn't have access to it. This memory will be "leaked" when
1628 // the node is deallocated, but recovered when the NodeAllocator is released.
1629 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1630 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
1632 ShuffleVectorSDNode *N =
1633 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1634 dl.getDebugLoc(), N1, N2,
1636 CSEMap.InsertNode(N, IP);
1638 return SDValue(N, 0);
1641 SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) {
1642 MVT VT = SV.getSimpleValueType(0);
1643 SmallVector<int, 8> MaskVec(SV.getMask().begin(), SV.getMask().end());
1644 ShuffleVectorSDNode::commuteMask(MaskVec);
1646 SDValue Op0 = SV.getOperand(0);
1647 SDValue Op1 = SV.getOperand(1);
1648 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, &MaskVec[0]);
1651 SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
1652 SDValue Val, SDValue DTy,
1653 SDValue STy, SDValue Rnd, SDValue Sat,
1654 ISD::CvtCode Code) {
1655 // If the src and dest types are the same and the conversion is between
1656 // integer types of the same sign or two floats, no conversion is necessary.
1658 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
1661 FoldingSetNodeID ID;
1662 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1663 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
1665 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1666 return SDValue(E, 0);
1668 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1671 CSEMap.InsertNode(N, IP);
1673 return SDValue(N, 0);
1676 SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
1677 FoldingSetNodeID ID;
1678 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
1679 ID.AddInteger(RegNo);
1681 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1682 return SDValue(E, 0);
1684 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
1685 CSEMap.InsertNode(N, IP);
1687 return SDValue(N, 0);
1690 SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1691 FoldingSetNodeID ID;
1692 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
1693 ID.AddPointer(RegMask);
1695 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1696 return SDValue(E, 0);
1698 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1699 CSEMap.InsertNode(N, IP);
1701 return SDValue(N, 0);
1704 SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
1705 FoldingSetNodeID ID;
1706 SDValue Ops[] = { Root };
1707 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
1708 ID.AddPointer(Label);
1710 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1711 return SDValue(E, 0);
1713 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1714 dl.getDebugLoc(), Root, Label);
1715 CSEMap.InsertNode(N, IP);
1717 return SDValue(N, 0);
1721 SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
1724 unsigned char TargetFlags) {
1725 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1727 FoldingSetNodeID ID;
1728 AddNodeIDNode(ID, Opc, getVTList(VT), None);
1730 ID.AddInteger(Offset);
1731 ID.AddInteger(TargetFlags);
1733 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1734 return SDValue(E, 0);
1736 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1738 CSEMap.InsertNode(N, IP);
1740 return SDValue(N, 0);
1743 SDValue SelectionDAG::getSrcValue(const Value *V) {
1744 assert((!V || V->getType()->isPointerTy()) &&
1745 "SrcValue is not a pointer?");
1747 FoldingSetNodeID ID;
1748 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
1752 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1753 return SDValue(E, 0);
1755 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
1756 CSEMap.InsertNode(N, IP);
1758 return SDValue(N, 0);
1761 /// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1762 SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1763 FoldingSetNodeID ID;
1764 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
1768 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1769 return SDValue(E, 0);
1771 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1772 CSEMap.InsertNode(N, IP);
1774 return SDValue(N, 0);
1777 /// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1778 SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1779 unsigned SrcAS, unsigned DestAS) {
1780 SDValue Ops[] = {Ptr};
1781 FoldingSetNodeID ID;
1782 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
1783 ID.AddInteger(SrcAS);
1784 ID.AddInteger(DestAS);
1787 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1788 return SDValue(E, 0);
1790 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1792 VT, Ptr, SrcAS, DestAS);
1793 CSEMap.InsertNode(N, IP);
1795 return SDValue(N, 0);
1798 /// getShiftAmountOperand - Return the specified value casted to
1799 /// the target's desired shift amount type.
1800 SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
1801 EVT OpTy = Op.getValueType();
1802 EVT ShTy = TLI->getShiftAmountTy(LHSTy);
1803 if (OpTy == ShTy || OpTy.isVector()) return Op;
1805 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1806 return getNode(Opcode, SDLoc(Op), ShTy, Op);
1809 /// CreateStackTemporary - Create a stack temporary, suitable for holding the
1810 /// specified value type.
1811 SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
1812 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1813 unsigned ByteSize = VT.getStoreSize();
1814 Type *Ty = VT.getTypeForEVT(*getContext());
1815 unsigned StackAlign =
1816 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
1818 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
1819 return getFrameIndex(FrameIdx, TLI->getPointerTy());
1822 /// CreateStackTemporary - Create a stack temporary suitable for holding
1823 /// either of the specified value types.
1824 SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
1825 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1826 VT2.getStoreSizeInBits())/8;
1827 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1828 Type *Ty2 = VT2.getTypeForEVT(*getContext());
1829 const DataLayout *TD = TLI->getDataLayout();
1830 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1831 TD->getPrefTypeAlignment(Ty2));
1833 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1834 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
1835 return getFrameIndex(FrameIdx, TLI->getPointerTy());
1838 SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
1839 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
1840 // These setcc operations always fold.
1844 case ISD::SETFALSE2: return getConstant(0, dl, VT);
1846 case ISD::SETTRUE2: {
1847 TargetLowering::BooleanContent Cnt =
1848 TLI->getBooleanContents(N1->getValueType(0));
1850 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, dl,
1864 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
1868 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
1869 const APInt &C2 = N2C->getAPIntValue();
1870 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
1871 const APInt &C1 = N1C->getAPIntValue();
1874 default: llvm_unreachable("Unknown integer setcc!");
1875 case ISD::SETEQ: return getConstant(C1 == C2, dl, VT);
1876 case ISD::SETNE: return getConstant(C1 != C2, dl, VT);
1877 case ISD::SETULT: return getConstant(C1.ult(C2), dl, VT);
1878 case ISD::SETUGT: return getConstant(C1.ugt(C2), dl, VT);
1879 case ISD::SETULE: return getConstant(C1.ule(C2), dl, VT);
1880 case ISD::SETUGE: return getConstant(C1.uge(C2), dl, VT);
1881 case ISD::SETLT: return getConstant(C1.slt(C2), dl, VT);
1882 case ISD::SETGT: return getConstant(C1.sgt(C2), dl, VT);
1883 case ISD::SETLE: return getConstant(C1.sle(C2), dl, VT);
1884 case ISD::SETGE: return getConstant(C1.sge(C2), dl, VT);
1888 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1889 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
1890 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
1893 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1894 return getUNDEF(VT);
1896 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, dl, VT);
1897 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1898 return getUNDEF(VT);
1900 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
1901 R==APFloat::cmpLessThan, dl, VT);
1902 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1903 return getUNDEF(VT);
1905 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, dl, VT);
1906 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1907 return getUNDEF(VT);
1909 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, dl, VT);
1910 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1911 return getUNDEF(VT);
1913 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
1914 R==APFloat::cmpEqual, dl, VT);
1915 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1916 return getUNDEF(VT);
1918 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
1919 R==APFloat::cmpEqual, dl, VT);
1920 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, dl, VT);
1921 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, dl, VT);
1922 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1923 R==APFloat::cmpEqual, dl, VT);
1924 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, dl, VT);
1925 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1926 R==APFloat::cmpLessThan, dl, VT);
1927 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1928 R==APFloat::cmpUnordered, dl, VT);
1929 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, dl, VT);
1930 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, dl, VT);
1933 // Ensure that the constant occurs on the RHS.
1934 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1935 MVT CompVT = N1.getValueType().getSimpleVT();
1936 if (!TLI->isCondCodeLegal(SwappedCond, CompVT))
1939 return getSetCC(dl, VT, N2, N1, SwappedCond);
1943 // Could not fold it.
1947 /// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1948 /// use this predicate to simplify operations downstream.
1949 bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
1950 // This predicate is not safe for vector operations.
1951 if (Op.getValueType().isVector())
1954 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
1955 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1958 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1959 /// this predicate to simplify operations downstream. Mask is known to be zero
1960 /// for bits that V cannot have.
1961 bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
1962 unsigned Depth) const {
1963 APInt KnownZero, KnownOne;
1964 computeKnownBits(Op, KnownZero, KnownOne, Depth);
1965 return (KnownZero & Mask) == Mask;
1968 /// Determine which bits of Op are known to be either zero or one and return
1969 /// them in the KnownZero/KnownOne bitsets.
1970 void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
1971 APInt &KnownOne, unsigned Depth) const {
1972 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
1974 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
1976 return; // Limit search depth.
1978 APInt KnownZero2, KnownOne2;
1980 switch (Op.getOpcode()) {
1982 // We know all of the bits for a constant!
1983 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1984 KnownZero = ~KnownOne;
1987 // If either the LHS or the RHS are Zero, the result is zero.
1988 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1989 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
1991 // Output known-1 bits are only known if set in both the LHS & RHS.
1992 KnownOne &= KnownOne2;
1993 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1994 KnownZero |= KnownZero2;
1997 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1998 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
2000 // Output known-0 bits are only known if clear in both the LHS & RHS.
2001 KnownZero &= KnownZero2;
2002 // Output known-1 are known to be set if set in either the LHS | RHS.
2003 KnownOne |= KnownOne2;
2006 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2007 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
2009 // Output known-0 bits are known if clear or set in both the LHS & RHS.
2010 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
2011 // Output known-1 are known to be set if set in only one of the LHS, RHS.
2012 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
2013 KnownZero = KnownZeroOut;
2017 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2018 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
2020 // If low bits are zero in either operand, output low known-0 bits.
2021 // Also compute a conserative estimate for high known-0 bits.
2022 // More trickiness is possible, but this is sufficient for the
2023 // interesting case of alignment computation.
2024 KnownOne.clearAllBits();
2025 unsigned TrailZ = KnownZero.countTrailingOnes() +
2026 KnownZero2.countTrailingOnes();
2027 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
2028 KnownZero2.countLeadingOnes(),
2029 BitWidth) - BitWidth;
2031 TrailZ = std::min(TrailZ, BitWidth);
2032 LeadZ = std::min(LeadZ, BitWidth);
2033 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
2034 APInt::getHighBitsSet(BitWidth, LeadZ);
2038 // For the purposes of computing leading zeros we can conservatively
2039 // treat a udiv as a logical right shift by the power of 2 known to
2040 // be less than the denominator.
2041 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
2042 unsigned LeadZ = KnownZero2.countLeadingOnes();
2044 KnownOne2.clearAllBits();
2045 KnownZero2.clearAllBits();
2046 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
2047 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
2048 if (RHSUnknownLeadingOnes != BitWidth)
2049 LeadZ = std::min(BitWidth,
2050 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
2052 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
2056 computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
2057 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
2059 // Only known if known in both the LHS and RHS.
2060 KnownOne &= KnownOne2;
2061 KnownZero &= KnownZero2;
2063 case ISD::SELECT_CC:
2064 computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
2065 computeKnownBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
2067 // Only known if known in both the LHS and RHS.
2068 KnownOne &= KnownOne2;
2069 KnownZero &= KnownZero2;
2077 if (Op.getResNo() != 1)
2079 // The boolean result conforms to getBooleanContents.
2080 // If we know the result of a setcc has the top bits zero, use this info.
2081 // We know that we have an integer-based boolean since these operations
2082 // are only available for integer.
2083 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2084 TargetLowering::ZeroOrOneBooleanContent &&
2086 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2089 // If we know the result of a setcc has the top bits zero, use this info.
2090 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2091 TargetLowering::ZeroOrOneBooleanContent &&
2093 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2096 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
2097 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2098 unsigned ShAmt = SA->getZExtValue();
2100 // If the shift count is an invalid immediate, don't do anything.
2101 if (ShAmt >= BitWidth)
2104 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2105 KnownZero <<= ShAmt;
2107 // low bits known zero.
2108 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
2112 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
2113 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2114 unsigned ShAmt = SA->getZExtValue();
2116 // If the shift count is an invalid immediate, don't do anything.
2117 if (ShAmt >= BitWidth)
2120 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2121 KnownZero = KnownZero.lshr(ShAmt);
2122 KnownOne = KnownOne.lshr(ShAmt);
2124 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
2125 KnownZero |= HighBits; // High bits known zero.
2129 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2130 unsigned ShAmt = SA->getZExtValue();
2132 // If the shift count is an invalid immediate, don't do anything.
2133 if (ShAmt >= BitWidth)
2136 // If any of the demanded bits are produced by the sign extension, we also
2137 // demand the input sign bit.
2138 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
2140 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2141 KnownZero = KnownZero.lshr(ShAmt);
2142 KnownOne = KnownOne.lshr(ShAmt);
2144 // Handle the sign bits.
2145 APInt SignBit = APInt::getSignBit(BitWidth);
2146 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
2148 if (KnownZero.intersects(SignBit)) {
2149 KnownZero |= HighBits; // New bits are known zero.
2150 } else if (KnownOne.intersects(SignBit)) {
2151 KnownOne |= HighBits; // New bits are known one.
2155 case ISD::SIGN_EXTEND_INREG: {
2156 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
2157 unsigned EBits = EVT.getScalarType().getSizeInBits();
2159 // Sign extension. Compute the demanded bits in the result that are not
2160 // present in the input.
2161 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
2163 APInt InSignBit = APInt::getSignBit(EBits);
2164 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
2166 // If the sign extended bits are demanded, we know that the sign
2168 InSignBit = InSignBit.zext(BitWidth);
2169 if (NewBits.getBoolValue())
2170 InputDemandedBits |= InSignBit;
2172 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2173 KnownOne &= InputDemandedBits;
2174 KnownZero &= InputDemandedBits;
2176 // If the sign bit of the input is known set or clear, then we know the
2177 // top bits of the result.
2178 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
2179 KnownZero |= NewBits;
2180 KnownOne &= ~NewBits;
2181 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
2182 KnownOne |= NewBits;
2183 KnownZero &= ~NewBits;
2184 } else { // Input sign bit unknown
2185 KnownZero &= ~NewBits;
2186 KnownOne &= ~NewBits;
2191 case ISD::CTTZ_ZERO_UNDEF:
2193 case ISD::CTLZ_ZERO_UNDEF:
2195 unsigned LowBits = Log2_32(BitWidth)+1;
2196 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
2197 KnownOne.clearAllBits();
2201 LoadSDNode *LD = cast<LoadSDNode>(Op);
2202 // If this is a ZEXTLoad and we are looking at the loaded value.
2203 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
2204 EVT VT = LD->getMemoryVT();
2205 unsigned MemBits = VT.getScalarType().getSizeInBits();
2206 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
2207 } else if (const MDNode *Ranges = LD->getRanges()) {
2208 computeKnownBitsFromRangeMetadata(*Ranges, KnownZero);
2212 case ISD::ZERO_EXTEND: {
2213 EVT InVT = Op.getOperand(0).getValueType();
2214 unsigned InBits = InVT.getScalarType().getSizeInBits();
2215 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
2216 KnownZero = KnownZero.trunc(InBits);
2217 KnownOne = KnownOne.trunc(InBits);
2218 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2219 KnownZero = KnownZero.zext(BitWidth);
2220 KnownOne = KnownOne.zext(BitWidth);
2221 KnownZero |= NewBits;
2224 case ISD::SIGN_EXTEND: {
2225 EVT InVT = Op.getOperand(0).getValueType();
2226 unsigned InBits = InVT.getScalarType().getSizeInBits();
2227 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
2229 KnownZero = KnownZero.trunc(InBits);
2230 KnownOne = KnownOne.trunc(InBits);
2231 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2233 // Note if the sign bit is known to be zero or one.
2234 bool SignBitKnownZero = KnownZero.isNegative();
2235 bool SignBitKnownOne = KnownOne.isNegative();
2237 KnownZero = KnownZero.zext(BitWidth);
2238 KnownOne = KnownOne.zext(BitWidth);
2240 // If the sign bit is known zero or one, the top bits match.
2241 if (SignBitKnownZero)
2242 KnownZero |= NewBits;
2243 else if (SignBitKnownOne)
2244 KnownOne |= NewBits;
2247 case ISD::ANY_EXTEND: {
2248 EVT InVT = Op.getOperand(0).getValueType();
2249 unsigned InBits = InVT.getScalarType().getSizeInBits();
2250 KnownZero = KnownZero.trunc(InBits);
2251 KnownOne = KnownOne.trunc(InBits);
2252 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2253 KnownZero = KnownZero.zext(BitWidth);
2254 KnownOne = KnownOne.zext(BitWidth);
2257 case ISD::TRUNCATE: {
2258 EVT InVT = Op.getOperand(0).getValueType();
2259 unsigned InBits = InVT.getScalarType().getSizeInBits();
2260 KnownZero = KnownZero.zext(InBits);
2261 KnownOne = KnownOne.zext(InBits);
2262 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2263 KnownZero = KnownZero.trunc(BitWidth);
2264 KnownOne = KnownOne.trunc(BitWidth);
2267 case ISD::AssertZext: {
2268 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
2269 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
2270 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2271 KnownZero |= (~InMask);
2272 KnownOne &= (~KnownZero);
2276 // All bits are zero except the low bit.
2277 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2281 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2282 // We know that the top bits of C-X are clear if X contains less bits
2283 // than C (i.e. no wrap-around can happen). For example, 20-X is
2284 // positive if we can prove that X is >= 0 and < 16.
2285 if (CLHS->getAPIntValue().isNonNegative()) {
2286 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2287 // NLZ can't be BitWidth with no sign bit
2288 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
2289 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
2291 // If all of the MaskV bits are known to be zero, then we know the
2292 // output top bits are zero, because we now know that the output is
2294 if ((KnownZero2 & MaskV) == MaskV) {
2295 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2296 // Top bits known zero.
2297 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
2305 // Output known-0 bits are known if clear or set in both the low clear bits
2306 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2307 // low 3 bits clear.
2308 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
2309 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2311 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
2312 KnownZeroOut = std::min(KnownZeroOut,
2313 KnownZero2.countTrailingOnes());
2315 if (Op.getOpcode() == ISD::ADD) {
2316 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
2320 // With ADDE, a carry bit may be added in, so we can only use this
2321 // information if we know (at least) that the low two bits are clear. We
2322 // then return to the caller that the low bit is unknown but that other bits
2324 if (KnownZeroOut >= 2) // ADDE
2325 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
2329 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2330 const APInt &RA = Rem->getAPIntValue().abs();
2331 if (RA.isPowerOf2()) {
2332 APInt LowBits = RA - 1;
2333 computeKnownBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
2335 // The low bits of the first operand are unchanged by the srem.
2336 KnownZero = KnownZero2 & LowBits;
2337 KnownOne = KnownOne2 & LowBits;
2339 // If the first operand is non-negative or has all low bits zero, then
2340 // the upper bits are all zero.
2341 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2342 KnownZero |= ~LowBits;
2344 // If the first operand is negative and not all low bits are zero, then
2345 // the upper bits are all one.
2346 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2347 KnownOne |= ~LowBits;
2348 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2353 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2354 const APInt &RA = Rem->getAPIntValue();
2355 if (RA.isPowerOf2()) {
2356 APInt LowBits = (RA - 1);
2357 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth + 1);
2359 // The upper bits are all zero, the lower ones are unchanged.
2360 KnownZero = KnownZero2 | ~LowBits;
2361 KnownOne = KnownOne2 & LowBits;
2366 // Since the result is less than or equal to either operand, any leading
2367 // zero bits in either operand must also exist in the result.
2368 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2369 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
2371 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2372 KnownZero2.countLeadingOnes());
2373 KnownOne.clearAllBits();
2374 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
2377 case ISD::EXTRACT_ELEMENT: {
2378 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2379 const unsigned Index =
2380 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2381 const unsigned BitWidth = Op.getValueType().getSizeInBits();
2383 // Remove low part of known bits mask
2384 KnownZero = KnownZero.getHiBits(KnownZero.getBitWidth() - Index * BitWidth);
2385 KnownOne = KnownOne.getHiBits(KnownOne.getBitWidth() - Index * BitWidth);
2387 // Remove high part of known bit mask
2388 KnownZero = KnownZero.trunc(BitWidth);
2389 KnownOne = KnownOne.trunc(BitWidth);
2392 case ISD::FrameIndex:
2393 case ISD::TargetFrameIndex:
2394 if (unsigned Align = InferPtrAlignment(Op)) {
2395 // The low bits are known zero if the pointer is aligned.
2396 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2402 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2405 case ISD::INTRINSIC_WO_CHAIN:
2406 case ISD::INTRINSIC_W_CHAIN:
2407 case ISD::INTRINSIC_VOID:
2408 // Allow the target to implement this method for its nodes.
2409 TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
2413 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
2416 /// ComputeNumSignBits - Return the number of times the sign bit of the
2417 /// register is replicated into the other bits. We know that at least 1 bit
2418 /// is always equal to the sign bit (itself), but other cases can give us
2419 /// information. For example, immediately after an "SRA X, 2", we know that
2420 /// the top 3 bits are all equal to each other, so we return 3.
2421 unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
2422 EVT VT = Op.getValueType();
2423 assert(VT.isInteger() && "Invalid VT!");
2424 unsigned VTBits = VT.getScalarType().getSizeInBits();
2426 unsigned FirstAnswer = 1;
2429 return 1; // Limit search depth.
2431 switch (Op.getOpcode()) {
2433 case ISD::AssertSext:
2434 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
2435 return VTBits-Tmp+1;
2436 case ISD::AssertZext:
2437 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
2440 case ISD::Constant: {
2441 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
2442 return Val.getNumSignBits();
2445 case ISD::SIGN_EXTEND:
2447 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
2448 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
2450 case ISD::SIGN_EXTEND_INREG:
2451 // Max of the input and what this extends.
2453 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
2456 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2457 return std::max(Tmp, Tmp2);
2460 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2461 // SRA X, C -> adds C sign bits.
2462 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2463 Tmp += C->getZExtValue();
2464 if (Tmp > VTBits) Tmp = VTBits;
2468 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2469 // shl destroys sign bits.
2470 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2471 if (C->getZExtValue() >= VTBits || // Bad shift.
2472 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2473 return Tmp - C->getZExtValue();
2478 case ISD::XOR: // NOT is handled here.
2479 // Logical binary ops preserve the number of sign bits at the worst.
2480 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2482 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2483 FirstAnswer = std::min(Tmp, Tmp2);
2484 // We computed what we know about the sign bits as our first
2485 // answer. Now proceed to the generic code that uses
2486 // computeKnownBits, and pick whichever answer is better.
2491 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2492 if (Tmp == 1) return 1; // Early out.
2493 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
2494 return std::min(Tmp, Tmp2);
2502 if (Op.getResNo() != 1)
2504 // The boolean result conforms to getBooleanContents. Fall through.
2505 // If setcc returns 0/-1, all bits are sign bits.
2506 // We know that we have an integer-based boolean since these operations
2507 // are only available for integer.
2508 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2509 TargetLowering::ZeroOrNegativeOneBooleanContent)
2513 // If setcc returns 0/-1, all bits are sign bits.
2514 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2515 TargetLowering::ZeroOrNegativeOneBooleanContent)
2520 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2521 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
2523 // Handle rotate right by N like a rotate left by 32-N.
2524 if (Op.getOpcode() == ISD::ROTR)
2525 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2527 // If we aren't rotating out all of the known-in sign bits, return the
2528 // number that are left. This handles rotl(sext(x), 1) for example.
2529 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2530 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2534 // Add can have at most one carry bit. Thus we know that the output
2535 // is, at worst, one more bit than the inputs.
2536 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2537 if (Tmp == 1) return 1; // Early out.
2539 // Special case decrementing a value (ADD X, -1):
2540 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2541 if (CRHS->isAllOnesValue()) {
2542 APInt KnownZero, KnownOne;
2543 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2545 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2547 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
2550 // If we are subtracting one from a positive number, there is no carry
2551 // out of the result.
2552 if (KnownZero.isNegative())
2556 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2557 if (Tmp2 == 1) return 1;
2558 return std::min(Tmp, Tmp2)-1;
2561 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2562 if (Tmp2 == 1) return 1;
2565 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
2566 if (CLHS->isNullValue()) {
2567 APInt KnownZero, KnownOne;
2568 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2569 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2571 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
2574 // If the input is known to be positive (the sign bit is known clear),
2575 // the output of the NEG has the same number of sign bits as the input.
2576 if (KnownZero.isNegative())
2579 // Otherwise, we treat this like a SUB.
2582 // Sub can have at most one carry bit. Thus we know that the output
2583 // is, at worst, one more bit than the inputs.
2584 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2585 if (Tmp == 1) return 1; // Early out.
2586 return std::min(Tmp, Tmp2)-1;
2588 // FIXME: it's tricky to do anything useful for this, but it is an important
2589 // case for targets like X86.
2591 case ISD::EXTRACT_ELEMENT: {
2592 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2593 const int BitWidth = Op.getValueType().getSizeInBits();
2595 Op.getOperand(0).getValueType().getSizeInBits() / BitWidth;
2597 // Get reverse index (starting from 1), Op1 value indexes elements from
2598 // little end. Sign starts at big end.
2599 const int rIndex = Items - 1 -
2600 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2602 // If the sign portion ends in our element the substraction gives correct
2603 // result. Otherwise it gives either negative or > bitwidth result
2604 return std::max(std::min(KnownSign - rIndex * BitWidth, BitWidth), 0);
2608 // If we are looking at the loaded value of the SDNode.
2609 if (Op.getResNo() == 0) {
2610 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2611 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2612 unsigned ExtType = LD->getExtensionType();
2615 case ISD::SEXTLOAD: // '17' bits known
2616 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2617 return VTBits-Tmp+1;
2618 case ISD::ZEXTLOAD: // '16' bits known
2619 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2625 // Allow the target to implement this method for its nodes.
2626 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
2627 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
2628 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2629 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2630 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
2631 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
2634 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2635 // use this information.
2636 APInt KnownZero, KnownOne;
2637 computeKnownBits(Op, KnownZero, KnownOne, Depth);
2640 if (KnownZero.isNegative()) { // sign bit is 0
2642 } else if (KnownOne.isNegative()) { // sign bit is 1;
2649 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2650 // the number of identical bits in the top of the input value.
2652 Mask <<= Mask.getBitWidth()-VTBits;
2653 // Return # leading zeros. We use 'min' here in case Val was zero before
2654 // shifting. We don't want to return '64' as for an i32 "0".
2655 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
2658 /// isBaseWithConstantOffset - Return true if the specified operand is an
2659 /// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2660 /// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2661 /// semantics as an ADD. This handles the equivalence:
2662 /// X|Cst == X+Cst iff X&Cst = 0.
2663 bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2664 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2665 !isa<ConstantSDNode>(Op.getOperand(1)))
2668 if (Op.getOpcode() == ISD::OR &&
2669 !MaskedValueIsZero(Op.getOperand(0),
2670 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2677 bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2678 // If we're told that NaNs won't happen, assume they won't.
2679 if (getTarget().Options.NoNaNsFPMath)
2682 // If the value is a constant, we can obviously see if it is a NaN or not.
2683 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2684 return !C->getValueAPF().isNaN();
2686 // TODO: Recognize more cases here.
2691 bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2692 // If the value is a constant, we can obviously see if it is a zero or not.
2693 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2694 return !C->isZero();
2696 // TODO: Recognize more cases here.
2697 switch (Op.getOpcode()) {
2700 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2701 return !C->isNullValue();
2708 bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2709 // Check the obvious case.
2710 if (A == B) return true;
2712 // For for negative and positive zero.
2713 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2714 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2715 if (CA->isZero() && CB->isZero()) return true;
2717 // Otherwise they may not be equal.
2721 /// getNode - Gets or creates the specified node.
2723 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
2724 FoldingSetNodeID ID;
2725 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
2727 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2728 return SDValue(E, 0);
2730 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2731 DL.getDebugLoc(), getVTList(VT));
2732 CSEMap.InsertNode(N, IP);
2735 return SDValue(N, 0);
2738 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
2739 EVT VT, SDValue Operand) {
2740 // Constant fold unary operations with an integer constant operand. Even
2741 // opaque constant will be folded, because the folding of unary operations
2742 // doesn't create new constants with different values. Nevertheless, the
2743 // opaque flag is preserved during folding to prevent future folding with
2745 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
2746 const APInt &Val = C->getAPIntValue();
2749 case ISD::SIGN_EXTEND:
2750 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
2751 C->isTargetOpcode(), C->isOpaque());
2752 case ISD::ANY_EXTEND:
2753 case ISD::ZERO_EXTEND:
2755 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
2756 C->isTargetOpcode(), C->isOpaque());
2757 case ISD::UINT_TO_FP:
2758 case ISD::SINT_TO_FP: {
2759 APFloat apf(EVTToAPFloatSemantics(VT),
2760 APInt::getNullValue(VT.getSizeInBits()));
2761 (void)apf.convertFromAPInt(Val,
2762 Opcode==ISD::SINT_TO_FP,
2763 APFloat::rmNearestTiesToEven);
2764 return getConstantFP(apf, DL, VT);
2767 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
2768 return getConstantFP(APFloat(APFloat::IEEEhalf, Val), DL, VT);
2769 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
2770 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), DL, VT);
2771 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
2772 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), DL, VT);
2775 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
2778 return getConstant(Val.countPopulation(), DL, VT, C->isTargetOpcode(),
2781 case ISD::CTLZ_ZERO_UNDEF:
2782 return getConstant(Val.countLeadingZeros(), DL, VT, C->isTargetOpcode(),
2785 case ISD::CTTZ_ZERO_UNDEF:
2786 return getConstant(Val.countTrailingZeros(), DL, VT, C->isTargetOpcode(),
2791 // Constant fold unary operations with a floating point constant operand.
2792 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
2793 APFloat V = C->getValueAPF(); // make copy
2797 return getConstantFP(V, DL, VT);
2800 return getConstantFP(V, DL, VT);
2802 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2803 if (fs == APFloat::opOK || fs == APFloat::opInexact)
2804 return getConstantFP(V, DL, VT);
2808 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2809 if (fs == APFloat::opOK || fs == APFloat::opInexact)
2810 return getConstantFP(V, DL, VT);
2814 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2815 if (fs == APFloat::opOK || fs == APFloat::opInexact)
2816 return getConstantFP(V, DL, VT);
2819 case ISD::FP_EXTEND: {
2821 // This can return overflow, underflow, or inexact; we don't care.
2822 // FIXME need to be more flexible about rounding mode.
2823 (void)V.convert(EVTToAPFloatSemantics(VT),
2824 APFloat::rmNearestTiesToEven, &ignored);
2825 return getConstantFP(V, DL, VT);
2827 case ISD::FP_TO_SINT:
2828 case ISD::FP_TO_UINT: {
2831 static_assert(integerPartWidth >= 64, "APFloat parts too small!");
2832 // FIXME need to be more flexible about rounding mode.
2833 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2834 Opcode==ISD::FP_TO_SINT,
2835 APFloat::rmTowardZero, &ignored);
2836 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2838 APInt api(VT.getSizeInBits(), x);
2839 return getConstant(api, DL, VT);
2842 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
2843 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
2844 else if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2845 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
2846 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2847 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
2852 // Constant fold unary operations with a vector integer or float operand.
2853 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Operand.getNode())) {
2854 if (BV->isConstant()) {
2857 // FIXME: Entirely reasonable to perform folding of other unary
2858 // operations here as the need arises.
2865 case ISD::FP_EXTEND:
2866 case ISD::FP_TO_SINT:
2867 case ISD::FP_TO_UINT:
2869 case ISD::UINT_TO_FP:
2870 case ISD::SINT_TO_FP: {
2871 EVT SVT = VT.getScalarType();
2872 EVT InVT = BV->getValueType(0);
2873 EVT InSVT = InVT.getScalarType();
2875 // Find legal integer scalar type for constant promotion.
2877 if (SVT.isInteger()) {
2878 LegalSVT = TLI->getTypeToTransformTo(*getContext(), SVT);
2879 assert(LegalSVT.bitsGE(SVT) && "Unexpected legal scalar type size");
2882 // Let the above scalar folding handle the folding of each element.
2883 SmallVector<SDValue, 8> Ops;
2884 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
2885 SDValue OpN = BV->getOperand(i);
2886 EVT OpVT = OpN.getValueType();
2888 // Build vector (integer) scalar operands may need implicit
2889 // truncation - do this before constant folding.
2890 if (OpVT.isInteger() && OpVT.bitsGT(InSVT))
2891 OpN = getNode(ISD::TRUNCATE, DL, InSVT, OpN);
2893 OpN = getNode(Opcode, DL, SVT, OpN);
2895 // Legalize the (integer) scalar constant if necessary.
2896 if (LegalSVT != SVT)
2897 OpN = getNode(ISD::ANY_EXTEND, DL, LegalSVT, OpN);
2899 if (OpN.getOpcode() != ISD::UNDEF &&
2900 OpN.getOpcode() != ISD::Constant &&
2901 OpN.getOpcode() != ISD::ConstantFP)
2905 if (Ops.size() == VT.getVectorNumElements())
2906 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
2913 unsigned OpOpcode = Operand.getNode()->getOpcode();
2915 case ISD::TokenFactor:
2916 case ISD::MERGE_VALUES:
2917 case ISD::CONCAT_VECTORS:
2918 return Operand; // Factor, merge or concat of one node? No need.
2919 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
2920 case ISD::FP_EXTEND:
2921 assert(VT.isFloatingPoint() &&
2922 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
2923 if (Operand.getValueType() == VT) return Operand; // noop conversion.
2924 assert((!VT.isVector() ||
2925 VT.getVectorNumElements() ==
2926 Operand.getValueType().getVectorNumElements()) &&
2927 "Vector element count mismatch!");
2928 if (Operand.getOpcode() == ISD::UNDEF)
2929 return getUNDEF(VT);
2931 case ISD::SIGN_EXTEND:
2932 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
2933 "Invalid SIGN_EXTEND!");
2934 if (Operand.getValueType() == VT) return Operand; // noop extension
2935 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2936 "Invalid sext node, dst < src!");
2937 assert((!VT.isVector() ||
2938 VT.getVectorNumElements() ==
2939 Operand.getValueType().getVectorNumElements()) &&
2940 "Vector element count mismatch!");
2941 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2942 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2943 else if (OpOpcode == ISD::UNDEF)
2944 // sext(undef) = 0, because the top bits will all be the same.
2945 return getConstant(0, DL, VT);
2947 case ISD::ZERO_EXTEND:
2948 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
2949 "Invalid ZERO_EXTEND!");
2950 if (Operand.getValueType() == VT) return Operand; // noop extension
2951 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2952 "Invalid zext node, dst < src!");
2953 assert((!VT.isVector() ||
2954 VT.getVectorNumElements() ==
2955 Operand.getValueType().getVectorNumElements()) &&
2956 "Vector element count mismatch!");
2957 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
2958 return getNode(ISD::ZERO_EXTEND, DL, VT,
2959 Operand.getNode()->getOperand(0));
2960 else if (OpOpcode == ISD::UNDEF)
2961 // zext(undef) = 0, because the top bits will be zero.
2962 return getConstant(0, DL, VT);
2964 case ISD::ANY_EXTEND:
2965 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
2966 "Invalid ANY_EXTEND!");
2967 if (Operand.getValueType() == VT) return Operand; // noop extension
2968 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2969 "Invalid anyext node, dst < src!");
2970 assert((!VT.isVector() ||
2971 VT.getVectorNumElements() ==
2972 Operand.getValueType().getVectorNumElements()) &&
2973 "Vector element count mismatch!");
2975 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2976 OpOpcode == ISD::ANY_EXTEND)
2977 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2978 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2979 else if (OpOpcode == ISD::UNDEF)
2980 return getUNDEF(VT);
2982 // (ext (trunx x)) -> x
2983 if (OpOpcode == ISD::TRUNCATE) {
2984 SDValue OpOp = Operand.getNode()->getOperand(0);
2985 if (OpOp.getValueType() == VT)
2990 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
2991 "Invalid TRUNCATE!");
2992 if (Operand.getValueType() == VT) return Operand; // noop truncate
2993 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2994 "Invalid truncate node, src < dst!");
2995 assert((!VT.isVector() ||
2996 VT.getVectorNumElements() ==
2997 Operand.getValueType().getVectorNumElements()) &&
2998 "Vector element count mismatch!");
2999 if (OpOpcode == ISD::TRUNCATE)
3000 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
3001 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
3002 OpOpcode == ISD::ANY_EXTEND) {
3003 // If the source is smaller than the dest, we still need an extend.
3004 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
3005 .bitsLT(VT.getScalarType()))
3006 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
3007 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
3008 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
3009 return Operand.getNode()->getOperand(0);
3011 if (OpOpcode == ISD::UNDEF)
3012 return getUNDEF(VT);
3015 // Basic sanity checking.
3016 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
3017 && "Cannot BITCAST between types of different sizes!");
3018 if (VT == Operand.getValueType()) return Operand; // noop conversion.
3019 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
3020 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
3021 if (OpOpcode == ISD::UNDEF)
3022 return getUNDEF(VT);
3024 case ISD::SCALAR_TO_VECTOR:
3025 assert(VT.isVector() && !Operand.getValueType().isVector() &&
3026 (VT.getVectorElementType() == Operand.getValueType() ||
3027 (VT.getVectorElementType().isInteger() &&
3028 Operand.getValueType().isInteger() &&
3029 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
3030 "Illegal SCALAR_TO_VECTOR node!");
3031 if (OpOpcode == ISD::UNDEF)
3032 return getUNDEF(VT);
3033 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
3034 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
3035 isa<ConstantSDNode>(Operand.getOperand(1)) &&
3036 Operand.getConstantOperandVal(1) == 0 &&
3037 Operand.getOperand(0).getValueType() == VT)
3038 return Operand.getOperand(0);
3041 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
3042 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
3043 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
3044 Operand.getNode()->getOperand(0));
3045 if (OpOpcode == ISD::FNEG) // --X -> X
3046 return Operand.getNode()->getOperand(0);
3049 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
3050 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
3055 SDVTList VTs = getVTList(VT);
3056 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
3057 FoldingSetNodeID ID;
3058 SDValue Ops[1] = { Operand };
3059 AddNodeIDNode(ID, Opcode, VTs, Ops);
3061 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3062 return SDValue(E, 0);
3064 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
3065 DL.getDebugLoc(), VTs, Operand);
3066 CSEMap.InsertNode(N, IP);
3068 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
3069 DL.getDebugLoc(), VTs, Operand);
3073 return SDValue(N, 0);
3076 SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, SDLoc DL, EVT VT,
3077 SDNode *Cst1, SDNode *Cst2) {
3078 // If the opcode is a target-specific ISD node, there's nothing we can
3079 // do here and the operand rules may not line up with the below, so
3081 if (Opcode >= ISD::BUILTIN_OP_END)
3084 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
3085 SmallVector<SDValue, 4> Outputs;
3086 EVT SVT = VT.getScalarType();
3088 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
3089 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
3090 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque()))
3093 if (Scalar1 && Scalar2)
3094 // Scalar instruction.
3095 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
3097 // For vectors extract each constant element into Inputs so we can constant
3098 // fold them individually.
3099 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
3100 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
3104 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
3106 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
3107 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
3108 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
3109 if (!V1 || !V2) // Not a constant, bail.
3112 if (V1->isOpaque() || V2->isOpaque())
3115 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
3116 // FIXME: This is valid and could be handled by truncating the APInts.
3117 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
3120 Inputs.push_back(std::make_pair(V1, V2));
3124 // We have a number of constant values, constant fold them element by element.
3125 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
3126 const APInt &C1 = Inputs[I].first->getAPIntValue();
3127 const APInt &C2 = Inputs[I].second->getAPIntValue();
3131 Outputs.push_back(getConstant(C1 + C2, DL, SVT));
3134 Outputs.push_back(getConstant(C1 - C2, DL, SVT));
3137 Outputs.push_back(getConstant(C1 * C2, DL, SVT));
3140 if (!C2.getBoolValue())
3142 Outputs.push_back(getConstant(C1.udiv(C2), DL, SVT));
3145 if (!C2.getBoolValue())
3147 Outputs.push_back(getConstant(C1.urem(C2), DL, SVT));
3150 if (!C2.getBoolValue())
3152 Outputs.push_back(getConstant(C1.sdiv(C2), DL, SVT));
3155 if (!C2.getBoolValue())
3157 Outputs.push_back(getConstant(C1.srem(C2), DL, SVT));
3160 Outputs.push_back(getConstant(C1 & C2, DL, SVT));
3163 Outputs.push_back(getConstant(C1 | C2, DL, SVT));
3166 Outputs.push_back(getConstant(C1 ^ C2, DL, SVT));
3169 Outputs.push_back(getConstant(C1 << C2, DL, SVT));
3172 Outputs.push_back(getConstant(C1.lshr(C2), DL, SVT));
3175 Outputs.push_back(getConstant(C1.ashr(C2), DL, SVT));
3178 Outputs.push_back(getConstant(C1.rotl(C2), DL, SVT));
3181 Outputs.push_back(getConstant(C1.rotr(C2), DL, SVT));
3188 assert((Scalar1 && Scalar2) || (VT.getVectorNumElements() == Outputs.size() &&
3189 "Expected a scalar or vector!"));
3191 // Handle the scalar case first.
3193 return Outputs.back();
3195 // We may have a vector type but a scalar result. Create a splat.
3196 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
3198 // Build a big vector out of the scalar elements we generated.
3199 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
3202 SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
3203 SDValue N2, bool nuw, bool nsw, bool exact) {
3204 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
3205 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
3208 case ISD::TokenFactor:
3209 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
3210 N2.getValueType() == MVT::Other && "Invalid token factor!");
3211 // Fold trivial token factors.
3212 if (N1.getOpcode() == ISD::EntryToken) return N2;
3213 if (N2.getOpcode() == ISD::EntryToken) return N1;
3214 if (N1 == N2) return N1;
3216 case ISD::CONCAT_VECTORS:
3217 // Concat of UNDEFs is UNDEF.
3218 if (N1.getOpcode() == ISD::UNDEF &&
3219 N2.getOpcode() == ISD::UNDEF)
3220 return getUNDEF(VT);
3222 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3223 // one big BUILD_VECTOR.
3224 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3225 N2.getOpcode() == ISD::BUILD_VECTOR) {
3226 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3227 N1.getNode()->op_end());
3228 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3230 // BUILD_VECTOR requires all inputs to be of the same type, find the
3231 // maximum type and extend them all.
3232 EVT SVT = VT.getScalarType();
3233 for (SDValue Op : Elts)
3234 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
3235 if (SVT.bitsGT(VT.getScalarType()))
3236 for (SDValue &Op : Elts)
3237 Op = TLI->isZExtFree(Op.getValueType(), SVT)
3238 ? getZExtOrTrunc(Op, DL, SVT)
3239 : getSExtOrTrunc(Op, DL, SVT);
3241 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
3245 assert(VT.isInteger() && "This operator does not apply to FP types!");
3246 assert(N1.getValueType() == N2.getValueType() &&
3247 N1.getValueType() == VT && "Binary operator types must match!");
3248 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
3249 // worth handling here.
3250 if (N2C && N2C->isNullValue())
3252 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
3259 assert(VT.isInteger() && "This operator does not apply to FP types!");
3260 assert(N1.getValueType() == N2.getValueType() &&
3261 N1.getValueType() == VT && "Binary operator types must match!");
3262 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
3263 // it's worth handling here.
3264 if (N2C && N2C->isNullValue())
3274 assert(VT.isInteger() && "This operator does not apply to FP types!");
3275 assert(N1.getValueType() == N2.getValueType() &&
3276 N1.getValueType() == VT && "Binary operator types must match!");
3283 if (getTarget().Options.UnsafeFPMath) {
3284 if (Opcode == ISD::FADD) {
3286 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3287 if (CFP->getValueAPF().isZero())
3290 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3291 if (CFP->getValueAPF().isZero())
3293 } else if (Opcode == ISD::FSUB) {
3295 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3296 if (CFP->getValueAPF().isZero())
3298 } else if (Opcode == ISD::FMUL) {
3299 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3302 // If the first operand isn't the constant, try the second
3304 CFP = dyn_cast<ConstantFPSDNode>(N2);
3311 return SDValue(CFP,0);
3313 if (CFP->isExactlyValue(1.0))
3318 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
3319 assert(N1.getValueType() == N2.getValueType() &&
3320 N1.getValueType() == VT && "Binary operator types must match!");
3322 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3323 assert(N1.getValueType() == VT &&
3324 N1.getValueType().isFloatingPoint() &&
3325 N2.getValueType().isFloatingPoint() &&
3326 "Invalid FCOPYSIGN!");
3333 assert(VT == N1.getValueType() &&
3334 "Shift operators return type must be the same as their first arg");
3335 assert(VT.isInteger() && N2.getValueType().isInteger() &&
3336 "Shifts only work on integers");
3337 assert((!VT.isVector() || VT == N2.getValueType()) &&
3338 "Vector shift amounts must be in the same as their first arg");
3339 // Verify that the shift amount VT is bit enough to hold valid shift
3340 // amounts. This catches things like trying to shift an i1024 value by an
3341 // i8, which is easy to fall into in generic code that uses
3342 // TLI.getShiftAmount().
3343 assert(N2.getValueType().getSizeInBits() >=
3344 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
3345 "Invalid use of small shift amount with oversized value!");
3347 // Always fold shifts of i1 values so the code generator doesn't need to
3348 // handle them. Since we know the size of the shift has to be less than the
3349 // size of the value, the shift/rotate count is guaranteed to be zero.
3352 if (N2C && N2C->isNullValue())
3355 case ISD::FP_ROUND_INREG: {
3356 EVT EVT = cast<VTSDNode>(N2)->getVT();
3357 assert(VT == N1.getValueType() && "Not an inreg round!");
3358 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
3359 "Cannot FP_ROUND_INREG integer types");
3360 assert(EVT.isVector() == VT.isVector() &&
3361 "FP_ROUND_INREG type should be vector iff the operand "
3363 assert((!EVT.isVector() ||
3364 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3365 "Vector element counts must match in FP_ROUND_INREG");
3366 assert(EVT.bitsLE(VT) && "Not rounding down!");