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 "llvm/Constants.h"
16 #include "llvm/GlobalAlias.h"
17 #include "llvm/GlobalVariable.h"
18 #include "llvm/Intrinsics.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Assembly/Writer.h"
21 #include "llvm/CodeGen/MachineBasicBlock.h"
22 #include "llvm/CodeGen/MachineConstantPool.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineModuleInfo.h"
25 #include "llvm/CodeGen/PseudoSourceValue.h"
26 #include "llvm/Support/MathExtras.h"
27 #include "llvm/Target/TargetRegisterInfo.h"
28 #include "llvm/Target/TargetData.h"
29 #include "llvm/Target/TargetLowering.h"
30 #include "llvm/Target/TargetInstrInfo.h"
31 #include "llvm/Target/TargetMachine.h"
32 #include "llvm/ADT/SetVector.h"
33 #include "llvm/ADT/SmallPtrSet.h"
34 #include "llvm/ADT/SmallSet.h"
35 #include "llvm/ADT/SmallVector.h"
36 #include "llvm/ADT/StringExtras.h"
41 /// makeVTList - Return an instance of the SDVTList struct initialized with the
42 /// specified members.
43 static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
44 SDVTList Res = {VTs, NumVTs};
48 static const fltSemantics *MVTToAPFloatSemantics(MVT::ValueType VT) {
50 default: assert(0 && "Unknown FP format");
51 case MVT::f32: return &APFloat::IEEEsingle;
52 case MVT::f64: return &APFloat::IEEEdouble;
53 case MVT::f80: return &APFloat::x87DoubleExtended;
54 case MVT::f128: return &APFloat::IEEEquad;
55 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
59 SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
61 //===----------------------------------------------------------------------===//
62 // ConstantFPSDNode Class
63 //===----------------------------------------------------------------------===//
65 /// isExactlyValue - We don't rely on operator== working on double values, as
66 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
67 /// As such, this method can be used to do an exact bit-for-bit comparison of
68 /// two floating point values.
69 bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
70 return Value.bitwiseIsEqual(V);
73 bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
75 assert(MVT::isFloatingPoint(VT) && "Can only convert between FP types");
77 // Anything can be extended to ppc long double.
78 if (VT == MVT::ppcf128)
81 // PPC long double cannot be shrunk to anything though.
82 if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
85 // convert modifies in place, so make a copy.
86 APFloat Val2 = APFloat(Val);
87 return Val2.convert(*MVTToAPFloatSemantics(VT),
88 APFloat::rmNearestTiesToEven) == APFloat::opOK;
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 if (N->getOpcode() == ISD::BIT_CONVERT)
100 N = N->getOperand(0).Val;
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
115 SDOperand NotZero = N->getOperand(i);
116 if (isa<ConstantSDNode>(NotZero)) {
117 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
119 } else if (isa<ConstantFPSDNode>(NotZero)) {
120 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
121 convertToAPInt().isAllOnesValue())
126 // Okay, we have at least one ~0 value, check to see if the rest match or are
128 for (++i; i != e; ++i)
129 if (N->getOperand(i) != NotZero &&
130 N->getOperand(i).getOpcode() != ISD::UNDEF)
136 /// isBuildVectorAllZeros - Return true if the specified node is a
137 /// BUILD_VECTOR where all of the elements are 0 or undef.
138 bool ISD::isBuildVectorAllZeros(const SDNode *N) {
139 // Look through a bit convert.
140 if (N->getOpcode() == ISD::BIT_CONVERT)
141 N = N->getOperand(0).Val;
143 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
145 unsigned i = 0, e = N->getNumOperands();
147 // Skip over all of the undef values.
148 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
151 // Do not accept an all-undef vector.
152 if (i == e) return false;
154 // Do not accept build_vectors that aren't all constants or which have non-~0
156 SDOperand Zero = N->getOperand(i);
157 if (isa<ConstantSDNode>(Zero)) {
158 if (!cast<ConstantSDNode>(Zero)->isNullValue())
160 } else if (isa<ConstantFPSDNode>(Zero)) {
161 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
166 // Okay, we have at least one ~0 value, check to see if the rest match or are
168 for (++i; i != e; ++i)
169 if (N->getOperand(i) != Zero &&
170 N->getOperand(i).getOpcode() != ISD::UNDEF)
175 /// isScalarToVector - Return true if the specified node is a
176 /// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
177 /// element is not an undef.
178 bool ISD::isScalarToVector(const SDNode *N) {
179 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
182 if (N->getOpcode() != ISD::BUILD_VECTOR)
184 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
186 unsigned NumElems = N->getNumOperands();
187 for (unsigned i = 1; i < NumElems; ++i) {
188 SDOperand V = N->getOperand(i);
189 if (V.getOpcode() != ISD::UNDEF)
196 /// isDebugLabel - Return true if the specified node represents a debug
197 /// label (i.e. ISD::LABEL or TargetInstrInfo::LABEL node and third operand
199 bool ISD::isDebugLabel(const SDNode *N) {
201 if (N->getOpcode() == ISD::LABEL)
202 Zero = N->getOperand(2);
203 else if (N->isTargetOpcode() &&
204 N->getTargetOpcode() == TargetInstrInfo::LABEL)
205 // Chain moved to last operand.
206 Zero = N->getOperand(1);
209 return isa<ConstantSDNode>(Zero) && cast<ConstantSDNode>(Zero)->isNullValue();
212 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
213 /// when given the operation for (X op Y).
214 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
215 // To perform this operation, we just need to swap the L and G bits of the
217 unsigned OldL = (Operation >> 2) & 1;
218 unsigned OldG = (Operation >> 1) & 1;
219 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
220 (OldL << 1) | // New G bit
221 (OldG << 2)); // New L bit.
224 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
225 /// 'op' is a valid SetCC operation.
226 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
227 unsigned Operation = Op;
229 Operation ^= 7; // Flip L, G, E bits, but not U.
231 Operation ^= 15; // Flip all of the condition bits.
232 if (Operation > ISD::SETTRUE2)
233 Operation &= ~8; // Don't let N and U bits get set.
234 return ISD::CondCode(Operation);
238 /// isSignedOp - For an integer comparison, return 1 if the comparison is a
239 /// signed operation and 2 if the result is an unsigned comparison. Return zero
240 /// if the operation does not depend on the sign of the input (setne and seteq).
241 static int isSignedOp(ISD::CondCode Opcode) {
243 default: assert(0 && "Illegal integer setcc operation!");
245 case ISD::SETNE: return 0;
249 case ISD::SETGE: return 1;
253 case ISD::SETUGE: return 2;
257 /// getSetCCOrOperation - Return the result of a logical OR between different
258 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
259 /// returns SETCC_INVALID if it is not possible to represent the resultant
261 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
263 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
264 // Cannot fold a signed integer setcc with an unsigned integer setcc.
265 return ISD::SETCC_INVALID;
267 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
269 // If the N and U bits get set then the resultant comparison DOES suddenly
270 // care about orderedness, and is true when ordered.
271 if (Op > ISD::SETTRUE2)
272 Op &= ~16; // Clear the U bit if the N bit is set.
274 // Canonicalize illegal integer setcc's.
275 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
278 return ISD::CondCode(Op);
281 /// getSetCCAndOperation - Return the result of a logical AND between different
282 /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
283 /// function returns zero if it is not possible to represent the resultant
285 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
287 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
288 // Cannot fold a signed setcc with an unsigned setcc.
289 return ISD::SETCC_INVALID;
291 // Combine all of the condition bits.
292 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
294 // Canonicalize illegal integer setcc's.
298 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
299 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
300 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
301 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
308 const TargetMachine &SelectionDAG::getTarget() const {
309 return TLI.getTargetMachine();
312 //===----------------------------------------------------------------------===//
313 // SDNode Profile Support
314 //===----------------------------------------------------------------------===//
316 /// AddNodeIDOpcode - Add the node opcode to the NodeID data.
318 static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
322 /// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
323 /// solely with their pointer.
324 void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
325 ID.AddPointer(VTList.VTs);
328 /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
330 static void AddNodeIDOperands(FoldingSetNodeID &ID,
331 const SDOperand *Ops, unsigned NumOps) {
332 for (; NumOps; --NumOps, ++Ops) {
333 ID.AddPointer(Ops->Val);
334 ID.AddInteger(Ops->ResNo);
338 static void AddNodeIDNode(FoldingSetNodeID &ID,
339 unsigned short OpC, SDVTList VTList,
340 const SDOperand *OpList, unsigned N) {
341 AddNodeIDOpcode(ID, OpC);
342 AddNodeIDValueTypes(ID, VTList);
343 AddNodeIDOperands(ID, OpList, N);
346 /// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
348 static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
349 AddNodeIDOpcode(ID, N->getOpcode());
350 // Add the return value info.
351 AddNodeIDValueTypes(ID, N->getVTList());
352 // Add the operand info.
353 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
355 // Handle SDNode leafs with special info.
356 switch (N->getOpcode()) {
357 default: break; // Normal nodes don't need extra info.
358 case ISD::TargetConstant:
360 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
362 case ISD::TargetConstantFP:
363 case ISD::ConstantFP: {
364 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
367 case ISD::TargetGlobalAddress:
368 case ISD::GlobalAddress:
369 case ISD::TargetGlobalTLSAddress:
370 case ISD::GlobalTLSAddress: {
371 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
372 ID.AddPointer(GA->getGlobal());
373 ID.AddInteger(GA->getOffset());
376 case ISD::BasicBlock:
377 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
380 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
383 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
385 case ISD::MEMOPERAND: {
386 const MemOperand &MO = cast<MemOperandSDNode>(N)->MO;
387 ID.AddPointer(MO.getValue());
388 ID.AddInteger(MO.getFlags());
389 ID.AddInteger(MO.getOffset());
390 ID.AddInteger(MO.getSize());
391 ID.AddInteger(MO.getAlignment());
394 case ISD::FrameIndex:
395 case ISD::TargetFrameIndex:
396 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
399 case ISD::TargetJumpTable:
400 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
402 case ISD::ConstantPool:
403 case ISD::TargetConstantPool: {
404 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
405 ID.AddInteger(CP->getAlignment());
406 ID.AddInteger(CP->getOffset());
407 if (CP->isMachineConstantPoolEntry())
408 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
410 ID.AddPointer(CP->getConstVal());
414 LoadSDNode *LD = cast<LoadSDNode>(N);
415 ID.AddInteger(LD->getAddressingMode());
416 ID.AddInteger(LD->getExtensionType());
417 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
418 ID.AddInteger(LD->getAlignment());
419 ID.AddInteger(LD->isVolatile());
423 StoreSDNode *ST = cast<StoreSDNode>(N);
424 ID.AddInteger(ST->getAddressingMode());
425 ID.AddInteger(ST->isTruncatingStore());
426 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
427 ID.AddInteger(ST->getAlignment());
428 ID.AddInteger(ST->isVolatile());
434 //===----------------------------------------------------------------------===//
435 // SelectionDAG Class
436 //===----------------------------------------------------------------------===//
438 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
440 void SelectionDAG::RemoveDeadNodes() {
441 // Create a dummy node (which is not added to allnodes), that adds a reference
442 // to the root node, preventing it from being deleted.
443 HandleSDNode Dummy(getRoot());
445 SmallVector<SDNode*, 128> DeadNodes;
447 // Add all obviously-dead nodes to the DeadNodes worklist.
448 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
450 DeadNodes.push_back(I);
452 // Process the worklist, deleting the nodes and adding their uses to the
454 while (!DeadNodes.empty()) {
455 SDNode *N = DeadNodes.back();
456 DeadNodes.pop_back();
458 // Take the node out of the appropriate CSE map.
459 RemoveNodeFromCSEMaps(N);
461 // Next, brutally remove the operand list. This is safe to do, as there are
462 // no cycles in the graph.
463 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
464 SDNode *Operand = I->Val;
465 Operand->removeUser(N);
467 // Now that we removed this operand, see if there are no uses of it left.
468 if (Operand->use_empty())
469 DeadNodes.push_back(Operand);
471 if (N->OperandsNeedDelete)
472 delete[] N->OperandList;
476 // Finally, remove N itself.
480 // If the root changed (e.g. it was a dead load, update the root).
481 setRoot(Dummy.getValue());
484 void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
485 SmallVector<SDNode*, 16> DeadNodes;
486 DeadNodes.push_back(N);
488 // Process the worklist, deleting the nodes and adding their uses to the
490 while (!DeadNodes.empty()) {
491 SDNode *N = DeadNodes.back();
492 DeadNodes.pop_back();
495 UpdateListener->NodeDeleted(N);
497 // Take the node out of the appropriate CSE map.
498 RemoveNodeFromCSEMaps(N);
500 // Next, brutally remove the operand list. This is safe to do, as there are
501 // no cycles in the graph.
502 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
503 SDNode *Operand = I->Val;
504 Operand->removeUser(N);
506 // Now that we removed this operand, see if there are no uses of it left.
507 if (Operand->use_empty())
508 DeadNodes.push_back(Operand);
510 if (N->OperandsNeedDelete)
511 delete[] N->OperandList;
515 // Finally, remove N itself.
520 void SelectionDAG::DeleteNode(SDNode *N) {
521 assert(N->use_empty() && "Cannot delete a node that is not dead!");
523 // First take this out of the appropriate CSE map.
524 RemoveNodeFromCSEMaps(N);
526 // Finally, remove uses due to operands of this node, remove from the
527 // AllNodes list, and delete the node.
528 DeleteNodeNotInCSEMaps(N);
531 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
533 // Remove it from the AllNodes list.
536 // Drop all of the operands and decrement used nodes use counts.
537 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
538 I->Val->removeUser(N);
539 if (N->OperandsNeedDelete)
540 delete[] N->OperandList;
547 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
548 /// correspond to it. This is useful when we're about to delete or repurpose
549 /// the node. We don't want future request for structurally identical nodes
550 /// to return N anymore.
551 void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
553 switch (N->getOpcode()) {
554 case ISD::HANDLENODE: return; // noop.
556 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
559 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
560 "Cond code doesn't exist!");
561 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
562 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
564 case ISD::ExternalSymbol:
565 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
567 case ISD::TargetExternalSymbol:
569 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
571 case ISD::VALUETYPE: {
572 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
573 if (MVT::isExtendedVT(VT)) {
574 Erased = ExtendedValueTypeNodes.erase(VT);
576 Erased = ValueTypeNodes[VT] != 0;
577 ValueTypeNodes[VT] = 0;
582 // Remove it from the CSE Map.
583 Erased = CSEMap.RemoveNode(N);
587 // Verify that the node was actually in one of the CSE maps, unless it has a
588 // flag result (which cannot be CSE'd) or is one of the special cases that are
589 // not subject to CSE.
590 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
591 !N->isTargetOpcode()) {
594 assert(0 && "Node is not in map!");
599 /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
600 /// has been taken out and modified in some way. If the specified node already
601 /// exists in the CSE maps, do not modify the maps, but return the existing node
602 /// instead. If it doesn't exist, add it and return null.
604 SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
605 assert(N->getNumOperands() && "This is a leaf node!");
606 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
607 return 0; // Never add these nodes.
609 // Check that remaining values produced are not flags.
610 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
611 if (N->getValueType(i) == MVT::Flag)
612 return 0; // Never CSE anything that produces a flag.
614 SDNode *New = CSEMap.GetOrInsertNode(N);
615 if (New != N) return New; // Node already existed.
619 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
620 /// were replaced with those specified. If this node is never memoized,
621 /// return null, otherwise return a pointer to the slot it would take. If a
622 /// node already exists with these operands, the slot will be non-null.
623 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
625 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
626 return 0; // Never add these nodes.
628 // Check that remaining values produced are not flags.
629 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
630 if (N->getValueType(i) == MVT::Flag)
631 return 0; // Never CSE anything that produces a flag.
633 SDOperand Ops[] = { Op };
635 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
636 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
639 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
640 /// were replaced with those specified. If this node is never memoized,
641 /// return null, otherwise return a pointer to the slot it would take. If a
642 /// node already exists with these operands, the slot will be non-null.
643 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
644 SDOperand Op1, SDOperand Op2,
646 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
647 return 0; // Never add these nodes.
649 // Check that remaining values produced are not flags.
650 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
651 if (N->getValueType(i) == MVT::Flag)
652 return 0; // Never CSE anything that produces a flag.
654 SDOperand Ops[] = { Op1, Op2 };
656 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
657 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
661 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
662 /// were replaced with those specified. If this node is never memoized,
663 /// return null, otherwise return a pointer to the slot it would take. If a
664 /// node already exists with these operands, the slot will be non-null.
665 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
666 const SDOperand *Ops,unsigned NumOps,
668 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
669 return 0; // Never add these nodes.
671 // Check that remaining values produced are not flags.
672 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
673 if (N->getValueType(i) == MVT::Flag)
674 return 0; // Never CSE anything that produces a flag.
677 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
679 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
680 ID.AddInteger(LD->getAddressingMode());
681 ID.AddInteger(LD->getExtensionType());
682 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
683 ID.AddInteger(LD->getAlignment());
684 ID.AddInteger(LD->isVolatile());
685 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
686 ID.AddInteger(ST->getAddressingMode());
687 ID.AddInteger(ST->isTruncatingStore());
688 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
689 ID.AddInteger(ST->getAlignment());
690 ID.AddInteger(ST->isVolatile());
693 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
697 SelectionDAG::~SelectionDAG() {
698 while (!AllNodes.empty()) {
699 SDNode *N = AllNodes.begin();
700 N->SetNextInBucket(0);
701 if (N->OperandsNeedDelete)
702 delete [] N->OperandList;
705 AllNodes.pop_front();
709 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
710 if (Op.getValueType() == VT) return Op;
711 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
712 MVT::getSizeInBits(VT));
713 return getNode(ISD::AND, Op.getValueType(), Op,
714 getConstant(Imm, Op.getValueType()));
717 SDOperand SelectionDAG::getString(const std::string &Val) {
718 StringSDNode *&N = StringNodes[Val];
720 N = new StringSDNode(Val);
721 AllNodes.push_back(N);
723 return SDOperand(N, 0);
726 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
727 MVT::ValueType EltVT =
728 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
730 return getConstant(APInt(MVT::getSizeInBits(EltVT), Val), VT, isT);
733 SDOperand SelectionDAG::getConstant(const APInt &Val, MVT::ValueType VT, bool isT) {
734 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
736 MVT::ValueType EltVT =
737 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
739 assert(Val.getBitWidth() == MVT::getSizeInBits(EltVT) &&
740 "APInt size does not match type size!");
742 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
744 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
748 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
749 if (!MVT::isVector(VT))
750 return SDOperand(N, 0);
752 N = new ConstantSDNode(isT, Val, EltVT);
753 CSEMap.InsertNode(N, IP);
754 AllNodes.push_back(N);
757 SDOperand Result(N, 0);
758 if (MVT::isVector(VT)) {
759 SmallVector<SDOperand, 8> Ops;
760 Ops.assign(MVT::getVectorNumElements(VT), Result);
761 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
766 SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
767 return getConstant(Val, TLI.getPointerTy(), isTarget);
771 SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
773 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
775 MVT::ValueType EltVT =
776 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
778 // Do the map lookup using the actual bit pattern for the floating point
779 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
780 // we don't have issues with SNANs.
781 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
783 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
787 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
788 if (!MVT::isVector(VT))
789 return SDOperand(N, 0);
791 N = new ConstantFPSDNode(isTarget, V, EltVT);
792 CSEMap.InsertNode(N, IP);
793 AllNodes.push_back(N);
796 SDOperand Result(N, 0);
797 if (MVT::isVector(VT)) {
798 SmallVector<SDOperand, 8> Ops;
799 Ops.assign(MVT::getVectorNumElements(VT), Result);
800 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
805 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
807 MVT::ValueType EltVT =
808 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
810 return getConstantFP(APFloat((float)Val), VT, isTarget);
812 return getConstantFP(APFloat(Val), VT, isTarget);
815 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
816 MVT::ValueType VT, int Offset,
820 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
822 // If GV is an alias - use aliasee for determing thread-localness
823 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
824 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
827 if (GVar && GVar->isThreadLocal())
828 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
830 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
833 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
835 ID.AddInteger(Offset);
837 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
838 return SDOperand(E, 0);
839 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
840 CSEMap.InsertNode(N, IP);
841 AllNodes.push_back(N);
842 return SDOperand(N, 0);
845 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
847 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
849 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
852 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
853 return SDOperand(E, 0);
854 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
855 CSEMap.InsertNode(N, IP);
856 AllNodes.push_back(N);
857 return SDOperand(N, 0);
860 SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
861 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
863 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
866 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
867 return SDOperand(E, 0);
868 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
869 CSEMap.InsertNode(N, IP);
870 AllNodes.push_back(N);
871 return SDOperand(N, 0);
874 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
875 unsigned Alignment, int Offset,
877 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
879 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
880 ID.AddInteger(Alignment);
881 ID.AddInteger(Offset);
884 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
885 return SDOperand(E, 0);
886 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
887 CSEMap.InsertNode(N, IP);
888 AllNodes.push_back(N);
889 return SDOperand(N, 0);
893 SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
895 unsigned Alignment, int Offset,
897 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
899 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
900 ID.AddInteger(Alignment);
901 ID.AddInteger(Offset);
902 C->AddSelectionDAGCSEId(ID);
904 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
905 return SDOperand(E, 0);
906 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
907 CSEMap.InsertNode(N, IP);
908 AllNodes.push_back(N);
909 return SDOperand(N, 0);
913 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
915 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
918 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
919 return SDOperand(E, 0);
920 SDNode *N = new BasicBlockSDNode(MBB);
921 CSEMap.InsertNode(N, IP);
922 AllNodes.push_back(N);
923 return SDOperand(N, 0);
926 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
927 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
928 ValueTypeNodes.resize(VT+1);
930 SDNode *&N = MVT::isExtendedVT(VT) ?
931 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
933 if (N) return SDOperand(N, 0);
934 N = new VTSDNode(VT);
935 AllNodes.push_back(N);
936 return SDOperand(N, 0);
939 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
940 SDNode *&N = ExternalSymbols[Sym];
941 if (N) return SDOperand(N, 0);
942 N = new ExternalSymbolSDNode(false, Sym, VT);
943 AllNodes.push_back(N);
944 return SDOperand(N, 0);
947 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
949 SDNode *&N = TargetExternalSymbols[Sym];
950 if (N) return SDOperand(N, 0);
951 N = new ExternalSymbolSDNode(true, Sym, VT);
952 AllNodes.push_back(N);
953 return SDOperand(N, 0);
956 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
957 if ((unsigned)Cond >= CondCodeNodes.size())
958 CondCodeNodes.resize(Cond+1);
960 if (CondCodeNodes[Cond] == 0) {
961 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
962 AllNodes.push_back(CondCodeNodes[Cond]);
964 return SDOperand(CondCodeNodes[Cond], 0);
967 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
969 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
970 ID.AddInteger(RegNo);
972 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
973 return SDOperand(E, 0);
974 SDNode *N = new RegisterSDNode(RegNo, VT);
975 CSEMap.InsertNode(N, IP);
976 AllNodes.push_back(N);
977 return SDOperand(N, 0);
980 SDOperand SelectionDAG::getSrcValue(const Value *V) {
981 assert((!V || isa<PointerType>(V->getType())) &&
982 "SrcValue is not a pointer?");
985 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
989 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
990 return SDOperand(E, 0);
992 SDNode *N = new SrcValueSDNode(V);
993 CSEMap.InsertNode(N, IP);
994 AllNodes.push_back(N);
995 return SDOperand(N, 0);
998 SDOperand SelectionDAG::getMemOperand(const MemOperand &MO) {
999 const Value *v = MO.getValue();
1000 assert((!v || isa<PointerType>(v->getType())) &&
1001 "SrcValue is not a pointer?");
1003 FoldingSetNodeID ID;
1004 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
1006 ID.AddInteger(MO.getFlags());
1007 ID.AddInteger(MO.getOffset());
1008 ID.AddInteger(MO.getSize());
1009 ID.AddInteger(MO.getAlignment());
1012 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1013 return SDOperand(E, 0);
1015 SDNode *N = new MemOperandSDNode(MO);
1016 CSEMap.InsertNode(N, IP);
1017 AllNodes.push_back(N);
1018 return SDOperand(N, 0);
1021 /// CreateStackTemporary - Create a stack temporary, suitable for holding the
1022 /// specified value type.
1023 SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
1024 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1025 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
1026 const Type *Ty = MVT::getTypeForValueType(VT);
1027 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
1028 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1029 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1033 SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
1034 SDOperand N2, ISD::CondCode Cond) {
1035 // These setcc operations always fold.
1039 case ISD::SETFALSE2: return getConstant(0, VT);
1041 case ISD::SETTRUE2: return getConstant(1, VT);
1053 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
1057 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
1058 const APInt &C2 = N2C->getAPIntValue();
1059 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1060 const APInt &C1 = N1C->getAPIntValue();
1063 default: assert(0 && "Unknown integer setcc!");
1064 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1065 case ISD::SETNE: return getConstant(C1 != C2, VT);
1066 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1067 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1068 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1069 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1070 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1071 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1072 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1073 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
1077 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
1078 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
1079 // No compile time operations on this type yet.
1080 if (N1C->getValueType(0) == MVT::ppcf128)
1083 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
1086 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1087 return getNode(ISD::UNDEF, VT);
1089 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1090 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1091 return getNode(ISD::UNDEF, VT);
1093 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
1094 R==APFloat::cmpLessThan, VT);
1095 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1096 return getNode(ISD::UNDEF, VT);
1098 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1099 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1100 return getNode(ISD::UNDEF, VT);
1102 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1103 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1104 return getNode(ISD::UNDEF, VT);
1106 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
1107 R==APFloat::cmpEqual, VT);
1108 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1109 return getNode(ISD::UNDEF, VT);
1111 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
1112 R==APFloat::cmpEqual, VT);
1113 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1114 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1115 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1116 R==APFloat::cmpEqual, VT);
1117 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1118 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1119 R==APFloat::cmpLessThan, VT);
1120 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1121 R==APFloat::cmpUnordered, VT);
1122 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1123 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
1126 // Ensure that the constant occurs on the RHS.
1127 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
1131 // Could not fold it.
1135 /// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1136 /// use this predicate to simplify operations downstream.
1137 bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1138 unsigned BitWidth = Op.getValueSizeInBits();
1139 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1142 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1143 /// this predicate to simplify operations downstream. Mask is known to be zero
1144 /// for bits that V cannot have.
1145 bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
1146 unsigned Depth) const {
1147 APInt KnownZero, KnownOne;
1148 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1149 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1150 return (KnownZero & Mask) == Mask;
1153 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
1154 /// known to be either zero or one and return them in the KnownZero/KnownOne
1155 /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1157 void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
1158 APInt &KnownZero, APInt &KnownOne,
1159 unsigned Depth) const {
1160 unsigned BitWidth = Mask.getBitWidth();
1161 assert(BitWidth == MVT::getSizeInBits(Op.getValueType()) &&
1162 "Mask size mismatches value type size!");
1164 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
1165 if (Depth == 6 || Mask == 0)
1166 return; // Limit search depth.
1168 APInt KnownZero2, KnownOne2;
1170 switch (Op.getOpcode()) {
1172 // We know all of the bits for a constant!
1173 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
1174 KnownZero = ~KnownOne & Mask;
1177 // If either the LHS or the RHS are Zero, the result is zero.
1178 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1179 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1180 KnownZero2, KnownOne2, Depth+1);
1181 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1182 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1184 // Output known-1 bits are only known if set in both the LHS & RHS.
1185 KnownOne &= KnownOne2;
1186 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1187 KnownZero |= KnownZero2;
1190 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1191 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1192 KnownZero2, KnownOne2, Depth+1);
1193 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1194 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1196 // Output known-0 bits are only known if clear in both the LHS & RHS.
1197 KnownZero &= KnownZero2;
1198 // Output known-1 are known to be set if set in either the LHS | RHS.
1199 KnownOne |= KnownOne2;
1202 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1203 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1204 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1205 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1207 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1208 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1209 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1210 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1211 KnownZero = KnownZeroOut;
1215 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1216 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1217 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1218 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1220 // Only known if known in both the LHS and RHS.
1221 KnownOne &= KnownOne2;
1222 KnownZero &= KnownZero2;
1224 case ISD::SELECT_CC:
1225 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1226 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1227 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1228 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1230 // Only known if known in both the LHS and RHS.
1231 KnownOne &= KnownOne2;
1232 KnownZero &= KnownZero2;
1235 // If we know the result of a setcc has the top bits zero, use this info.
1236 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1238 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
1241 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1242 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1243 unsigned ShAmt = SA->getValue();
1245 // If the shift count is an invalid immediate, don't do anything.
1246 if (ShAmt >= BitWidth)
1249 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
1250 KnownZero, KnownOne, Depth+1);
1251 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1252 KnownZero <<= ShAmt;
1254 // low bits known zero.
1255 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
1259 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1260 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1261 unsigned ShAmt = SA->getValue();
1263 // If the shift count is an invalid immediate, don't do anything.
1264 if (ShAmt >= BitWidth)
1267 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
1268 KnownZero, KnownOne, Depth+1);
1269 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1270 KnownZero = KnownZero.lshr(ShAmt);
1271 KnownOne = KnownOne.lshr(ShAmt);
1273 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1274 KnownZero |= HighBits; // High bits known zero.
1278 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1279 unsigned ShAmt = SA->getValue();
1281 // If the shift count is an invalid immediate, don't do anything.
1282 if (ShAmt >= BitWidth)
1285 APInt InDemandedMask = (Mask << ShAmt);
1286 // If any of the demanded bits are produced by the sign extension, we also
1287 // demand the input sign bit.
1288 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1289 if (HighBits.getBoolValue())
1290 InDemandedMask |= APInt::getSignBit(BitWidth);
1292 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1294 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1295 KnownZero = KnownZero.lshr(ShAmt);
1296 KnownOne = KnownOne.lshr(ShAmt);
1298 // Handle the sign bits.
1299 APInt SignBit = APInt::getSignBit(BitWidth);
1300 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
1302 if (KnownZero.intersects(SignBit)) {
1303 KnownZero |= HighBits; // New bits are known zero.
1304 } else if (KnownOne.intersects(SignBit)) {
1305 KnownOne |= HighBits; // New bits are known one.
1309 case ISD::SIGN_EXTEND_INREG: {
1310 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1311 unsigned EBits = MVT::getSizeInBits(EVT);
1313 // Sign extension. Compute the demanded bits in the result that are not
1314 // present in the input.
1315 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
1317 APInt InSignBit = APInt::getSignBit(EBits);
1318 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
1320 // If the sign extended bits are demanded, we know that the sign
1322 InSignBit.zext(BitWidth);
1323 if (NewBits.getBoolValue())
1324 InputDemandedBits |= InSignBit;
1326 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1327 KnownZero, KnownOne, Depth+1);
1328 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1330 // If the sign bit of the input is known set or clear, then we know the
1331 // top bits of the result.
1332 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
1333 KnownZero |= NewBits;
1334 KnownOne &= ~NewBits;
1335 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
1336 KnownOne |= NewBits;
1337 KnownZero &= ~NewBits;
1338 } else { // Input sign bit unknown
1339 KnownZero &= ~NewBits;
1340 KnownOne &= ~NewBits;
1347 unsigned LowBits = Log2_32(BitWidth)+1;
1348 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
1349 KnownOne = APInt(BitWidth, 0);
1353 if (ISD::isZEXTLoad(Op.Val)) {
1354 LoadSDNode *LD = cast<LoadSDNode>(Op);
1355 MVT::ValueType VT = LD->getMemoryVT();
1356 unsigned MemBits = MVT::getSizeInBits(VT);
1357 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
1361 case ISD::ZERO_EXTEND: {
1362 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1363 unsigned InBits = MVT::getSizeInBits(InVT);
1364 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1365 APInt InMask = Mask;
1366 InMask.trunc(InBits);
1367 KnownZero.trunc(InBits);
1368 KnownOne.trunc(InBits);
1369 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1370 KnownZero.zext(BitWidth);
1371 KnownOne.zext(BitWidth);
1372 KnownZero |= NewBits;
1375 case ISD::SIGN_EXTEND: {
1376 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1377 unsigned InBits = MVT::getSizeInBits(InVT);
1378 APInt InSignBit = APInt::getSignBit(InBits);
1379 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1380 APInt InMask = Mask;
1381 InMask.trunc(InBits);
1383 // If any of the sign extended bits are demanded, we know that the sign
1384 // bit is demanded. Temporarily set this bit in the mask for our callee.
1385 if (NewBits.getBoolValue())
1386 InMask |= InSignBit;
1388 KnownZero.trunc(InBits);
1389 KnownOne.trunc(InBits);
1390 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1392 // Note if the sign bit is known to be zero or one.
1393 bool SignBitKnownZero = KnownZero.isNegative();
1394 bool SignBitKnownOne = KnownOne.isNegative();
1395 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1396 "Sign bit can't be known to be both zero and one!");
1398 // If the sign bit wasn't actually demanded by our caller, we don't
1399 // want it set in the KnownZero and KnownOne result values. Reset the
1400 // mask and reapply it to the result values.
1402 InMask.trunc(InBits);
1403 KnownZero &= InMask;
1406 KnownZero.zext(BitWidth);
1407 KnownOne.zext(BitWidth);
1409 // If the sign bit is known zero or one, the top bits match.
1410 if (SignBitKnownZero)
1411 KnownZero |= NewBits;
1412 else if (SignBitKnownOne)
1413 KnownOne |= NewBits;
1416 case ISD::ANY_EXTEND: {
1417 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1418 unsigned InBits = MVT::getSizeInBits(InVT);
1419 APInt InMask = Mask;
1420 InMask.trunc(InBits);
1421 KnownZero.trunc(InBits);
1422 KnownOne.trunc(InBits);
1423 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1424 KnownZero.zext(BitWidth);
1425 KnownOne.zext(BitWidth);
1428 case ISD::TRUNCATE: {
1429 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1430 unsigned InBits = MVT::getSizeInBits(InVT);
1431 APInt InMask = Mask;
1432 InMask.zext(InBits);
1433 KnownZero.zext(InBits);
1434 KnownOne.zext(InBits);
1435 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1436 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1437 KnownZero.trunc(BitWidth);
1438 KnownOne.trunc(BitWidth);
1441 case ISD::AssertZext: {
1442 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1443 APInt InMask = APInt::getLowBitsSet(BitWidth, MVT::getSizeInBits(VT));
1444 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1446 KnownZero |= (~InMask) & Mask;
1450 // All bits are zero except the low bit.
1451 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
1455 // If either the LHS or the RHS are Zero, the result is zero.
1456 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1457 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1458 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1459 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1461 // Output known-0 bits are known if clear or set in both the low clear bits
1462 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1463 // low 3 bits clear.
1464 unsigned KnownZeroOut = std::min(KnownZero.countTrailingOnes(),
1465 KnownZero2.countTrailingOnes());
1467 KnownZero = APInt::getLowBitsSet(BitWidth, KnownZeroOut);
1468 KnownOne = APInt(BitWidth, 0);
1472 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1475 // We know that the top bits of C-X are clear if X contains less bits
1476 // than C (i.e. no wrap-around can happen). For example, 20-X is
1477 // positive if we can prove that X is >= 0 and < 16.
1478 if (CLHS->getAPIntValue().isNonNegative()) {
1479 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1480 // NLZ can't be BitWidth with no sign bit
1481 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1482 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1484 // If all of the MaskV bits are known to be zero, then we know the output
1485 // top bits are zero, because we now know that the output is from [0-C].
1486 if ((KnownZero & MaskV) == MaskV) {
1487 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1488 // Top bits known zero.
1489 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1490 KnownOne = APInt(BitWidth, 0); // No one bits known.
1492 KnownZero = KnownOne = APInt(BitWidth, 0); // Otherwise, nothing known.
1498 // Allow the target to implement this method for its nodes.
1499 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1500 case ISD::INTRINSIC_WO_CHAIN:
1501 case ISD::INTRINSIC_W_CHAIN:
1502 case ISD::INTRINSIC_VOID:
1503 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1509 /// ComputeNumSignBits - Return the number of times the sign bit of the
1510 /// register is replicated into the other bits. We know that at least 1 bit
1511 /// is always equal to the sign bit (itself), but other cases can give us
1512 /// information. For example, immediately after an "SRA X, 2", we know that
1513 /// the top 3 bits are all equal to each other, so we return 3.
1514 unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1515 MVT::ValueType VT = Op.getValueType();
1516 assert(MVT::isInteger(VT) && "Invalid VT!");
1517 unsigned VTBits = MVT::getSizeInBits(VT);
1521 return 1; // Limit search depth.
1523 switch (Op.getOpcode()) {
1525 case ISD::AssertSext:
1526 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1527 return VTBits-Tmp+1;
1528 case ISD::AssertZext:
1529 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1532 case ISD::Constant: {
1533 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1534 // If negative, return # leading ones.
1535 if (Val.isNegative())
1536 return Val.countLeadingOnes();
1538 // Return # leading zeros.
1539 return Val.countLeadingZeros();
1542 case ISD::SIGN_EXTEND:
1543 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1544 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1546 case ISD::SIGN_EXTEND_INREG:
1547 // Max of the input and what this extends.
1548 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1551 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1552 return std::max(Tmp, Tmp2);
1555 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1556 // SRA X, C -> adds C sign bits.
1557 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1558 Tmp += C->getValue();
1559 if (Tmp > VTBits) Tmp = VTBits;
1563 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1564 // shl destroys sign bits.
1565 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1566 if (C->getValue() >= VTBits || // Bad shift.
1567 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1568 return Tmp - C->getValue();
1573 case ISD::XOR: // NOT is handled here.
1574 // Logical binary ops preserve the number of sign bits.
1575 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1576 if (Tmp == 1) return 1; // Early out.
1577 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1578 return std::min(Tmp, Tmp2);
1581 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1582 if (Tmp == 1) return 1; // Early out.
1583 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1584 return std::min(Tmp, Tmp2);
1587 // If setcc returns 0/-1, all bits are sign bits.
1588 if (TLI.getSetCCResultContents() ==
1589 TargetLowering::ZeroOrNegativeOneSetCCResult)
1594 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1595 unsigned RotAmt = C->getValue() & (VTBits-1);
1597 // Handle rotate right by N like a rotate left by 32-N.
1598 if (Op.getOpcode() == ISD::ROTR)
1599 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1601 // If we aren't rotating out all of the known-in sign bits, return the
1602 // number that are left. This handles rotl(sext(x), 1) for example.
1603 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1604 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1608 // Add can have at most one carry bit. Thus we know that the output
1609 // is, at worst, one more bit than the inputs.
1610 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1611 if (Tmp == 1) return 1; // Early out.
1613 // Special case decrementing a value (ADD X, -1):
1614 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1615 if (CRHS->isAllOnesValue()) {
1616 APInt KnownZero, KnownOne;
1617 APInt Mask = APInt::getAllOnesValue(VTBits);
1618 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1620 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1622 if ((KnownZero | APInt(VTBits, 1)) == Mask)
1625 // If we are subtracting one from a positive number, there is no carry
1626 // out of the result.
1627 if (KnownZero.isNegative())
1631 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1632 if (Tmp2 == 1) return 1;
1633 return std::min(Tmp, Tmp2)-1;
1637 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1638 if (Tmp2 == 1) return 1;
1641 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1642 if (CLHS->getValue() == 0) {
1643 APInt KnownZero, KnownOne;
1644 APInt Mask = APInt::getAllOnesValue(VTBits);
1645 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1646 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1648 if ((KnownZero | APInt(VTBits, 1)) == Mask)
1651 // If the input is known to be positive (the sign bit is known clear),
1652 // the output of the NEG has the same number of sign bits as the input.
1653 if (KnownZero.isNegative())
1656 // Otherwise, we treat this like a SUB.
1659 // Sub can have at most one carry bit. Thus we know that the output
1660 // is, at worst, one more bit than the inputs.
1661 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1662 if (Tmp == 1) return 1; // Early out.
1663 return std::min(Tmp, Tmp2)-1;
1666 // FIXME: it's tricky to do anything useful for this, but it is an important
1667 // case for targets like X86.
1671 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1672 if (Op.getOpcode() == ISD::LOAD) {
1673 LoadSDNode *LD = cast<LoadSDNode>(Op);
1674 unsigned ExtType = LD->getExtensionType();
1677 case ISD::SEXTLOAD: // '17' bits known
1678 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
1679 return VTBits-Tmp+1;
1680 case ISD::ZEXTLOAD: // '16' bits known
1681 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
1686 // Allow the target to implement this method for its nodes.
1687 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1688 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1689 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1690 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1691 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1692 if (NumBits > 1) return NumBits;
1695 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1696 // use this information.
1697 APInt KnownZero, KnownOne;
1698 APInt Mask = APInt::getAllOnesValue(VTBits);
1699 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1701 if (KnownZero.isNegative()) { // sign bit is 0
1703 } else if (KnownOne.isNegative()) { // sign bit is 1;
1710 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1711 // the number of identical bits in the top of the input value.
1713 Mask <<= Mask.getBitWidth()-VTBits;
1714 // Return # leading zeros. We use 'min' here in case Val was zero before
1715 // shifting. We don't want to return '64' as for an i32 "0".
1716 return std::min(VTBits, Mask.countLeadingZeros());
1720 bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1721 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1722 if (!GA) return false;
1723 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1724 if (!GV) return false;
1725 MachineModuleInfo *MMI = getMachineModuleInfo();
1726 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1730 /// getNode - Gets or creates the specified node.
1732 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
1733 FoldingSetNodeID ID;
1734 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
1736 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1737 return SDOperand(E, 0);
1738 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
1739 CSEMap.InsertNode(N, IP);
1741 AllNodes.push_back(N);
1742 return SDOperand(N, 0);
1745 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1746 SDOperand Operand) {
1747 // Constant fold unary operations with an integer constant operand.
1748 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1749 const APInt &Val = C->getAPIntValue();
1750 unsigned BitWidth = MVT::getSizeInBits(VT);
1753 case ISD::SIGN_EXTEND:
1754 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
1755 case ISD::ANY_EXTEND:
1756 case ISD::ZERO_EXTEND:
1758 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
1759 case ISD::UINT_TO_FP:
1760 case ISD::SINT_TO_FP: {
1761 const uint64_t zero[] = {0, 0};
1762 // No compile time operations on this type.
1763 if (VT==MVT::ppcf128)
1765 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1766 (void)apf.convertFromAPInt(Val,
1767 Opcode==ISD::SINT_TO_FP,
1768 APFloat::rmNearestTiesToEven);
1769 return getConstantFP(apf, VT);
1771 case ISD::BIT_CONVERT:
1772 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
1773 return getConstantFP(Val.bitsToFloat(), VT);
1774 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
1775 return getConstantFP(Val.bitsToDouble(), VT);
1778 return getConstant(Val.byteSwap(), VT);
1780 return getConstant(Val.countPopulation(), VT);
1782 return getConstant(Val.countLeadingZeros(), VT);
1784 return getConstant(Val.countTrailingZeros(), VT);
1788 // Constant fold unary operations with a floating point constant operand.
1789 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1790 APFloat V = C->getValueAPF(); // make copy
1791 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
1795 return getConstantFP(V, VT);
1798 return getConstantFP(V, VT);
1800 case ISD::FP_EXTEND:
1801 // This can return overflow, underflow, or inexact; we don't care.
1802 // FIXME need to be more flexible about rounding mode.
1803 (void)V.convert(*MVTToAPFloatSemantics(VT),
1804 APFloat::rmNearestTiesToEven);
1805 return getConstantFP(V, VT);
1806 case ISD::FP_TO_SINT:
1807 case ISD::FP_TO_UINT: {
1809 assert(integerPartWidth >= 64);
1810 // FIXME need to be more flexible about rounding mode.
1811 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1812 Opcode==ISD::FP_TO_SINT,
1813 APFloat::rmTowardZero);
1814 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1816 return getConstant(x, VT);
1818 case ISD::BIT_CONVERT:
1819 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1820 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1821 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1822 return getConstant(V.convertToAPInt().getZExtValue(), VT);
1828 unsigned OpOpcode = Operand.Val->getOpcode();
1830 case ISD::TokenFactor:
1831 return Operand; // Factor of one node? No factor.
1832 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
1833 case ISD::FP_EXTEND:
1834 assert(MVT::isFloatingPoint(VT) &&
1835 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
1836 if (Operand.getValueType() == VT) return Operand; // noop conversion.
1837 if (Operand.getOpcode() == ISD::UNDEF)
1838 return getNode(ISD::UNDEF, VT);
1840 case ISD::SIGN_EXTEND:
1841 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1842 "Invalid SIGN_EXTEND!");
1843 if (Operand.getValueType() == VT) return Operand; // noop extension
1844 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1845 && "Invalid sext node, dst < src!");
1846 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1847 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1849 case ISD::ZERO_EXTEND:
1850 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1851 "Invalid ZERO_EXTEND!");
1852 if (Operand.getValueType() == VT) return Operand; // noop extension
1853 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1854 && "Invalid zext node, dst < src!");
1855 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1856 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1858 case ISD::ANY_EXTEND:
1859 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1860 "Invalid ANY_EXTEND!");
1861 if (Operand.getValueType() == VT) return Operand; // noop extension
1862 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1863 && "Invalid anyext node, dst < src!");
1864 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1865 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1866 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1869 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1870 "Invalid TRUNCATE!");
1871 if (Operand.getValueType() == VT) return Operand; // noop truncate
1872 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1873 && "Invalid truncate node, src < dst!");
1874 if (OpOpcode == ISD::TRUNCATE)
1875 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1876 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1877 OpOpcode == ISD::ANY_EXTEND) {
1878 // If the source is smaller than the dest, we still need an extend.
1879 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1880 < MVT::getSizeInBits(VT))
1881 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1882 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1883 > MVT::getSizeInBits(VT))
1884 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1886 return Operand.Val->getOperand(0);
1889 case ISD::BIT_CONVERT:
1890 // Basic sanity checking.
1891 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
1892 && "Cannot BIT_CONVERT between types of different sizes!");
1893 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1894 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1895 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1896 if (OpOpcode == ISD::UNDEF)
1897 return getNode(ISD::UNDEF, VT);
1899 case ISD::SCALAR_TO_VECTOR:
1900 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1901 MVT::getVectorElementType(VT) == Operand.getValueType() &&
1902 "Illegal SCALAR_TO_VECTOR node!");
1903 if (OpOpcode == ISD::UNDEF)
1904 return getNode(ISD::UNDEF, VT);
1905 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
1906 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
1907 isa<ConstantSDNode>(Operand.getOperand(1)) &&
1908 Operand.getConstantOperandVal(1) == 0 &&
1909 Operand.getOperand(0).getValueType() == VT)
1910 return Operand.getOperand(0);
1913 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1914 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1915 Operand.Val->getOperand(0));
1916 if (OpOpcode == ISD::FNEG) // --X -> X
1917 return Operand.Val->getOperand(0);
1920 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1921 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1926 SDVTList VTs = getVTList(VT);
1927 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1928 FoldingSetNodeID ID;
1929 SDOperand Ops[1] = { Operand };
1930 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
1932 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1933 return SDOperand(E, 0);
1934 N = new UnarySDNode(Opcode, VTs, Operand);
1935 CSEMap.InsertNode(N, IP);
1937 N = new UnarySDNode(Opcode, VTs, Operand);
1939 AllNodes.push_back(N);
1940 return SDOperand(N, 0);
1945 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1946 SDOperand N1, SDOperand N2) {
1947 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1948 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1951 case ISD::TokenFactor:
1952 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1953 N2.getValueType() == MVT::Other && "Invalid token factor!");
1954 // Fold trivial token factors.
1955 if (N1.getOpcode() == ISD::EntryToken) return N2;
1956 if (N2.getOpcode() == ISD::EntryToken) return N1;
1959 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1960 N1.getValueType() == VT && "Binary operator types must match!");
1961 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1962 // worth handling here.
1963 if (N2C && N2C->getValue() == 0)
1965 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
1970 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1971 N1.getValueType() == VT && "Binary operator types must match!");
1972 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1973 // worth handling here.
1974 if (N2C && N2C->getValue() == 0)
1981 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1993 assert(N1.getValueType() == N2.getValueType() &&
1994 N1.getValueType() == VT && "Binary operator types must match!");
1996 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1997 assert(N1.getValueType() == VT &&
1998 MVT::isFloatingPoint(N1.getValueType()) &&
1999 MVT::isFloatingPoint(N2.getValueType()) &&
2000 "Invalid FCOPYSIGN!");
2007 assert(VT == N1.getValueType() &&
2008 "Shift operators return type must be the same as their first arg");
2009 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
2010 VT != MVT::i1 && "Shifts only work on integers");
2012 case ISD::FP_ROUND_INREG: {
2013 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2014 assert(VT == N1.getValueType() && "Not an inreg round!");
2015 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
2016 "Cannot FP_ROUND_INREG integer types");
2017 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2018 "Not rounding down!");
2019 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
2023 assert(MVT::isFloatingPoint(VT) &&
2024 MVT::isFloatingPoint(N1.getValueType()) &&
2025 MVT::getSizeInBits(VT) <= MVT::getSizeInBits(N1.getValueType()) &&
2026 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
2027 if (N1.getValueType() == VT) return N1; // noop conversion.
2029 case ISD::AssertSext:
2030 case ISD::AssertZext: {
2031 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2032 assert(VT == N1.getValueType() && "Not an inreg extend!");
2033 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2034 "Cannot *_EXTEND_INREG FP types");
2035 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2037 if (VT == EVT) return N1; // noop assertion.
2040 case ISD::SIGN_EXTEND_INREG: {
2041 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2042 assert(VT == N1.getValueType() && "Not an inreg extend!");
2043 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2044 "Cannot *_EXTEND_INREG FP types");
2045 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2047 if (EVT == VT) return N1; // Not actually extending
2050 APInt Val = N1C->getAPIntValue();
2051 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
2052 Val <<= Val.getBitWidth()-FromBits;
2053 Val = Val.ashr(Val.getBitWidth()-FromBits);
2054 return getConstant(Val, VT);
2058 case ISD::EXTRACT_VECTOR_ELT:
2059 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2061 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2062 if (N1.getOpcode() == ISD::UNDEF)
2063 return getNode(ISD::UNDEF, VT);
2065 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2066 // expanding copies of large vectors from registers.
2067 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2068 N1.getNumOperands() > 0) {
2070 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2071 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2072 N1.getOperand(N2C->getValue() / Factor),
2073 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2076 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2077 // expanding large vector constants.
2078 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2079 return N1.getOperand(N2C->getValue());
2081 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2082 // operations are lowered to scalars.
2083 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2084 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2086 return N1.getOperand(1);
2088 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2091 case ISD::EXTRACT_ELEMENT:
2092 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
2093 assert(!MVT::isVector(N1.getValueType()) &&
2094 MVT::isInteger(N1.getValueType()) &&
2095 !MVT::isVector(VT) && MVT::isInteger(VT) &&
2096 "EXTRACT_ELEMENT only applies to integers!");
2098 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2099 // 64-bit integers into 32-bit parts. Instead of building the extract of
2100 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2101 if (N1.getOpcode() == ISD::BUILD_PAIR)
2102 return N1.getOperand(N2C->getValue());
2104 // EXTRACT_ELEMENT of a constant int is also very common.
2105 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2106 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2107 return getConstant(C->getValue() >> Shift, VT);
2110 case ISD::EXTRACT_SUBVECTOR:
2111 if (N1.getValueType() == VT) // Trivial extraction.
2118 APInt C1 = N1C->getAPIntValue(), C2 = N2C->getAPIntValue();
2120 case ISD::ADD: return getConstant(C1 + C2, VT);
2121 case ISD::SUB: return getConstant(C1 - C2, VT);
2122 case ISD::MUL: return getConstant(C1 * C2, VT);
2124 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2127 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2130 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2133 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2135 case ISD::AND : return getConstant(C1 & C2, VT);
2136 case ISD::OR : return getConstant(C1 | C2, VT);
2137 case ISD::XOR : return getConstant(C1 ^ C2, VT);
2138 case ISD::SHL : return getConstant(C1 << C2, VT);
2139 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2140 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2141 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2142 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
2145 } else { // Cannonicalize constant to RHS if commutative
2146 if (isCommutativeBinOp(Opcode)) {
2147 std::swap(N1C, N2C);
2153 // Constant fold FP operations.
2154 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2155 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
2157 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2158 // Cannonicalize constant to RHS if commutative
2159 std::swap(N1CFP, N2CFP);
2161 } else if (N2CFP && VT != MVT::ppcf128) {
2162 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2163 APFloat::opStatus s;
2166 s = V1.add(V2, APFloat::rmNearestTiesToEven);
2167 if (s != APFloat::opInvalidOp)
2168 return getConstantFP(V1, VT);
2171 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2172 if (s!=APFloat::opInvalidOp)
2173 return getConstantFP(V1, VT);
2176 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2177 if (s!=APFloat::opInvalidOp)
2178 return getConstantFP(V1, VT);
2181 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2182 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2183 return getConstantFP(V1, VT);
2186 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2187 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2188 return getConstantFP(V1, VT);
2190 case ISD::FCOPYSIGN:
2192 return getConstantFP(V1, VT);
2198 // Canonicalize an UNDEF to the RHS, even over a constant.
2199 if (N1.getOpcode() == ISD::UNDEF) {
2200 if (isCommutativeBinOp(Opcode)) {
2204 case ISD::FP_ROUND_INREG:
2205 case ISD::SIGN_EXTEND_INREG:
2211 return N1; // fold op(undef, arg2) -> undef
2218 if (!MVT::isVector(VT))
2219 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2220 // For vectors, we can't easily build an all zero vector, just return
2227 // Fold a bunch of operators when the RHS is undef.
2228 if (N2.getOpcode() == ISD::UNDEF) {
2244 return N2; // fold op(arg1, undef) -> undef
2249 if (!MVT::isVector(VT))
2250 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2251 // For vectors, we can't easily build an all zero vector, just return
2255 if (!MVT::isVector(VT))
2256 return getConstant(MVT::getIntVTBitMask(VT), VT);
2257 // For vectors, we can't easily build an all one vector, just return
2265 // Memoize this node if possible.
2267 SDVTList VTs = getVTList(VT);
2268 if (VT != MVT::Flag) {
2269 SDOperand Ops[] = { N1, N2 };
2270 FoldingSetNodeID ID;
2271 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
2273 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2274 return SDOperand(E, 0);
2275 N = new BinarySDNode(Opcode, VTs, N1, N2);
2276 CSEMap.InsertNode(N, IP);
2278 N = new BinarySDNode(Opcode, VTs, N1, N2);
2281 AllNodes.push_back(N);
2282 return SDOperand(N, 0);
2285 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2286 SDOperand N1, SDOperand N2, SDOperand N3) {
2287 // Perform various simplifications.
2288 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2289 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2292 // Use FoldSetCC to simplify SETCC's.
2293 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
2294 if (Simp.Val) return Simp;
2299 if (N1C->getValue())
2300 return N2; // select true, X, Y -> X
2302 return N3; // select false, X, Y -> Y
2305 if (N2 == N3) return N2; // select C, X, X -> X
2309 if (N2C->getValue()) // Unconditional branch
2310 return getNode(ISD::BR, MVT::Other, N1, N3);
2312 return N1; // Never-taken branch
2315 case ISD::VECTOR_SHUFFLE:
2316 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2317 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2318 N3.getOpcode() == ISD::BUILD_VECTOR &&
2319 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2320 "Illegal VECTOR_SHUFFLE node!");
2322 case ISD::BIT_CONVERT:
2323 // Fold bit_convert nodes from a type to themselves.
2324 if (N1.getValueType() == VT)
2329 // Memoize node if it doesn't produce a flag.
2331 SDVTList VTs = getVTList(VT);
2332 if (VT != MVT::Flag) {
2333 SDOperand Ops[] = { N1, N2, N3 };
2334 FoldingSetNodeID ID;
2335 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2337 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2338 return SDOperand(E, 0);
2339 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2340 CSEMap.InsertNode(N, IP);
2342 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2344 AllNodes.push_back(N);
2345 return SDOperand(N, 0);
2348 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2349 SDOperand N1, SDOperand N2, SDOperand N3,
2351 SDOperand Ops[] = { N1, N2, N3, N4 };
2352 return getNode(Opcode, VT, Ops, 4);
2355 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2356 SDOperand N1, SDOperand N2, SDOperand N3,
2357 SDOperand N4, SDOperand N5) {
2358 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2359 return getNode(Opcode, VT, Ops, 5);
2362 SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dest,
2363 SDOperand Src, SDOperand Size,
2365 SDOperand AlwaysInline) {
2366 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2367 return getNode(ISD::MEMCPY, MVT::Other, Ops, 6);
2370 SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dest,
2371 SDOperand Src, SDOperand Size,
2373 SDOperand AlwaysInline) {
2374 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2375 return getNode(ISD::MEMMOVE, MVT::Other, Ops, 6);
2378 SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dest,
2379 SDOperand Src, SDOperand Size,
2381 SDOperand AlwaysInline) {
2382 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2383 return getNode(ISD::MEMSET, MVT::Other, Ops, 6);
2386 SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
2387 SDOperand Ptr, SDOperand Cmp,
2388 SDOperand Swp, MVT::ValueType VT) {
2389 assert(Opcode == ISD::ATOMIC_LCS && "Invalid Atomic Op");
2390 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
2391 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
2392 FoldingSetNodeID ID;
2393 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
2394 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
2395 ID.AddInteger((unsigned int)VT);
2397 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2398 return SDOperand(E, 0);
2399 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, VT);
2400 CSEMap.InsertNode(N, IP);
2401 AllNodes.push_back(N);
2402 return SDOperand(N, 0);
2405 SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
2406 SDOperand Ptr, SDOperand Val,
2407 MVT::ValueType VT) {
2408 assert((Opcode == ISD::ATOMIC_LAS || Opcode == ISD::ATOMIC_SWAP)
2409 && "Invalid Atomic Op");
2410 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
2411 FoldingSetNodeID ID;
2412 SDOperand Ops[] = {Chain, Ptr, Val};
2413 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2414 ID.AddInteger((unsigned int)VT);
2416 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2417 return SDOperand(E, 0);
2418 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, VT);
2419 CSEMap.InsertNode(N, IP);
2420 AllNodes.push_back(N);
2421 return SDOperand(N, 0);
2424 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2425 SDOperand Chain, SDOperand Ptr,
2426 const Value *SV, int SVOffset,
2427 bool isVolatile, unsigned Alignment) {
2428 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2430 if (VT != MVT::iPTR) {
2431 Ty = MVT::getTypeForValueType(VT);
2433 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2434 assert(PT && "Value for load must be a pointer");
2435 Ty = PT->getElementType();
2437 assert(Ty && "Could not get type information for load");
2438 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2440 SDVTList VTs = getVTList(VT, MVT::Other);
2441 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2442 SDOperand Ops[] = { Chain, Ptr, Undef };
2443 FoldingSetNodeID ID;
2444 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2445 ID.AddInteger(ISD::UNINDEXED);
2446 ID.AddInteger(ISD::NON_EXTLOAD);
2447 ID.AddInteger((unsigned int)VT);
2448 ID.AddInteger(Alignment);
2449 ID.AddInteger(isVolatile);
2451 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2452 return SDOperand(E, 0);
2453 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
2454 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2456 CSEMap.InsertNode(N, IP);
2457 AllNodes.push_back(N);
2458 return SDOperand(N, 0);
2461 SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
2462 SDOperand Chain, SDOperand Ptr,
2464 int SVOffset, MVT::ValueType EVT,
2465 bool isVolatile, unsigned Alignment) {
2466 // If they are asking for an extending load from/to the same thing, return a
2469 return getLoad(VT, Chain, Ptr, SV, SVOffset, isVolatile, Alignment);
2471 if (MVT::isVector(VT))
2472 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
2474 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2475 "Should only be an extending load, not truncating!");
2476 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2477 "Cannot sign/zero extend a FP/Vector load!");
2478 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2479 "Cannot convert from FP to Int or Int -> FP!");
2481 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2483 if (VT != MVT::iPTR) {
2484 Ty = MVT::getTypeForValueType(VT);
2486 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2487 assert(PT && "Value for load must be a pointer");
2488 Ty = PT->getElementType();
2490 assert(Ty && "Could not get type information for load");
2491 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2493 SDVTList VTs = getVTList(VT, MVT::Other);
2494 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2495 SDOperand Ops[] = { Chain, Ptr, Undef };
2496 FoldingSetNodeID ID;
2497 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2498 ID.AddInteger(ISD::UNINDEXED);
2499 ID.AddInteger(ExtType);
2500 ID.AddInteger((unsigned int)EVT);
2501 ID.AddInteger(Alignment);
2502 ID.AddInteger(isVolatile);
2504 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2505 return SDOperand(E, 0);
2506 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
2507 SV, SVOffset, Alignment, isVolatile);
2508 CSEMap.InsertNode(N, IP);
2509 AllNodes.push_back(N);
2510 return SDOperand(N, 0);
2514 SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2515 SDOperand Offset, ISD::MemIndexedMode AM) {
2516 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
2517 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2518 "Load is already a indexed load!");
2519 MVT::ValueType VT = OrigLoad.getValueType();
2520 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
2521 SDOperand Ops[] = { LD->getChain(), Base, Offset };
2522 FoldingSetNodeID ID;
2523 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2525 ID.AddInteger(LD->getExtensionType());
2526 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
2527 ID.AddInteger(LD->getAlignment());
2528 ID.AddInteger(LD->isVolatile());
2530 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2531 return SDOperand(E, 0);
2532 SDNode *N = new LoadSDNode(Ops, VTs, AM,
2533 LD->getExtensionType(), LD->getMemoryVT(),
2534 LD->getSrcValue(), LD->getSrcValueOffset(),
2535 LD->getAlignment(), LD->isVolatile());
2536 CSEMap.InsertNode(N, IP);
2537 AllNodes.push_back(N);
2538 return SDOperand(N, 0);
2541 SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
2542 SDOperand Ptr, const Value *SV, int SVOffset,
2543 bool isVolatile, unsigned Alignment) {
2544 MVT::ValueType VT = Val.getValueType();
2546 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2548 if (VT != MVT::iPTR) {
2549 Ty = MVT::getTypeForValueType(VT);
2551 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2552 assert(PT && "Value for store must be a pointer");
2553 Ty = PT->getElementType();
2555 assert(Ty && "Could not get type information for store");
2556 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2558 SDVTList VTs = getVTList(MVT::Other);
2559 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2560 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2561 FoldingSetNodeID ID;
2562 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2563 ID.AddInteger(ISD::UNINDEXED);
2564 ID.AddInteger(false);
2565 ID.AddInteger((unsigned int)VT);
2566 ID.AddInteger(Alignment);
2567 ID.AddInteger(isVolatile);
2569 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2570 return SDOperand(E, 0);
2571 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
2572 VT, SV, SVOffset, Alignment, isVolatile);
2573 CSEMap.InsertNode(N, IP);
2574 AllNodes.push_back(N);
2575 return SDOperand(N, 0);
2578 SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
2579 SDOperand Ptr, const Value *SV,
2580 int SVOffset, MVT::ValueType SVT,
2581 bool isVolatile, unsigned Alignment) {
2582 MVT::ValueType VT = Val.getValueType();
2585 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
2587 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2588 "Not a truncation?");
2589 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2590 "Can't do FP-INT conversion!");
2592 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2594 if (VT != MVT::iPTR) {
2595 Ty = MVT::getTypeForValueType(VT);
2597 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2598 assert(PT && "Value for store must be a pointer");
2599 Ty = PT->getElementType();
2601 assert(Ty && "Could not get type information for store");
2602 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2604 SDVTList VTs = getVTList(MVT::Other);
2605 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2606 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2607 FoldingSetNodeID ID;
2608 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2609 ID.AddInteger(ISD::UNINDEXED);
2611 ID.AddInteger((unsigned int)SVT);
2612 ID.AddInteger(Alignment);
2613 ID.AddInteger(isVolatile);
2615 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2616 return SDOperand(E, 0);
2617 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
2618 SVT, SV, SVOffset, Alignment, isVolatile);
2619 CSEMap.InsertNode(N, IP);
2620 AllNodes.push_back(N);
2621 return SDOperand(N, 0);
2625 SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2626 SDOperand Offset, ISD::MemIndexedMode AM) {
2627 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2628 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2629 "Store is already a indexed store!");
2630 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2631 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2632 FoldingSetNodeID ID;
2633 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2635 ID.AddInteger(ST->isTruncatingStore());
2636 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
2637 ID.AddInteger(ST->getAlignment());
2638 ID.AddInteger(ST->isVolatile());
2640 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2641 return SDOperand(E, 0);
2642 SDNode *N = new StoreSDNode(Ops, VTs, AM,
2643 ST->isTruncatingStore(), ST->getMemoryVT(),
2644 ST->getSrcValue(), ST->getSrcValueOffset(),
2645 ST->getAlignment(), ST->isVolatile());
2646 CSEMap.InsertNode(N, IP);
2647 AllNodes.push_back(N);
2648 return SDOperand(N, 0);
2651 SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2652 SDOperand Chain, SDOperand Ptr,
2654 SDOperand Ops[] = { Chain, Ptr, SV };
2655 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
2658 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2659 const SDOperand *Ops, unsigned NumOps) {
2661 case 0: return getNode(Opcode, VT);
2662 case 1: return getNode(Opcode, VT, Ops[0]);
2663 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2664 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
2670 case ISD::SELECT_CC: {
2671 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
2672 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2673 "LHS and RHS of condition must have same type!");
2674 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2675 "True and False arms of SelectCC must have same type!");
2676 assert(Ops[2].getValueType() == VT &&
2677 "select_cc node must be of same type as true and false value!");
2681 assert(NumOps == 5 && "BR_CC takes 5 operands!");
2682 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2683 "LHS/RHS of comparison should match types!");
2690 SDVTList VTs = getVTList(VT);
2691 if (VT != MVT::Flag) {
2692 FoldingSetNodeID ID;
2693 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
2695 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2696 return SDOperand(E, 0);
2697 N = new SDNode(Opcode, VTs, Ops, NumOps);
2698 CSEMap.InsertNode(N, IP);
2700 N = new SDNode(Opcode, VTs, Ops, NumOps);
2702 AllNodes.push_back(N);
2703 return SDOperand(N, 0);
2706 SDOperand SelectionDAG::getNode(unsigned Opcode,
2707 std::vector<MVT::ValueType> &ResultTys,
2708 const SDOperand *Ops, unsigned NumOps) {
2709 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2713 SDOperand SelectionDAG::getNode(unsigned Opcode,
2714 const MVT::ValueType *VTs, unsigned NumVTs,
2715 const SDOperand *Ops, unsigned NumOps) {
2717 return getNode(Opcode, VTs[0], Ops, NumOps);
2718 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2721 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2722 const SDOperand *Ops, unsigned NumOps) {
2723 if (VTList.NumVTs == 1)
2724 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
2727 // FIXME: figure out how to safely handle things like
2728 // int foo(int x) { return 1 << (x & 255); }
2729 // int bar() { return foo(256); }
2731 case ISD::SRA_PARTS:
2732 case ISD::SRL_PARTS:
2733 case ISD::SHL_PARTS:
2734 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2735 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
2736 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2737 else if (N3.getOpcode() == ISD::AND)
2738 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2739 // If the and is only masking out bits that cannot effect the shift,
2740 // eliminate the and.
2741 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2742 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2743 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2749 // Memoize the node unless it returns a flag.
2751 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
2752 FoldingSetNodeID ID;
2753 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
2755 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2756 return SDOperand(E, 0);
2758 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2759 else if (NumOps == 2)
2760 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2761 else if (NumOps == 3)
2762 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2764 N = new SDNode(Opcode, VTList, Ops, NumOps);
2765 CSEMap.InsertNode(N, IP);
2768 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2769 else if (NumOps == 2)
2770 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2771 else if (NumOps == 3)
2772 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2774 N = new SDNode(Opcode, VTList, Ops, NumOps);
2776 AllNodes.push_back(N);
2777 return SDOperand(N, 0);
2780 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2781 return getNode(Opcode, VTList, 0, 0);
2784 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2786 SDOperand Ops[] = { N1 };
2787 return getNode(Opcode, VTList, Ops, 1);
2790 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2791 SDOperand N1, SDOperand N2) {
2792 SDOperand Ops[] = { N1, N2 };
2793 return getNode(Opcode, VTList, Ops, 2);
2796 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2797 SDOperand N1, SDOperand N2, SDOperand N3) {
2798 SDOperand Ops[] = { N1, N2, N3 };
2799 return getNode(Opcode, VTList, Ops, 3);
2802 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2803 SDOperand N1, SDOperand N2, SDOperand N3,
2805 SDOperand Ops[] = { N1, N2, N3, N4 };
2806 return getNode(Opcode, VTList, Ops, 4);
2809 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2810 SDOperand N1, SDOperand N2, SDOperand N3,
2811 SDOperand N4, SDOperand N5) {
2812 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2813 return getNode(Opcode, VTList, Ops, 5);
2816 SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
2817 return makeVTList(SDNode::getValueTypeList(VT), 1);
2820 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
2821 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2822 E = VTList.end(); I != E; ++I) {
2823 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
2824 return makeVTList(&(*I)[0], 2);
2826 std::vector<MVT::ValueType> V;
2829 VTList.push_front(V);
2830 return makeVTList(&(*VTList.begin())[0], 2);
2832 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2833 MVT::ValueType VT3) {
2834 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2835 E = VTList.end(); I != E; ++I) {
2836 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2838 return makeVTList(&(*I)[0], 3);
2840 std::vector<MVT::ValueType> V;
2844 VTList.push_front(V);
2845 return makeVTList(&(*VTList.begin())[0], 3);
2848 SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2850 case 0: assert(0 && "Cannot have nodes without results!");
2851 case 1: return getVTList(VTs[0]);
2852 case 2: return getVTList(VTs[0], VTs[1]);
2853 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2857 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2858 E = VTList.end(); I != E; ++I) {
2859 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2861 bool NoMatch = false;
2862 for (unsigned i = 2; i != NumVTs; ++i)
2863 if (VTs[i] != (*I)[i]) {
2868 return makeVTList(&*I->begin(), NumVTs);
2871 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2872 return makeVTList(&*VTList.begin()->begin(), NumVTs);
2876 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2877 /// specified operands. If the resultant node already exists in the DAG,
2878 /// this does not modify the specified node, instead it returns the node that
2879 /// already exists. If the resultant node does not exist in the DAG, the
2880 /// input node is returned. As a degenerate case, if you specify the same
2881 /// input operands as the node already has, the input node is returned.
2882 SDOperand SelectionDAG::
2883 UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2884 SDNode *N = InN.Val;
2885 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2887 // Check to see if there is no change.
2888 if (Op == N->getOperand(0)) return InN;
2890 // See if the modified node already exists.
2891 void *InsertPos = 0;
2892 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2893 return SDOperand(Existing, InN.ResNo);
2895 // Nope it doesn't. Remove the node from it's current place in the maps.
2897 RemoveNodeFromCSEMaps(N);
2899 // Now we update the operands.
2900 N->OperandList[0].Val->removeUser(N);
2902 N->OperandList[0] = Op;
2904 // If this gets put into a CSE map, add it.
2905 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2909 SDOperand SelectionDAG::
2910 UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2911 SDNode *N = InN.Val;
2912 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2914 // Check to see if there is no change.
2915 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2916 return InN; // No operands changed, just return the input node.
2918 // See if the modified node already exists.
2919 void *InsertPos = 0;
2920 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2921 return SDOperand(Existing, InN.ResNo);
2923 // Nope it doesn't. Remove the node from it's current place in the maps.
2925 RemoveNodeFromCSEMaps(N);
2927 // Now we update the operands.
2928 if (N->OperandList[0] != Op1) {
2929 N->OperandList[0].Val->removeUser(N);
2930 Op1.Val->addUser(N);
2931 N->OperandList[0] = Op1;
2933 if (N->OperandList[1] != Op2) {
2934 N->OperandList[1].Val->removeUser(N);
2935 Op2.Val->addUser(N);
2936 N->OperandList[1] = Op2;
2939 // If this gets put into a CSE map, add it.
2940 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2944 SDOperand SelectionDAG::
2945 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2946 SDOperand Ops[] = { Op1, Op2, Op3 };
2947 return UpdateNodeOperands(N, Ops, 3);
2950 SDOperand SelectionDAG::
2951 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2952 SDOperand Op3, SDOperand Op4) {
2953 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2954 return UpdateNodeOperands(N, Ops, 4);
2957 SDOperand SelectionDAG::
2958 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2959 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2960 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2961 return UpdateNodeOperands(N, Ops, 5);
2965 SDOperand SelectionDAG::
2966 UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
2967 SDNode *N = InN.Val;
2968 assert(N->getNumOperands() == NumOps &&
2969 "Update with wrong number of operands");
2971 // Check to see if there is no change.
2972 bool AnyChange = false;
2973 for (unsigned i = 0; i != NumOps; ++i) {
2974 if (Ops[i] != N->getOperand(i)) {
2980 // No operands changed, just return the input node.
2981 if (!AnyChange) return InN;
2983 // See if the modified node already exists.
2984 void *InsertPos = 0;
2985 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
2986 return SDOperand(Existing, InN.ResNo);
2988 // Nope it doesn't. Remove the node from it's current place in the maps.
2990 RemoveNodeFromCSEMaps(N);
2992 // Now we update the operands.
2993 for (unsigned i = 0; i != NumOps; ++i) {
2994 if (N->OperandList[i] != Ops[i]) {
2995 N->OperandList[i].Val->removeUser(N);
2996 Ops[i].Val->addUser(N);
2997 N->OperandList[i] = Ops[i];
3001 // If this gets put into a CSE map, add it.
3002 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
3007 /// MorphNodeTo - This frees the operands of the current node, resets the
3008 /// opcode, types, and operands to the specified value. This should only be
3009 /// used by the SelectionDAG class.
3010 void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
3011 const SDOperand *Ops, unsigned NumOps) {
3014 NumValues = L.NumVTs;
3016 // Clear the operands list, updating used nodes to remove this from their
3018 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3019 I->Val->removeUser(this);
3021 // If NumOps is larger than the # of operands we currently have, reallocate
3022 // the operand list.
3023 if (NumOps > NumOperands) {
3024 if (OperandsNeedDelete)
3025 delete [] OperandList;
3026 OperandList = new SDOperand[NumOps];
3027 OperandsNeedDelete = true;
3030 // Assign the new operands.
3031 NumOperands = NumOps;
3033 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3034 OperandList[i] = Ops[i];
3035 SDNode *N = OperandList[i].Val;
3036 N->Uses.push_back(this);
3040 /// SelectNodeTo - These are used for target selectors to *mutate* the
3041 /// specified node to have the specified return type, Target opcode, and
3042 /// operands. Note that target opcodes are stored as
3043 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
3045 /// Note that SelectNodeTo returns the resultant node. If there is already a
3046 /// node of the specified opcode and operands, it returns that node instead of
3047 /// the current one.
3048 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3049 MVT::ValueType VT) {
3050 SDVTList VTs = getVTList(VT);
3051 FoldingSetNodeID ID;
3052 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
3054 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3057 RemoveNodeFromCSEMaps(N);
3059 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
3061 CSEMap.InsertNode(N, IP);
3065 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3066 MVT::ValueType VT, SDOperand Op1) {
3067 // If an identical node already exists, use it.
3068 SDVTList VTs = getVTList(VT);
3069 SDOperand Ops[] = { Op1 };
3071 FoldingSetNodeID ID;
3072 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
3074 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3077 RemoveNodeFromCSEMaps(N);
3078 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
3079 CSEMap.InsertNode(N, IP);
3083 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3084 MVT::ValueType VT, SDOperand Op1,
3086 // If an identical node already exists, use it.
3087 SDVTList VTs = getVTList(VT);
3088 SDOperand Ops[] = { Op1, Op2 };
3090 FoldingSetNodeID ID;
3091 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3093 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3096 RemoveNodeFromCSEMaps(N);
3098 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3100 CSEMap.InsertNode(N, IP); // Memoize the new node.
3104 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3105 MVT::ValueType VT, SDOperand Op1,
3106 SDOperand Op2, SDOperand Op3) {
3107 // If an identical node already exists, use it.
3108 SDVTList VTs = getVTList(VT);
3109 SDOperand Ops[] = { Op1, Op2, Op3 };
3110 FoldingSetNodeID ID;
3111 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3113 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3116 RemoveNodeFromCSEMaps(N);
3118 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3120 CSEMap.InsertNode(N, IP); // Memoize the new node.
3124 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3125 MVT::ValueType VT, const SDOperand *Ops,
3127 // If an identical node already exists, use it.
3128 SDVTList VTs = getVTList(VT);
3129 FoldingSetNodeID ID;
3130 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
3132 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3135 RemoveNodeFromCSEMaps(N);
3136 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
3138 CSEMap.InsertNode(N, IP); // Memoize the new node.
3142 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3143 MVT::ValueType VT1, MVT::ValueType VT2,
3144 SDOperand Op1, SDOperand Op2) {
3145 SDVTList VTs = getVTList(VT1, VT2);
3146 FoldingSetNodeID ID;
3147 SDOperand Ops[] = { Op1, Op2 };
3148 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3150 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3153 RemoveNodeFromCSEMaps(N);
3154 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3155 CSEMap.InsertNode(N, IP); // Memoize the new node.
3159 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3160 MVT::ValueType VT1, MVT::ValueType VT2,
3161 SDOperand Op1, SDOperand Op2,
3163 // If an identical node already exists, use it.
3164 SDVTList VTs = getVTList(VT1, VT2);
3165 SDOperand Ops[] = { Op1, Op2, Op3 };
3166 FoldingSetNodeID ID;
3167 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3169 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3172 RemoveNodeFromCSEMaps(N);
3174 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3175 CSEMap.InsertNode(N, IP); // Memoize the new node.
3180 /// getTargetNode - These are used for target selectors to create a new node
3181 /// with specified return type(s), target opcode, and operands.
3183 /// Note that getTargetNode returns the resultant node. If there is already a
3184 /// node of the specified opcode and operands, it returns that node instead of
3185 /// the current one.
3186 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3187 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3189 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3191 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3193 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3194 SDOperand Op1, SDOperand Op2) {
3195 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3197 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3198 SDOperand Op1, SDOperand Op2,
3200 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3202 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3203 const SDOperand *Ops, unsigned NumOps) {
3204 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
3206 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3207 MVT::ValueType VT2) {
3208 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3210 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3212 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3213 MVT::ValueType VT2, SDOperand Op1) {
3214 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3215 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
3217 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3218 MVT::ValueType VT2, SDOperand Op1,
3220 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3221 SDOperand Ops[] = { Op1, Op2 };
3222 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
3224 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3225 MVT::ValueType VT2, SDOperand Op1,
3226 SDOperand Op2, SDOperand Op3) {
3227 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3228 SDOperand Ops[] = { Op1, Op2, Op3 };
3229 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
3231 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3233 const SDOperand *Ops, unsigned NumOps) {
3234 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3235 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
3237 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3238 MVT::ValueType VT2, MVT::ValueType VT3,
3239 SDOperand Op1, SDOperand Op2) {
3240 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3241 SDOperand Ops[] = { Op1, Op2 };
3242 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
3244 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3245 MVT::ValueType VT2, MVT::ValueType VT3,
3246 SDOperand Op1, SDOperand Op2,
3248 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3249 SDOperand Ops[] = { Op1, Op2, Op3 };
3250 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3252 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3253 MVT::ValueType VT2, MVT::ValueType VT3,
3254 const SDOperand *Ops, unsigned NumOps) {
3255 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3256 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
3258 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3259 MVT::ValueType VT2, MVT::ValueType VT3,
3261 const SDOperand *Ops, unsigned NumOps) {
3262 std::vector<MVT::ValueType> VTList;
3263 VTList.push_back(VT1);
3264 VTList.push_back(VT2);
3265 VTList.push_back(VT3);
3266 VTList.push_back(VT4);
3267 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3268 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3270 SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3271 std::vector<MVT::ValueType> &ResultTys,
3272 const SDOperand *Ops, unsigned NumOps) {
3273 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3274 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3279 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3280 /// This can cause recursive merging of nodes in the DAG.
3282 /// This version assumes From has a single result value.
3284 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
3285 DAGUpdateListener *UpdateListener) {
3286 SDNode *From = FromN.Val;
3287 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
3288 "Cannot replace with this method!");
3289 assert(From != To.Val && "Cannot replace uses of with self");
3291 while (!From->use_empty()) {
3292 // Process users until they are all gone.
3293 SDNode *U = *From->use_begin();
3295 // This node is about to morph, remove its old self from the CSE maps.
3296 RemoveNodeFromCSEMaps(U);
3298 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3300 if (I->Val == From) {
3301 From->removeUser(U);
3306 // Now that we have modified U, add it back to the CSE maps. If it already
3307 // exists there, recursively merge the results together.
3308 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3309 ReplaceAllUsesWith(U, Existing, UpdateListener);
3310 // U is now dead. Inform the listener if it exists and delete it.
3312 UpdateListener->NodeDeleted(U);
3313 DeleteNodeNotInCSEMaps(U);
3315 // If the node doesn't already exist, we updated it. Inform a listener if
3318 UpdateListener->NodeUpdated(U);
3323 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3324 /// This can cause recursive merging of nodes in the DAG.
3326 /// This version assumes From/To have matching types and numbers of result
3329 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3330 DAGUpdateListener *UpdateListener) {
3331 assert(From != To && "Cannot replace uses of with self");
3332 assert(From->getNumValues() == To->getNumValues() &&
3333 "Cannot use this version of ReplaceAllUsesWith!");
3334 if (From->getNumValues() == 1) // If possible, use the faster version.
3335 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3338 while (!From->use_empty()) {
3339 // Process users until they are all gone.
3340 SDNode *U = *From->use_begin();
3342 // This node is about to morph, remove its old self from the CSE maps.
3343 RemoveNodeFromCSEMaps(U);
3345 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3347 if (I->Val == From) {
3348 From->removeUser(U);
3353 // Now that we have modified U, add it back to the CSE maps. If it already
3354 // exists there, recursively merge the results together.
3355 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3356 ReplaceAllUsesWith(U, Existing, UpdateListener);
3357 // U is now dead. Inform the listener if it exists and delete it.
3359 UpdateListener->NodeDeleted(U);
3360 DeleteNodeNotInCSEMaps(U);
3362 // If the node doesn't already exist, we updated it. Inform a listener if
3365 UpdateListener->NodeUpdated(U);
3370 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3371 /// This can cause recursive merging of nodes in the DAG.
3373 /// This version can replace From with any result values. To must match the
3374 /// number and types of values returned by From.
3375 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
3376 const SDOperand *To,
3377 DAGUpdateListener *UpdateListener) {
3378 if (From->getNumValues() == 1) // Handle the simple case efficiently.
3379 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
3381 while (!From->use_empty()) {
3382 // Process users until they are all gone.
3383 SDNode *U = *From->use_begin();
3385 // This node is about to morph, remove its old self from the CSE maps.
3386 RemoveNodeFromCSEMaps(U);
3388 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3390 if (I->Val == From) {
3391 const SDOperand &ToOp = To[I->ResNo];
3392 From->removeUser(U);
3394 ToOp.Val->addUser(U);
3397 // Now that we have modified U, add it back to the CSE maps. If it already
3398 // exists there, recursively merge the results together.
3399 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3400 ReplaceAllUsesWith(U, Existing, UpdateListener);
3401 // U is now dead. Inform the listener if it exists and delete it.
3403 UpdateListener->NodeDeleted(U);
3404 DeleteNodeNotInCSEMaps(U);
3406 // If the node doesn't already exist, we updated it. Inform a listener if
3409 UpdateListener->NodeUpdated(U);
3415 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
3416 /// any deleted nodes from the set passed into its constructor and recursively
3417 /// notifies another update listener if specified.
3418 class ChainedSetUpdaterListener :
3419 public SelectionDAG::DAGUpdateListener {
3420 SmallSetVector<SDNode*, 16> &Set;
3421 SelectionDAG::DAGUpdateListener *Chain;
3423 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
3424 SelectionDAG::DAGUpdateListener *chain)
3425 : Set(set), Chain(chain) {}
3427 virtual void NodeDeleted(SDNode *N) {
3429 if (Chain) Chain->NodeDeleted(N);
3431 virtual void NodeUpdated(SDNode *N) {
3432 if (Chain) Chain->NodeUpdated(N);
3437 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3438 /// uses of other values produced by From.Val alone. The Deleted vector is
3439 /// handled the same way as for ReplaceAllUsesWith.
3440 void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
3441 DAGUpdateListener *UpdateListener){
3442 assert(From != To && "Cannot replace a value with itself");
3444 // Handle the simple, trivial, case efficiently.
3445 if (From.Val->getNumValues() == 1) {
3446 ReplaceAllUsesWith(From, To, UpdateListener);
3450 if (From.use_empty()) return;
3452 // Get all of the users of From.Val. We want these in a nice,
3453 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3454 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
3456 // When one of the recursive merges deletes nodes from the graph, we need to
3457 // make sure that UpdateListener is notified *and* that the node is removed
3458 // from Users if present. CSUL does this.
3459 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
3461 while (!Users.empty()) {
3462 // We know that this user uses some value of From. If it is the right
3463 // value, update it.
3464 SDNode *User = Users.back();
3467 // Scan for an operand that matches From.
3468 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3469 for (; Op != E; ++Op)
3470 if (*Op == From) break;
3472 // If there are no matches, the user must use some other result of From.
3473 if (Op == E) continue;
3475 // Okay, we know this user needs to be updated. Remove its old self
3476 // from the CSE maps.
3477 RemoveNodeFromCSEMaps(User);
3479 // Update all operands that match "From" in case there are multiple uses.
3480 for (; Op != E; ++Op) {
3482 From.Val->removeUser(User);
3484 To.Val->addUser(User);
3488 // Now that we have modified User, add it back to the CSE maps. If it
3489 // already exists there, recursively merge the results together.
3490 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
3492 if (UpdateListener) UpdateListener->NodeUpdated(User);
3493 continue; // Continue on to next user.
3496 // If there was already an existing matching node, use ReplaceAllUsesWith
3497 // to replace the dead one with the existing one. This can cause
3498 // recursive merging of other unrelated nodes down the line. The merging
3499 // can cause deletion of nodes that used the old value. To handle this, we
3500 // use CSUL to remove them from the Users set.
3501 ReplaceAllUsesWith(User, Existing, &CSUL);
3503 // User is now dead. Notify a listener if present.
3504 if (UpdateListener) UpdateListener->NodeDeleted(User);
3505 DeleteNodeNotInCSEMaps(User);
3510 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3511 /// their allnodes order. It returns the maximum id.
3512 unsigned SelectionDAG::AssignNodeIds() {
3514 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3521 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
3522 /// based on their topological order. It returns the maximum id and a vector
3523 /// of the SDNodes* in assigned order by reference.
3524 unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
3525 unsigned DAGSize = AllNodes.size();
3526 std::vector<unsigned> InDegree(DAGSize);
3527 std::vector<SDNode*> Sources;
3529 // Use a two pass approach to avoid using a std::map which is slow.
3531 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3534 unsigned Degree = N->use_size();
3535 InDegree[N->getNodeId()] = Degree;
3537 Sources.push_back(N);
3541 while (!Sources.empty()) {
3542 SDNode *N = Sources.back();
3544 TopOrder.push_back(N);
3545 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3547 unsigned Degree = --InDegree[P->getNodeId()];
3549 Sources.push_back(P);
3553 // Second pass, assign the actual topological order as node ids.
3555 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3557 (*TI)->setNodeId(Id++);
3564 //===----------------------------------------------------------------------===//
3566 //===----------------------------------------------------------------------===//
3568 // Out-of-line virtual method to give class a home.
3569 void SDNode::ANCHOR() {}
3570 void UnarySDNode::ANCHOR() {}
3571 void BinarySDNode::ANCHOR() {}
3572 void TernarySDNode::ANCHOR() {}
3573 void HandleSDNode::ANCHOR() {}
3574 void StringSDNode::ANCHOR() {}
3575 void ConstantSDNode::ANCHOR() {}
3576 void ConstantFPSDNode::ANCHOR() {}
3577 void GlobalAddressSDNode::ANCHOR() {}
3578 void FrameIndexSDNode::ANCHOR() {}
3579 void JumpTableSDNode::ANCHOR() {}
3580 void ConstantPoolSDNode::ANCHOR() {}
3581 void BasicBlockSDNode::ANCHOR() {}
3582 void SrcValueSDNode::ANCHOR() {}
3583 void MemOperandSDNode::ANCHOR() {}
3584 void RegisterSDNode::ANCHOR() {}
3585 void ExternalSymbolSDNode::ANCHOR() {}
3586 void CondCodeSDNode::ANCHOR() {}
3587 void VTSDNode::ANCHOR() {}
3588 void LoadSDNode::ANCHOR() {}
3589 void StoreSDNode::ANCHOR() {}
3590 void AtomicSDNode::ANCHOR() {}
3592 HandleSDNode::~HandleSDNode() {
3593 SDVTList VTs = { 0, 0 };
3594 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
3597 GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3598 MVT::ValueType VT, int o)
3599 : SDNode(isa<GlobalVariable>(GA) &&
3600 cast<GlobalVariable>(GA)->isThreadLocal() ?
3602 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3604 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3605 getSDVTList(VT)), Offset(o) {
3606 TheGlobal = const_cast<GlobalValue*>(GA);
3609 /// getMemOperand - Return a MemOperand object describing the memory
3610 /// reference performed by this load or store.
3611 MemOperand LSBaseSDNode::getMemOperand() const {
3612 int Size = (MVT::getSizeInBits(getMemoryVT()) + 7) >> 3;
3614 getOpcode() == ISD::LOAD ? MemOperand::MOLoad : MemOperand::MOStore;
3615 if (IsVolatile) Flags |= MemOperand::MOVolatile;
3617 // Check if the load references a frame index, and does not have
3619 const FrameIndexSDNode *FI =
3620 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
3621 if (!getSrcValue() && FI)
3622 return MemOperand(PseudoSourceValue::getFixedStack(), Flags,
3623 FI->getIndex(), Size, Alignment);
3625 return MemOperand(getSrcValue(), Flags,
3626 getSrcValueOffset(), Size, Alignment);
3629 /// Profile - Gather unique data for the node.
3631 void SDNode::Profile(FoldingSetNodeID &ID) {
3632 AddNodeIDNode(ID, this);
3635 /// getValueTypeList - Return a pointer to the specified value type.
3637 const MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
3638 if (MVT::isExtendedVT(VT)) {
3639 static std::set<MVT::ValueType> EVTs;
3640 return &(*EVTs.insert(VT).first);
3642 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3648 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3649 /// indicated value. This method ignores uses of other values defined by this
3651 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
3652 assert(Value < getNumValues() && "Bad value!");
3654 // If there is only one value, this is easy.
3655 if (getNumValues() == 1)
3656 return use_size() == NUses;
3657 if (use_size() < NUses) return false;
3659 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3661 SmallPtrSet<SDNode*, 32> UsersHandled;
3663 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3665 if (User->getNumOperands() == 1 ||
3666 UsersHandled.insert(User)) // First time we've seen this?
3667 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3668 if (User->getOperand(i) == TheValue) {
3670 return false; // too many uses
3675 // Found exactly the right number of uses?
3680 /// hasAnyUseOfValue - Return true if there are any use of the indicated
3681 /// value. This method ignores uses of other values defined by this operation.
3682 bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3683 assert(Value < getNumValues() && "Bad value!");
3685 if (use_empty()) return false;
3687 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3689 SmallPtrSet<SDNode*, 32> UsersHandled;
3691 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3693 if (User->getNumOperands() == 1 ||
3694 UsersHandled.insert(User)) // First time we've seen this?
3695 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3696 if (User->getOperand(i) == TheValue) {
3705 /// isOnlyUseOf - Return true if this node is the only use of N.
3707 bool SDNode::isOnlyUseOf(SDNode *N) const {
3709 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3720 /// isOperand - Return true if this node is an operand of N.
3722 bool SDOperand::isOperandOf(SDNode *N) const {
3723 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3724 if (*this == N->getOperand(i))
3729 bool SDNode::isOperandOf(SDNode *N) const {
3730 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3731 if (this == N->OperandList[i].Val)
3736 /// reachesChainWithoutSideEffects - Return true if this operand (which must
3737 /// be a chain) reaches the specified operand without crossing any
3738 /// side-effecting instructions. In practice, this looks through token
3739 /// factors and non-volatile loads. In order to remain efficient, this only
3740 /// looks a couple of nodes in, it does not do an exhaustive search.
3741 bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
3742 unsigned Depth) const {
3743 if (*this == Dest) return true;
3745 // Don't search too deeply, we just want to be able to see through
3746 // TokenFactor's etc.
3747 if (Depth == 0) return false;
3749 // If this is a token factor, all inputs to the TF happen in parallel. If any
3750 // of the operands of the TF reach dest, then we can do the xform.
3751 if (getOpcode() == ISD::TokenFactor) {
3752 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
3753 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
3758 // Loads don't have side effects, look through them.
3759 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
3760 if (!Ld->isVolatile())
3761 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
3767 static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
3768 SmallPtrSet<SDNode *, 32> &Visited) {
3769 if (found || !Visited.insert(N))
3772 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3773 SDNode *Op = N->getOperand(i).Val;
3778 findPredecessor(Op, P, found, Visited);
3782 /// isPredecessorOf - Return true if this node is a predecessor of N. This node
3783 /// is either an operand of N or it can be reached by recursively traversing
3784 /// up the operands.
3785 /// NOTE: this is an expensive method. Use it carefully.
3786 bool SDNode::isPredecessorOf(SDNode *N) const {
3787 SmallPtrSet<SDNode *, 32> Visited;
3789 findPredecessor(N, this, found, Visited);
3793 uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3794 assert(Num < NumOperands && "Invalid child # of SDNode!");
3795 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3798 std::string SDNode::getOperationName(const SelectionDAG *G) const {
3799 switch (getOpcode()) {
3801 if (getOpcode() < ISD::BUILTIN_OP_END)
3802 return "<<Unknown DAG Node>>";
3805 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
3806 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
3807 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
3809 TargetLowering &TLI = G->getTargetLoweringInfo();
3811 TLI.getTargetNodeName(getOpcode());
3812 if (Name) return Name;
3815 return "<<Unknown Target Node>>";
3818 case ISD::PREFETCH: return "Prefetch";
3819 case ISD::MEMBARRIER: return "MemBarrier";
3820 case ISD::ATOMIC_LCS: return "AtomicLCS";
3821 case ISD::ATOMIC_LAS: return "AtomicLAS";
3822 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
3823 case ISD::PCMARKER: return "PCMarker";
3824 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
3825 case ISD::SRCVALUE: return "SrcValue";
3826 case ISD::MEMOPERAND: return "MemOperand";
3827 case ISD::EntryToken: return "EntryToken";
3828 case ISD::TokenFactor: return "TokenFactor";
3829 case ISD::AssertSext: return "AssertSext";
3830 case ISD::AssertZext: return "AssertZext";
3832 case ISD::STRING: return "String";
3833 case ISD::BasicBlock: return "BasicBlock";
3834 case ISD::VALUETYPE: return "ValueType";
3835 case ISD::Register: return "Register";
3837 case ISD::Constant: return "Constant";
3838 case ISD::ConstantFP: return "ConstantFP";
3839 case ISD::GlobalAddress: return "GlobalAddress";
3840 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
3841 case ISD::FrameIndex: return "FrameIndex";
3842 case ISD::JumpTable: return "JumpTable";
3843 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
3844 case ISD::RETURNADDR: return "RETURNADDR";
3845 case ISD::FRAMEADDR: return "FRAMEADDR";
3846 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
3847 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3848 case ISD::EHSELECTION: return "EHSELECTION";
3849 case ISD::EH_RETURN: return "EH_RETURN";
3850 case ISD::ConstantPool: return "ConstantPool";
3851 case ISD::ExternalSymbol: return "ExternalSymbol";
3852 case ISD::INTRINSIC_WO_CHAIN: {
3853 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3854 return Intrinsic::getName((Intrinsic::ID)IID);
3856 case ISD::INTRINSIC_VOID:
3857 case ISD::INTRINSIC_W_CHAIN: {
3858 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
3859 return Intrinsic::getName((Intrinsic::ID)IID);
3862 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
3863 case ISD::TargetConstant: return "TargetConstant";
3864 case ISD::TargetConstantFP:return "TargetConstantFP";
3865 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
3866 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
3867 case ISD::TargetFrameIndex: return "TargetFrameIndex";
3868 case ISD::TargetJumpTable: return "TargetJumpTable";
3869 case ISD::TargetConstantPool: return "TargetConstantPool";
3870 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
3872 case ISD::CopyToReg: return "CopyToReg";
3873 case ISD::CopyFromReg: return "CopyFromReg";
3874 case ISD::UNDEF: return "undef";
3875 case ISD::MERGE_VALUES: return "merge_values";
3876 case ISD::INLINEASM: return "inlineasm";
3877 case ISD::LABEL: return "label";
3878 case ISD::DECLARE: return "declare";
3879 case ISD::HANDLENODE: return "handlenode";
3880 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
3881 case ISD::CALL: return "call";
3884 case ISD::FABS: return "fabs";
3885 case ISD::FNEG: return "fneg";
3886 case ISD::FSQRT: return "fsqrt";
3887 case ISD::FSIN: return "fsin";
3888 case ISD::FCOS: return "fcos";
3889 case ISD::FPOWI: return "fpowi";
3890 case ISD::FPOW: return "fpow";
3893 case ISD::ADD: return "add";
3894 case ISD::SUB: return "sub";
3895 case ISD::MUL: return "mul";
3896 case ISD::MULHU: return "mulhu";
3897 case ISD::MULHS: return "mulhs";
3898 case ISD::SDIV: return "sdiv";
3899 case ISD::UDIV: return "udiv";
3900 case ISD::SREM: return "srem";
3901 case ISD::UREM: return "urem";
3902 case ISD::SMUL_LOHI: return "smul_lohi";
3903 case ISD::UMUL_LOHI: return "umul_lohi";
3904 case ISD::SDIVREM: return "sdivrem";
3905 case ISD::UDIVREM: return "divrem";
3906 case ISD::AND: return "and";
3907 case ISD::OR: return "or";
3908 case ISD::XOR: return "xor";
3909 case ISD::SHL: return "shl";
3910 case ISD::SRA: return "sra";
3911 case ISD::SRL: return "srl";
3912 case ISD::ROTL: return "rotl";
3913 case ISD::ROTR: return "rotr";
3914 case ISD::FADD: return "fadd";
3915 case ISD::FSUB: return "fsub";
3916 case ISD::FMUL: return "fmul";
3917 case ISD::FDIV: return "fdiv";
3918 case ISD::FREM: return "frem";
3919 case ISD::FCOPYSIGN: return "fcopysign";
3920 case ISD::FGETSIGN: return "fgetsign";
3922 case ISD::SETCC: return "setcc";
3923 case ISD::SELECT: return "select";
3924 case ISD::SELECT_CC: return "select_cc";
3925 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
3926 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
3927 case ISD::CONCAT_VECTORS: return "concat_vectors";
3928 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
3929 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
3930 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
3931 case ISD::CARRY_FALSE: return "carry_false";
3932 case ISD::ADDC: return "addc";
3933 case ISD::ADDE: return "adde";
3934 case ISD::SUBC: return "subc";
3935 case ISD::SUBE: return "sube";
3936 case ISD::SHL_PARTS: return "shl_parts";
3937 case ISD::SRA_PARTS: return "sra_parts";
3938 case ISD::SRL_PARTS: return "srl_parts";
3940 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3941 case ISD::INSERT_SUBREG: return "insert_subreg";
3943 // Conversion operators.
3944 case ISD::SIGN_EXTEND: return "sign_extend";
3945 case ISD::ZERO_EXTEND: return "zero_extend";
3946 case ISD::ANY_EXTEND: return "any_extend";
3947 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
3948 case ISD::TRUNCATE: return "truncate";
3949 case ISD::FP_ROUND: return "fp_round";
3950 case ISD::FLT_ROUNDS_: return "flt_rounds";
3951 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
3952 case ISD::FP_EXTEND: return "fp_extend";
3954 case ISD::SINT_TO_FP: return "sint_to_fp";
3955 case ISD::UINT_TO_FP: return "uint_to_fp";
3956 case ISD::FP_TO_SINT: return "fp_to_sint";
3957 case ISD::FP_TO_UINT: return "fp_to_uint";
3958 case ISD::BIT_CONVERT: return "bit_convert";
3960 // Control flow instructions
3961 case ISD::BR: return "br";
3962 case ISD::BRIND: return "brind";
3963 case ISD::BR_JT: return "br_jt";
3964 case ISD::BRCOND: return "brcond";
3965 case ISD::BR_CC: return "br_cc";
3966 case ISD::RET: return "ret";
3967 case ISD::CALLSEQ_START: return "callseq_start";
3968 case ISD::CALLSEQ_END: return "callseq_end";
3971 case ISD::LOAD: return "load";
3972 case ISD::STORE: return "store";
3973 case ISD::VAARG: return "vaarg";
3974 case ISD::VACOPY: return "vacopy";
3975 case ISD::VAEND: return "vaend";
3976 case ISD::VASTART: return "vastart";
3977 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
3978 case ISD::EXTRACT_ELEMENT: return "extract_element";
3979 case ISD::BUILD_PAIR: return "build_pair";
3980 case ISD::STACKSAVE: return "stacksave";
3981 case ISD::STACKRESTORE: return "stackrestore";
3982 case ISD::TRAP: return "trap";
3984 // Block memory operations.
3985 case ISD::MEMSET: return "memset";
3986 case ISD::MEMCPY: return "memcpy";
3987 case ISD::MEMMOVE: return "memmove";
3990 case ISD::BSWAP: return "bswap";
3991 case ISD::CTPOP: return "ctpop";
3992 case ISD::CTTZ: return "cttz";
3993 case ISD::CTLZ: return "ctlz";
3996 case ISD::LOCATION: return "location";
3997 case ISD::DEBUG_LOC: return "debug_loc";
4000 case ISD::TRAMPOLINE: return "trampoline";
4003 switch (cast<CondCodeSDNode>(this)->get()) {
4004 default: assert(0 && "Unknown setcc condition!");
4005 case ISD::SETOEQ: return "setoeq";
4006 case ISD::SETOGT: return "setogt";
4007 case ISD::SETOGE: return "setoge";
4008 case ISD::SETOLT: return "setolt";
4009 case ISD::SETOLE: return "setole";
4010 case ISD::SETONE: return "setone";
4012 case ISD::SETO: return "seto";
4013 case ISD::SETUO: return "setuo";
4014 case ISD::SETUEQ: return "setue";
4015 case ISD::SETUGT: return "setugt";
4016 case ISD::SETUGE: return "setuge";
4017 case ISD::SETULT: return "setult";
4018 case ISD::SETULE: return "setule";
4019 case ISD::SETUNE: return "setune";
4021 case ISD::SETEQ: return "seteq";
4022 case ISD::SETGT: return "setgt";
4023 case ISD::SETGE: return "setge";
4024 case ISD::SETLT: return "setlt";
4025 case ISD::SETLE: return "setle";
4026 case ISD::SETNE: return "setne";
4031 const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
4040 return "<post-inc>";
4042 return "<post-dec>";
4046 void SDNode::dump() const { dump(0); }
4047 void SDNode::dump(const SelectionDAG *G) const {
4048 cerr << (void*)this << ": ";
4050 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
4052 if (getValueType(i) == MVT::Other)
4055 cerr << MVT::getValueTypeString(getValueType(i));
4057 cerr << " = " << getOperationName(G);
4060 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
4061 if (i) cerr << ", ";
4062 cerr << (void*)getOperand(i).Val;
4063 if (unsigned RN = getOperand(i).ResNo)
4067 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4068 SDNode *Mask = getOperand(2).Val;
4070 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4072 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4075 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4080 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
4081 cerr << "<" << CSDN->getValue() << ">";
4082 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
4083 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4084 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4085 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4086 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4088 cerr << "<APFloat(";
4089 CSDN->getValueAPF().convertToAPInt().dump();
4092 } else if (const GlobalAddressSDNode *GADN =
4093 dyn_cast<GlobalAddressSDNode>(this)) {
4094 int offset = GADN->getOffset();
4096 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
4098 cerr << " + " << offset;
4100 cerr << " " << offset;
4101 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
4102 cerr << "<" << FIDN->getIndex() << ">";
4103 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
4104 cerr << "<" << JTDN->getIndex() << ">";
4105 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
4106 int offset = CP->getOffset();
4107 if (CP->isMachineConstantPoolEntry())
4108 cerr << "<" << *CP->getMachineCPVal() << ">";
4110 cerr << "<" << *CP->getConstVal() << ">";
4112 cerr << " + " << offset;
4114 cerr << " " << offset;
4115 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
4117 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4119 cerr << LBB->getName() << " ";
4120 cerr << (const void*)BBDN->getBasicBlock() << ">";
4121 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
4122 if (G && R->getReg() &&
4123 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
4124 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
4126 cerr << " #" << R->getReg();
4128 } else if (const ExternalSymbolSDNode *ES =
4129 dyn_cast<ExternalSymbolSDNode>(this)) {
4130 cerr << "'" << ES->getSymbol() << "'";
4131 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4133 cerr << "<" << M->getValue() << ">";
4136 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4137 if (M->MO.getValue())
4138 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4140 cerr << "<null:" << M->MO.getOffset() << ">";
4141 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
4142 cerr << ":" << MVT::getValueTypeString(N->getVT());
4143 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
4144 const Value *SrcValue = LD->getSrcValue();
4145 int SrcOffset = LD->getSrcValueOffset();
4151 cerr << ":" << SrcOffset << ">";
4154 switch (LD->getExtensionType()) {
4155 default: doExt = false; break;
4157 cerr << " <anyext ";
4167 cerr << MVT::getValueTypeString(LD->getMemoryVT()) << ">";
4169 const char *AM = getIndexedModeName(LD->getAddressingMode());
4172 if (LD->isVolatile())
4173 cerr << " <volatile>";
4174 cerr << " alignment=" << LD->getAlignment();
4175 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
4176 const Value *SrcValue = ST->getSrcValue();
4177 int SrcOffset = ST->getSrcValueOffset();
4183 cerr << ":" << SrcOffset << ">";
4185 if (ST->isTruncatingStore())
4187 << MVT::getValueTypeString(ST->getMemoryVT()) << ">";
4189 const char *AM = getIndexedModeName(ST->getAddressingMode());
4192 if (ST->isVolatile())
4193 cerr << " <volatile>";
4194 cerr << " alignment=" << ST->getAlignment();
4198 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
4199 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4200 if (N->getOperand(i).Val->hasOneUse())
4201 DumpNodes(N->getOperand(i).Val, indent+2, G);
4203 cerr << "\n" << std::string(indent+2, ' ')
4204 << (void*)N->getOperand(i).Val << ": <multiple use>";
4207 cerr << "\n" << std::string(indent, ' ');
4211 void SelectionDAG::dump() const {
4212 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
4213 std::vector<const SDNode*> Nodes;
4214 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4218 std::sort(Nodes.begin(), Nodes.end());
4220 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
4221 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
4222 DumpNodes(Nodes[i], 2, this);
4225 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
4230 const Type *ConstantPoolSDNode::getType() const {
4231 if (isMachineConstantPoolEntry())
4232 return Val.MachineCPVal->getType();
4233 return Val.ConstVal->getType();