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.
359 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
361 case ISD::TargetConstant:
363 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
365 case ISD::TargetConstantFP:
366 case ISD::ConstantFP: {
367 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
370 case ISD::TargetGlobalAddress:
371 case ISD::GlobalAddress:
372 case ISD::TargetGlobalTLSAddress:
373 case ISD::GlobalTLSAddress: {
374 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
375 ID.AddPointer(GA->getGlobal());
376 ID.AddInteger(GA->getOffset());
379 case ISD::BasicBlock:
380 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
383 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
386 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
388 case ISD::MEMOPERAND: {
389 const MemOperand &MO = cast<MemOperandSDNode>(N)->MO;
390 ID.AddPointer(MO.getValue());
391 ID.AddInteger(MO.getFlags());
392 ID.AddInteger(MO.getOffset());
393 ID.AddInteger(MO.getSize());
394 ID.AddInteger(MO.getAlignment());
397 case ISD::FrameIndex:
398 case ISD::TargetFrameIndex:
399 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
402 case ISD::TargetJumpTable:
403 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
405 case ISD::ConstantPool:
406 case ISD::TargetConstantPool: {
407 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
408 ID.AddInteger(CP->getAlignment());
409 ID.AddInteger(CP->getOffset());
410 if (CP->isMachineConstantPoolEntry())
411 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
413 ID.AddPointer(CP->getConstVal());
417 LoadSDNode *LD = cast<LoadSDNode>(N);
418 ID.AddInteger(LD->getAddressingMode());
419 ID.AddInteger(LD->getExtensionType());
420 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
421 ID.AddInteger(LD->getAlignment());
422 ID.AddInteger(LD->isVolatile());
426 StoreSDNode *ST = cast<StoreSDNode>(N);
427 ID.AddInteger(ST->getAddressingMode());
428 ID.AddInteger(ST->isTruncatingStore());
429 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
430 ID.AddInteger(ST->getAlignment());
431 ID.AddInteger(ST->isVolatile());
437 //===----------------------------------------------------------------------===//
438 // SelectionDAG Class
439 //===----------------------------------------------------------------------===//
441 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
443 void SelectionDAG::RemoveDeadNodes() {
444 // Create a dummy node (which is not added to allnodes), that adds a reference
445 // to the root node, preventing it from being deleted.
446 HandleSDNode Dummy(getRoot());
448 SmallVector<SDNode*, 128> DeadNodes;
450 // Add all obviously-dead nodes to the DeadNodes worklist.
451 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
453 DeadNodes.push_back(I);
455 // Process the worklist, deleting the nodes and adding their uses to the
457 while (!DeadNodes.empty()) {
458 SDNode *N = DeadNodes.back();
459 DeadNodes.pop_back();
461 // Take the node out of the appropriate CSE map.
462 RemoveNodeFromCSEMaps(N);
464 // Next, brutally remove the operand list. This is safe to do, as there are
465 // no cycles in the graph.
466 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
467 SDNode *Operand = I->Val;
468 Operand->removeUser(N);
470 // Now that we removed this operand, see if there are no uses of it left.
471 if (Operand->use_empty())
472 DeadNodes.push_back(Operand);
474 if (N->OperandsNeedDelete)
475 delete[] N->OperandList;
479 // Finally, remove N itself.
483 // If the root changed (e.g. it was a dead load, update the root).
484 setRoot(Dummy.getValue());
487 void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
488 SmallVector<SDNode*, 16> DeadNodes;
489 DeadNodes.push_back(N);
491 // Process the worklist, deleting the nodes and adding their uses to the
493 while (!DeadNodes.empty()) {
494 SDNode *N = DeadNodes.back();
495 DeadNodes.pop_back();
498 UpdateListener->NodeDeleted(N);
500 // Take the node out of the appropriate CSE map.
501 RemoveNodeFromCSEMaps(N);
503 // Next, brutally remove the operand list. This is safe to do, as there are
504 // no cycles in the graph.
505 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
506 SDNode *Operand = I->Val;
507 Operand->removeUser(N);
509 // Now that we removed this operand, see if there are no uses of it left.
510 if (Operand->use_empty())
511 DeadNodes.push_back(Operand);
513 if (N->OperandsNeedDelete)
514 delete[] N->OperandList;
518 // Finally, remove N itself.
523 void SelectionDAG::DeleteNode(SDNode *N) {
524 assert(N->use_empty() && "Cannot delete a node that is not dead!");
526 // First take this out of the appropriate CSE map.
527 RemoveNodeFromCSEMaps(N);
529 // Finally, remove uses due to operands of this node, remove from the
530 // AllNodes list, and delete the node.
531 DeleteNodeNotInCSEMaps(N);
534 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
536 // Remove it from the AllNodes list.
539 // Drop all of the operands and decrement used nodes use counts.
540 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
541 I->Val->removeUser(N);
542 if (N->OperandsNeedDelete)
543 delete[] N->OperandList;
550 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
551 /// correspond to it. This is useful when we're about to delete or repurpose
552 /// the node. We don't want future request for structurally identical nodes
553 /// to return N anymore.
554 void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
556 switch (N->getOpcode()) {
557 case ISD::HANDLENODE: return; // noop.
559 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
562 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
563 "Cond code doesn't exist!");
564 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
565 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
567 case ISD::ExternalSymbol:
568 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
570 case ISD::TargetExternalSymbol:
572 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
574 case ISD::VALUETYPE: {
575 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
576 if (MVT::isExtendedVT(VT)) {
577 Erased = ExtendedValueTypeNodes.erase(VT);
579 Erased = ValueTypeNodes[VT] != 0;
580 ValueTypeNodes[VT] = 0;
585 // Remove it from the CSE Map.
586 Erased = CSEMap.RemoveNode(N);
590 // Verify that the node was actually in one of the CSE maps, unless it has a
591 // flag result (which cannot be CSE'd) or is one of the special cases that are
592 // not subject to CSE.
593 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
594 !N->isTargetOpcode()) {
597 assert(0 && "Node is not in map!");
602 /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
603 /// has been taken out and modified in some way. If the specified node already
604 /// exists in the CSE maps, do not modify the maps, but return the existing node
605 /// instead. If it doesn't exist, add it and return null.
607 SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
608 assert(N->getNumOperands() && "This is a leaf node!");
609 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
610 return 0; // Never add these nodes.
612 // Check that remaining values produced are not flags.
613 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
614 if (N->getValueType(i) == MVT::Flag)
615 return 0; // Never CSE anything that produces a flag.
617 SDNode *New = CSEMap.GetOrInsertNode(N);
618 if (New != N) return New; // Node already existed.
622 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
623 /// were replaced with those specified. If this node is never memoized,
624 /// return null, otherwise return a pointer to the slot it would take. If a
625 /// node already exists with these operands, the slot will be non-null.
626 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
628 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
629 return 0; // Never add these nodes.
631 // Check that remaining values produced are not flags.
632 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
633 if (N->getValueType(i) == MVT::Flag)
634 return 0; // Never CSE anything that produces a flag.
636 SDOperand Ops[] = { Op };
638 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
639 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
642 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
643 /// were replaced with those specified. If this node is never memoized,
644 /// return null, otherwise return a pointer to the slot it would take. If a
645 /// node already exists with these operands, the slot will be non-null.
646 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
647 SDOperand Op1, SDOperand Op2,
649 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
650 return 0; // Never add these nodes.
652 // Check that remaining values produced are not flags.
653 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
654 if (N->getValueType(i) == MVT::Flag)
655 return 0; // Never CSE anything that produces a flag.
657 SDOperand Ops[] = { Op1, Op2 };
659 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
660 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
664 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
665 /// were replaced with those specified. If this node is never memoized,
666 /// return null, otherwise return a pointer to the slot it would take. If a
667 /// node already exists with these operands, the slot will be non-null.
668 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
669 const SDOperand *Ops,unsigned NumOps,
671 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
672 return 0; // Never add these nodes.
674 // Check that remaining values produced are not flags.
675 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
676 if (N->getValueType(i) == MVT::Flag)
677 return 0; // Never CSE anything that produces a flag.
680 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
682 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
683 ID.AddInteger(LD->getAddressingMode());
684 ID.AddInteger(LD->getExtensionType());
685 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
686 ID.AddInteger(LD->getAlignment());
687 ID.AddInteger(LD->isVolatile());
688 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
689 ID.AddInteger(ST->getAddressingMode());
690 ID.AddInteger(ST->isTruncatingStore());
691 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
692 ID.AddInteger(ST->getAlignment());
693 ID.AddInteger(ST->isVolatile());
696 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
700 SelectionDAG::~SelectionDAG() {
701 while (!AllNodes.empty()) {
702 SDNode *N = AllNodes.begin();
703 N->SetNextInBucket(0);
704 if (N->OperandsNeedDelete)
705 delete [] N->OperandList;
708 AllNodes.pop_front();
712 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
713 if (Op.getValueType() == VT) return Op;
714 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
715 MVT::getSizeInBits(VT));
716 return getNode(ISD::AND, Op.getValueType(), Op,
717 getConstant(Imm, Op.getValueType()));
720 SDOperand SelectionDAG::getString(const std::string &Val) {
721 StringSDNode *&N = StringNodes[Val];
723 N = new StringSDNode(Val);
724 AllNodes.push_back(N);
726 return SDOperand(N, 0);
729 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
730 MVT::ValueType EltVT =
731 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
733 return getConstant(APInt(MVT::getSizeInBits(EltVT), Val), VT, isT);
736 SDOperand SelectionDAG::getConstant(const APInt &Val, MVT::ValueType VT, bool isT) {
737 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
739 MVT::ValueType EltVT =
740 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
742 assert(Val.getBitWidth() == MVT::getSizeInBits(EltVT) &&
743 "APInt size does not match type size!");
745 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
747 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
751 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
752 if (!MVT::isVector(VT))
753 return SDOperand(N, 0);
755 N = new ConstantSDNode(isT, Val, EltVT);
756 CSEMap.InsertNode(N, IP);
757 AllNodes.push_back(N);
760 SDOperand Result(N, 0);
761 if (MVT::isVector(VT)) {
762 SmallVector<SDOperand, 8> Ops;
763 Ops.assign(MVT::getVectorNumElements(VT), Result);
764 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
769 SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
770 return getConstant(Val, TLI.getPointerTy(), isTarget);
774 SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
776 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
778 MVT::ValueType EltVT =
779 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
781 // Do the map lookup using the actual bit pattern for the floating point
782 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
783 // we don't have issues with SNANs.
784 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
786 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
790 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
791 if (!MVT::isVector(VT))
792 return SDOperand(N, 0);
794 N = new ConstantFPSDNode(isTarget, V, EltVT);
795 CSEMap.InsertNode(N, IP);
796 AllNodes.push_back(N);
799 SDOperand Result(N, 0);
800 if (MVT::isVector(VT)) {
801 SmallVector<SDOperand, 8> Ops;
802 Ops.assign(MVT::getVectorNumElements(VT), Result);
803 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
808 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
810 MVT::ValueType EltVT =
811 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
813 return getConstantFP(APFloat((float)Val), VT, isTarget);
815 return getConstantFP(APFloat(Val), VT, isTarget);
818 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
819 MVT::ValueType VT, int Offset,
823 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
825 // If GV is an alias then use the aliasee for determining thread-localness.
826 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
827 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
830 if (GVar && GVar->isThreadLocal())
831 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
833 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
836 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
838 ID.AddInteger(Offset);
840 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
841 return SDOperand(E, 0);
842 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
843 CSEMap.InsertNode(N, IP);
844 AllNodes.push_back(N);
845 return SDOperand(N, 0);
848 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
850 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
852 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
855 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
856 return SDOperand(E, 0);
857 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
858 CSEMap.InsertNode(N, IP);
859 AllNodes.push_back(N);
860 return SDOperand(N, 0);
863 SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
864 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
866 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
869 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
870 return SDOperand(E, 0);
871 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
872 CSEMap.InsertNode(N, IP);
873 AllNodes.push_back(N);
874 return SDOperand(N, 0);
877 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
878 unsigned Alignment, int Offset,
880 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
882 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
883 ID.AddInteger(Alignment);
884 ID.AddInteger(Offset);
887 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
888 return SDOperand(E, 0);
889 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
890 CSEMap.InsertNode(N, IP);
891 AllNodes.push_back(N);
892 return SDOperand(N, 0);
896 SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
898 unsigned Alignment, int Offset,
900 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
902 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
903 ID.AddInteger(Alignment);
904 ID.AddInteger(Offset);
905 C->AddSelectionDAGCSEId(ID);
907 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
908 return SDOperand(E, 0);
909 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
910 CSEMap.InsertNode(N, IP);
911 AllNodes.push_back(N);
912 return SDOperand(N, 0);
916 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
918 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
921 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
922 return SDOperand(E, 0);
923 SDNode *N = new BasicBlockSDNode(MBB);
924 CSEMap.InsertNode(N, IP);
925 AllNodes.push_back(N);
926 return SDOperand(N, 0);
929 SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
931 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
932 ID.AddInteger(Flags.getRawBits());
934 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
935 return SDOperand(E, 0);
936 SDNode *N = new ARG_FLAGSSDNode(Flags);
937 CSEMap.InsertNode(N, IP);
938 AllNodes.push_back(N);
939 return SDOperand(N, 0);
942 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
943 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
944 ValueTypeNodes.resize(VT+1);
946 SDNode *&N = MVT::isExtendedVT(VT) ?
947 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
949 if (N) return SDOperand(N, 0);
950 N = new VTSDNode(VT);
951 AllNodes.push_back(N);
952 return SDOperand(N, 0);
955 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
956 SDNode *&N = ExternalSymbols[Sym];
957 if (N) return SDOperand(N, 0);
958 N = new ExternalSymbolSDNode(false, Sym, VT);
959 AllNodes.push_back(N);
960 return SDOperand(N, 0);
963 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
965 SDNode *&N = TargetExternalSymbols[Sym];
966 if (N) return SDOperand(N, 0);
967 N = new ExternalSymbolSDNode(true, Sym, VT);
968 AllNodes.push_back(N);
969 return SDOperand(N, 0);
972 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
973 if ((unsigned)Cond >= CondCodeNodes.size())
974 CondCodeNodes.resize(Cond+1);
976 if (CondCodeNodes[Cond] == 0) {
977 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
978 AllNodes.push_back(CondCodeNodes[Cond]);
980 return SDOperand(CondCodeNodes[Cond], 0);
983 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
985 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
986 ID.AddInteger(RegNo);
988 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
989 return SDOperand(E, 0);
990 SDNode *N = new RegisterSDNode(RegNo, VT);
991 CSEMap.InsertNode(N, IP);
992 AllNodes.push_back(N);
993 return SDOperand(N, 0);
996 SDOperand SelectionDAG::getSrcValue(const Value *V) {
997 assert((!V || isa<PointerType>(V->getType())) &&
998 "SrcValue is not a pointer?");
1000 FoldingSetNodeID ID;
1001 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
1005 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1006 return SDOperand(E, 0);
1008 SDNode *N = new SrcValueSDNode(V);
1009 CSEMap.InsertNode(N, IP);
1010 AllNodes.push_back(N);
1011 return SDOperand(N, 0);
1014 SDOperand SelectionDAG::getMemOperand(const MemOperand &MO) {
1015 const Value *v = MO.getValue();
1016 assert((!v || isa<PointerType>(v->getType())) &&
1017 "SrcValue is not a pointer?");
1019 FoldingSetNodeID ID;
1020 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
1022 ID.AddInteger(MO.getFlags());
1023 ID.AddInteger(MO.getOffset());
1024 ID.AddInteger(MO.getSize());
1025 ID.AddInteger(MO.getAlignment());
1028 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1029 return SDOperand(E, 0);
1031 SDNode *N = new MemOperandSDNode(MO);
1032 CSEMap.InsertNode(N, IP);
1033 AllNodes.push_back(N);
1034 return SDOperand(N, 0);
1037 /// CreateStackTemporary - Create a stack temporary, suitable for holding the
1038 /// specified value type.
1039 SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
1040 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1041 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
1042 const Type *Ty = MVT::getTypeForValueType(VT);
1043 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
1044 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1045 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1049 SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
1050 SDOperand N2, ISD::CondCode Cond) {
1051 // These setcc operations always fold.
1055 case ISD::SETFALSE2: return getConstant(0, VT);
1057 case ISD::SETTRUE2: return getConstant(1, VT);
1069 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
1073 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
1074 const APInt &C2 = N2C->getAPIntValue();
1075 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1076 const APInt &C1 = N1C->getAPIntValue();
1079 default: assert(0 && "Unknown integer setcc!");
1080 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1081 case ISD::SETNE: return getConstant(C1 != C2, VT);
1082 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1083 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1084 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1085 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1086 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1087 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1088 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1089 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
1093 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
1094 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
1095 // No compile time operations on this type yet.
1096 if (N1C->getValueType(0) == MVT::ppcf128)
1099 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
1102 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1103 return getNode(ISD::UNDEF, VT);
1105 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1106 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1107 return getNode(ISD::UNDEF, VT);
1109 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
1110 R==APFloat::cmpLessThan, VT);
1111 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1112 return getNode(ISD::UNDEF, VT);
1114 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1115 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1116 return getNode(ISD::UNDEF, VT);
1118 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1119 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1120 return getNode(ISD::UNDEF, VT);
1122 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
1123 R==APFloat::cmpEqual, VT);
1124 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1125 return getNode(ISD::UNDEF, VT);
1127 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
1128 R==APFloat::cmpEqual, VT);
1129 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1130 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1131 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1132 R==APFloat::cmpEqual, VT);
1133 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1134 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1135 R==APFloat::cmpLessThan, VT);
1136 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1137 R==APFloat::cmpUnordered, VT);
1138 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1139 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
1142 // Ensure that the constant occurs on the RHS.
1143 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
1147 // Could not fold it.
1151 /// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1152 /// use this predicate to simplify operations downstream.
1153 bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1154 unsigned BitWidth = Op.getValueSizeInBits();
1155 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1158 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1159 /// this predicate to simplify operations downstream. Mask is known to be zero
1160 /// for bits that V cannot have.
1161 bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
1162 unsigned Depth) const {
1163 APInt KnownZero, KnownOne;
1164 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1165 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1166 return (KnownZero & Mask) == Mask;
1169 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
1170 /// known to be either zero or one and return them in the KnownZero/KnownOne
1171 /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1173 void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
1174 APInt &KnownZero, APInt &KnownOne,
1175 unsigned Depth) const {
1176 unsigned BitWidth = Mask.getBitWidth();
1177 assert(BitWidth == MVT::getSizeInBits(Op.getValueType()) &&
1178 "Mask size mismatches value type size!");
1180 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
1181 if (Depth == 6 || Mask == 0)
1182 return; // Limit search depth.
1184 APInt KnownZero2, KnownOne2;
1186 switch (Op.getOpcode()) {
1188 // We know all of the bits for a constant!
1189 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
1190 KnownZero = ~KnownOne & Mask;
1193 // If either the LHS or the RHS are Zero, the result is zero.
1194 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1195 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1196 KnownZero2, KnownOne2, Depth+1);
1197 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1198 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1200 // Output known-1 bits are only known if set in both the LHS & RHS.
1201 KnownOne &= KnownOne2;
1202 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1203 KnownZero |= KnownZero2;
1206 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1207 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1208 KnownZero2, KnownOne2, Depth+1);
1209 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1210 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1212 // Output known-0 bits are only known if clear in both the LHS & RHS.
1213 KnownZero &= KnownZero2;
1214 // Output known-1 are known to be set if set in either the LHS | RHS.
1215 KnownOne |= KnownOne2;
1218 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1219 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1220 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1221 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1223 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1224 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1225 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1226 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1227 KnownZero = KnownZeroOut;
1231 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1232 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1233 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1234 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1236 // Only known if known in both the LHS and RHS.
1237 KnownOne &= KnownOne2;
1238 KnownZero &= KnownZero2;
1240 case ISD::SELECT_CC:
1241 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1242 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1243 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1244 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1246 // Only known if known in both the LHS and RHS.
1247 KnownOne &= KnownOne2;
1248 KnownZero &= KnownZero2;
1251 // If we know the result of a setcc has the top bits zero, use this info.
1252 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1254 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
1257 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1258 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1259 unsigned ShAmt = SA->getValue();
1261 // If the shift count is an invalid immediate, don't do anything.
1262 if (ShAmt >= BitWidth)
1265 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
1266 KnownZero, KnownOne, Depth+1);
1267 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1268 KnownZero <<= ShAmt;
1270 // low bits known zero.
1271 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
1275 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1276 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1277 unsigned ShAmt = SA->getValue();
1279 // If the shift count is an invalid immediate, don't do anything.
1280 if (ShAmt >= BitWidth)
1283 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
1284 KnownZero, KnownOne, Depth+1);
1285 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1286 KnownZero = KnownZero.lshr(ShAmt);
1287 KnownOne = KnownOne.lshr(ShAmt);
1289 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1290 KnownZero |= HighBits; // High bits known zero.
1294 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1295 unsigned ShAmt = SA->getValue();
1297 // If the shift count is an invalid immediate, don't do anything.
1298 if (ShAmt >= BitWidth)
1301 APInt InDemandedMask = (Mask << ShAmt);
1302 // If any of the demanded bits are produced by the sign extension, we also
1303 // demand the input sign bit.
1304 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1305 if (HighBits.getBoolValue())
1306 InDemandedMask |= APInt::getSignBit(BitWidth);
1308 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1310 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1311 KnownZero = KnownZero.lshr(ShAmt);
1312 KnownOne = KnownOne.lshr(ShAmt);
1314 // Handle the sign bits.
1315 APInt SignBit = APInt::getSignBit(BitWidth);
1316 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
1318 if (KnownZero.intersects(SignBit)) {
1319 KnownZero |= HighBits; // New bits are known zero.
1320 } else if (KnownOne.intersects(SignBit)) {
1321 KnownOne |= HighBits; // New bits are known one.
1325 case ISD::SIGN_EXTEND_INREG: {
1326 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1327 unsigned EBits = MVT::getSizeInBits(EVT);
1329 // Sign extension. Compute the demanded bits in the result that are not
1330 // present in the input.
1331 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
1333 APInt InSignBit = APInt::getSignBit(EBits);
1334 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
1336 // If the sign extended bits are demanded, we know that the sign
1338 InSignBit.zext(BitWidth);
1339 if (NewBits.getBoolValue())
1340 InputDemandedBits |= InSignBit;
1342 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1343 KnownZero, KnownOne, Depth+1);
1344 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1346 // If the sign bit of the input is known set or clear, then we know the
1347 // top bits of the result.
1348 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
1349 KnownZero |= NewBits;
1350 KnownOne &= ~NewBits;
1351 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
1352 KnownOne |= NewBits;
1353 KnownZero &= ~NewBits;
1354 } else { // Input sign bit unknown
1355 KnownZero &= ~NewBits;
1356 KnownOne &= ~NewBits;
1363 unsigned LowBits = Log2_32(BitWidth)+1;
1364 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
1365 KnownOne = APInt(BitWidth, 0);
1369 if (ISD::isZEXTLoad(Op.Val)) {
1370 LoadSDNode *LD = cast<LoadSDNode>(Op);
1371 MVT::ValueType VT = LD->getMemoryVT();
1372 unsigned MemBits = MVT::getSizeInBits(VT);
1373 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
1377 case ISD::ZERO_EXTEND: {
1378 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1379 unsigned InBits = MVT::getSizeInBits(InVT);
1380 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1381 APInt InMask = Mask;
1382 InMask.trunc(InBits);
1383 KnownZero.trunc(InBits);
1384 KnownOne.trunc(InBits);
1385 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1386 KnownZero.zext(BitWidth);
1387 KnownOne.zext(BitWidth);
1388 KnownZero |= NewBits;
1391 case ISD::SIGN_EXTEND: {
1392 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1393 unsigned InBits = MVT::getSizeInBits(InVT);
1394 APInt InSignBit = APInt::getSignBit(InBits);
1395 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1396 APInt InMask = Mask;
1397 InMask.trunc(InBits);
1399 // If any of the sign extended bits are demanded, we know that the sign
1400 // bit is demanded. Temporarily set this bit in the mask for our callee.
1401 if (NewBits.getBoolValue())
1402 InMask |= InSignBit;
1404 KnownZero.trunc(InBits);
1405 KnownOne.trunc(InBits);
1406 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1408 // Note if the sign bit is known to be zero or one.
1409 bool SignBitKnownZero = KnownZero.isNegative();
1410 bool SignBitKnownOne = KnownOne.isNegative();
1411 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1412 "Sign bit can't be known to be both zero and one!");
1414 // If the sign bit wasn't actually demanded by our caller, we don't
1415 // want it set in the KnownZero and KnownOne result values. Reset the
1416 // mask and reapply it to the result values.
1418 InMask.trunc(InBits);
1419 KnownZero &= InMask;
1422 KnownZero.zext(BitWidth);
1423 KnownOne.zext(BitWidth);
1425 // If the sign bit is known zero or one, the top bits match.
1426 if (SignBitKnownZero)
1427 KnownZero |= NewBits;
1428 else if (SignBitKnownOne)
1429 KnownOne |= NewBits;
1432 case ISD::ANY_EXTEND: {
1433 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1434 unsigned InBits = MVT::getSizeInBits(InVT);
1435 APInt InMask = Mask;
1436 InMask.trunc(InBits);
1437 KnownZero.trunc(InBits);
1438 KnownOne.trunc(InBits);
1439 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1440 KnownZero.zext(BitWidth);
1441 KnownOne.zext(BitWidth);
1444 case ISD::TRUNCATE: {
1445 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1446 unsigned InBits = MVT::getSizeInBits(InVT);
1447 APInt InMask = Mask;
1448 InMask.zext(InBits);
1449 KnownZero.zext(InBits);
1450 KnownOne.zext(InBits);
1451 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1452 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1453 KnownZero.trunc(BitWidth);
1454 KnownOne.trunc(BitWidth);
1457 case ISD::AssertZext: {
1458 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1459 APInt InMask = APInt::getLowBitsSet(BitWidth, MVT::getSizeInBits(VT));
1460 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1462 KnownZero |= (~InMask) & Mask;
1466 // All bits are zero except the low bit.
1467 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
1471 // If either the LHS or the RHS are Zero, the result is zero.
1472 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1473 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1474 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1475 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1477 // Output known-0 bits are known if clear or set in both the low clear bits
1478 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1479 // low 3 bits clear.
1480 unsigned KnownZeroOut = std::min(KnownZero.countTrailingOnes(),
1481 KnownZero2.countTrailingOnes());
1483 KnownZero = APInt::getLowBitsSet(BitWidth, KnownZeroOut);
1484 KnownOne = APInt(BitWidth, 0);
1488 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1491 // We know that the top bits of C-X are clear if X contains less bits
1492 // than C (i.e. no wrap-around can happen). For example, 20-X is
1493 // positive if we can prove that X is >= 0 and < 16.
1494 if (CLHS->getAPIntValue().isNonNegative()) {
1495 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1496 // NLZ can't be BitWidth with no sign bit
1497 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1498 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1500 // If all of the MaskV bits are known to be zero, then we know the output
1501 // top bits are zero, because we now know that the output is from [0-C].
1502 if ((KnownZero & MaskV) == MaskV) {
1503 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1504 // Top bits known zero.
1505 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1506 KnownOne = APInt(BitWidth, 0); // No one bits known.
1508 KnownZero = KnownOne = APInt(BitWidth, 0); // Otherwise, nothing known.
1514 // Allow the target to implement this method for its nodes.
1515 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1516 case ISD::INTRINSIC_WO_CHAIN:
1517 case ISD::INTRINSIC_W_CHAIN:
1518 case ISD::INTRINSIC_VOID:
1519 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1525 /// ComputeNumSignBits - Return the number of times the sign bit of the
1526 /// register is replicated into the other bits. We know that at least 1 bit
1527 /// is always equal to the sign bit (itself), but other cases can give us
1528 /// information. For example, immediately after an "SRA X, 2", we know that
1529 /// the top 3 bits are all equal to each other, so we return 3.
1530 unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1531 MVT::ValueType VT = Op.getValueType();
1532 assert(MVT::isInteger(VT) && "Invalid VT!");
1533 unsigned VTBits = MVT::getSizeInBits(VT);
1537 return 1; // Limit search depth.
1539 switch (Op.getOpcode()) {
1541 case ISD::AssertSext:
1542 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1543 return VTBits-Tmp+1;
1544 case ISD::AssertZext:
1545 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1548 case ISD::Constant: {
1549 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1550 // If negative, return # leading ones.
1551 if (Val.isNegative())
1552 return Val.countLeadingOnes();
1554 // Return # leading zeros.
1555 return Val.countLeadingZeros();
1558 case ISD::SIGN_EXTEND:
1559 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1560 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1562 case ISD::SIGN_EXTEND_INREG:
1563 // Max of the input and what this extends.
1564 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1567 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1568 return std::max(Tmp, Tmp2);
1571 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1572 // SRA X, C -> adds C sign bits.
1573 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1574 Tmp += C->getValue();
1575 if (Tmp > VTBits) Tmp = VTBits;
1579 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1580 // shl destroys sign bits.
1581 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1582 if (C->getValue() >= VTBits || // Bad shift.
1583 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1584 return Tmp - C->getValue();
1589 case ISD::XOR: // NOT is handled here.
1590 // Logical binary ops preserve the number of sign bits.
1591 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1592 if (Tmp == 1) return 1; // Early out.
1593 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1594 return std::min(Tmp, Tmp2);
1597 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1598 if (Tmp == 1) return 1; // Early out.
1599 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1600 return std::min(Tmp, Tmp2);
1603 // If setcc returns 0/-1, all bits are sign bits.
1604 if (TLI.getSetCCResultContents() ==
1605 TargetLowering::ZeroOrNegativeOneSetCCResult)
1610 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1611 unsigned RotAmt = C->getValue() & (VTBits-1);
1613 // Handle rotate right by N like a rotate left by 32-N.
1614 if (Op.getOpcode() == ISD::ROTR)
1615 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1617 // If we aren't rotating out all of the known-in sign bits, return the
1618 // number that are left. This handles rotl(sext(x), 1) for example.
1619 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1620 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1624 // Add can have at most one carry bit. Thus we know that the output
1625 // is, at worst, one more bit than the inputs.
1626 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1627 if (Tmp == 1) return 1; // Early out.
1629 // Special case decrementing a value (ADD X, -1):
1630 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1631 if (CRHS->isAllOnesValue()) {
1632 APInt KnownZero, KnownOne;
1633 APInt Mask = APInt::getAllOnesValue(VTBits);
1634 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1636 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1638 if ((KnownZero | APInt(VTBits, 1)) == Mask)
1641 // If we are subtracting one from a positive number, there is no carry
1642 // out of the result.
1643 if (KnownZero.isNegative())
1647 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1648 if (Tmp2 == 1) return 1;
1649 return std::min(Tmp, Tmp2)-1;
1653 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1654 if (Tmp2 == 1) return 1;
1657 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1658 if (CLHS->isNullValue()) {
1659 APInt KnownZero, KnownOne;
1660 APInt Mask = APInt::getAllOnesValue(VTBits);
1661 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1662 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1664 if ((KnownZero | APInt(VTBits, 1)) == Mask)
1667 // If the input is known to be positive (the sign bit is known clear),
1668 // the output of the NEG has the same number of sign bits as the input.
1669 if (KnownZero.isNegative())
1672 // Otherwise, we treat this like a SUB.
1675 // Sub can have at most one carry bit. Thus we know that the output
1676 // is, at worst, one more bit than the inputs.
1677 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1678 if (Tmp == 1) return 1; // Early out.
1679 return std::min(Tmp, Tmp2)-1;
1682 // FIXME: it's tricky to do anything useful for this, but it is an important
1683 // case for targets like X86.
1687 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1688 if (Op.getOpcode() == ISD::LOAD) {
1689 LoadSDNode *LD = cast<LoadSDNode>(Op);
1690 unsigned ExtType = LD->getExtensionType();
1693 case ISD::SEXTLOAD: // '17' bits known
1694 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
1695 return VTBits-Tmp+1;
1696 case ISD::ZEXTLOAD: // '16' bits known
1697 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
1702 // Allow the target to implement this method for its nodes.
1703 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1704 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1705 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1706 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1707 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1708 if (NumBits > 1) return NumBits;
1711 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1712 // use this information.
1713 APInt KnownZero, KnownOne;
1714 APInt Mask = APInt::getAllOnesValue(VTBits);
1715 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1717 if (KnownZero.isNegative()) { // sign bit is 0
1719 } else if (KnownOne.isNegative()) { // sign bit is 1;
1726 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1727 // the number of identical bits in the top of the input value.
1729 Mask <<= Mask.getBitWidth()-VTBits;
1730 // Return # leading zeros. We use 'min' here in case Val was zero before
1731 // shifting. We don't want to return '64' as for an i32 "0".
1732 return std::min(VTBits, Mask.countLeadingZeros());
1736 bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1737 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1738 if (!GA) return false;
1739 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1740 if (!GV) return false;
1741 MachineModuleInfo *MMI = getMachineModuleInfo();
1742 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1746 /// getNode - Gets or creates the specified node.
1748 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
1749 FoldingSetNodeID ID;
1750 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
1752 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1753 return SDOperand(E, 0);
1754 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
1755 CSEMap.InsertNode(N, IP);
1757 AllNodes.push_back(N);
1758 return SDOperand(N, 0);
1761 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1762 SDOperand Operand) {
1763 // Constant fold unary operations with an integer constant operand.
1764 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1765 const APInt &Val = C->getAPIntValue();
1766 unsigned BitWidth = MVT::getSizeInBits(VT);
1769 case ISD::SIGN_EXTEND:
1770 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
1771 case ISD::ANY_EXTEND:
1772 case ISD::ZERO_EXTEND:
1774 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
1775 case ISD::UINT_TO_FP:
1776 case ISD::SINT_TO_FP: {
1777 const uint64_t zero[] = {0, 0};
1778 // No compile time operations on this type.
1779 if (VT==MVT::ppcf128)
1781 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1782 (void)apf.convertFromAPInt(Val,
1783 Opcode==ISD::SINT_TO_FP,
1784 APFloat::rmNearestTiesToEven);
1785 return getConstantFP(apf, VT);
1787 case ISD::BIT_CONVERT:
1788 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
1789 return getConstantFP(Val.bitsToFloat(), VT);
1790 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
1791 return getConstantFP(Val.bitsToDouble(), VT);
1794 return getConstant(Val.byteSwap(), VT);
1796 return getConstant(Val.countPopulation(), VT);
1798 return getConstant(Val.countLeadingZeros(), VT);
1800 return getConstant(Val.countTrailingZeros(), VT);
1804 // Constant fold unary operations with a floating point constant operand.
1805 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1806 APFloat V = C->getValueAPF(); // make copy
1807 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
1811 return getConstantFP(V, VT);
1814 return getConstantFP(V, VT);
1816 case ISD::FP_EXTEND:
1817 // This can return overflow, underflow, or inexact; we don't care.
1818 // FIXME need to be more flexible about rounding mode.
1819 (void)V.convert(*MVTToAPFloatSemantics(VT),
1820 APFloat::rmNearestTiesToEven);
1821 return getConstantFP(V, VT);
1822 case ISD::FP_TO_SINT:
1823 case ISD::FP_TO_UINT: {
1825 assert(integerPartWidth >= 64);
1826 // FIXME need to be more flexible about rounding mode.
1827 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1828 Opcode==ISD::FP_TO_SINT,
1829 APFloat::rmTowardZero);
1830 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1832 return getConstant(x, VT);
1834 case ISD::BIT_CONVERT:
1835 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1836 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1837 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1838 return getConstant(V.convertToAPInt().getZExtValue(), VT);
1844 unsigned OpOpcode = Operand.Val->getOpcode();
1846 case ISD::TokenFactor:
1847 return Operand; // Factor of one node? No factor.
1848 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
1849 case ISD::FP_EXTEND:
1850 assert(MVT::isFloatingPoint(VT) &&
1851 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
1852 if (Operand.getValueType() == VT) return Operand; // noop conversion.
1853 if (Operand.getOpcode() == ISD::UNDEF)
1854 return getNode(ISD::UNDEF, VT);
1856 case ISD::SIGN_EXTEND:
1857 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1858 "Invalid SIGN_EXTEND!");
1859 if (Operand.getValueType() == VT) return Operand; // noop extension
1860 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1861 && "Invalid sext node, dst < src!");
1862 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1863 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1865 case ISD::ZERO_EXTEND:
1866 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1867 "Invalid ZERO_EXTEND!");
1868 if (Operand.getValueType() == VT) return Operand; // noop extension
1869 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1870 && "Invalid zext node, dst < src!");
1871 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1872 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1874 case ISD::ANY_EXTEND:
1875 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1876 "Invalid ANY_EXTEND!");
1877 if (Operand.getValueType() == VT) return Operand; // noop extension
1878 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1879 && "Invalid anyext node, dst < src!");
1880 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1881 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1882 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1885 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1886 "Invalid TRUNCATE!");
1887 if (Operand.getValueType() == VT) return Operand; // noop truncate
1888 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1889 && "Invalid truncate node, src < dst!");
1890 if (OpOpcode == ISD::TRUNCATE)
1891 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1892 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1893 OpOpcode == ISD::ANY_EXTEND) {
1894 // If the source is smaller than the dest, we still need an extend.
1895 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1896 < MVT::getSizeInBits(VT))
1897 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1898 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1899 > MVT::getSizeInBits(VT))
1900 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1902 return Operand.Val->getOperand(0);
1905 case ISD::BIT_CONVERT:
1906 // Basic sanity checking.
1907 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
1908 && "Cannot BIT_CONVERT between types of different sizes!");
1909 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1910 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1911 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1912 if (OpOpcode == ISD::UNDEF)
1913 return getNode(ISD::UNDEF, VT);
1915 case ISD::SCALAR_TO_VECTOR:
1916 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1917 MVT::getVectorElementType(VT) == Operand.getValueType() &&
1918 "Illegal SCALAR_TO_VECTOR node!");
1919 if (OpOpcode == ISD::UNDEF)
1920 return getNode(ISD::UNDEF, VT);
1921 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
1922 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
1923 isa<ConstantSDNode>(Operand.getOperand(1)) &&
1924 Operand.getConstantOperandVal(1) == 0 &&
1925 Operand.getOperand(0).getValueType() == VT)
1926 return Operand.getOperand(0);
1929 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1930 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1931 Operand.Val->getOperand(0));
1932 if (OpOpcode == ISD::FNEG) // --X -> X
1933 return Operand.Val->getOperand(0);
1936 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1937 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1942 SDVTList VTs = getVTList(VT);
1943 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1944 FoldingSetNodeID ID;
1945 SDOperand Ops[1] = { Operand };
1946 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
1948 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1949 return SDOperand(E, 0);
1950 N = new UnarySDNode(Opcode, VTs, Operand);
1951 CSEMap.InsertNode(N, IP);
1953 N = new UnarySDNode(Opcode, VTs, Operand);
1955 AllNodes.push_back(N);
1956 return SDOperand(N, 0);
1961 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1962 SDOperand N1, SDOperand N2) {
1963 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1964 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1967 case ISD::TokenFactor:
1968 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1969 N2.getValueType() == MVT::Other && "Invalid token factor!");
1970 // Fold trivial token factors.
1971 if (N1.getOpcode() == ISD::EntryToken) return N2;
1972 if (N2.getOpcode() == ISD::EntryToken) return N1;
1975 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1976 N1.getValueType() == VT && "Binary operator types must match!");
1977 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1978 // worth handling here.
1979 if (N2C && N2C->isNullValue())
1981 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
1986 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1987 N1.getValueType() == VT && "Binary operator types must match!");
1988 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1989 // worth handling here.
1990 if (N2C && N2C->isNullValue())
1997 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
2009 assert(N1.getValueType() == N2.getValueType() &&
2010 N1.getValueType() == VT && "Binary operator types must match!");
2012 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2013 assert(N1.getValueType() == VT &&
2014 MVT::isFloatingPoint(N1.getValueType()) &&
2015 MVT::isFloatingPoint(N2.getValueType()) &&
2016 "Invalid FCOPYSIGN!");
2023 assert(VT == N1.getValueType() &&
2024 "Shift operators return type must be the same as their first arg");
2025 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
2026 VT != MVT::i1 && "Shifts only work on integers");
2028 case ISD::FP_ROUND_INREG: {
2029 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2030 assert(VT == N1.getValueType() && "Not an inreg round!");
2031 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
2032 "Cannot FP_ROUND_INREG integer types");
2033 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2034 "Not rounding down!");
2035 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
2039 assert(MVT::isFloatingPoint(VT) &&
2040 MVT::isFloatingPoint(N1.getValueType()) &&
2041 MVT::getSizeInBits(VT) <= MVT::getSizeInBits(N1.getValueType()) &&
2042 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
2043 if (N1.getValueType() == VT) return N1; // noop conversion.
2045 case ISD::AssertSext:
2046 case ISD::AssertZext: {
2047 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2048 assert(VT == N1.getValueType() && "Not an inreg extend!");
2049 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2050 "Cannot *_EXTEND_INREG FP types");
2051 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2053 if (VT == EVT) return N1; // noop assertion.
2056 case ISD::SIGN_EXTEND_INREG: {
2057 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2058 assert(VT == N1.getValueType() && "Not an inreg extend!");
2059 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2060 "Cannot *_EXTEND_INREG FP types");
2061 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2063 if (EVT == VT) return N1; // Not actually extending
2066 APInt Val = N1C->getAPIntValue();
2067 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
2068 Val <<= Val.getBitWidth()-FromBits;
2069 Val = Val.ashr(Val.getBitWidth()-FromBits);
2070 return getConstant(Val, VT);
2074 case ISD::EXTRACT_VECTOR_ELT:
2075 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2077 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2078 if (N1.getOpcode() == ISD::UNDEF)
2079 return getNode(ISD::UNDEF, VT);
2081 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2082 // expanding copies of large vectors from registers.
2083 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2084 N1.getNumOperands() > 0) {
2086 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2087 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2088 N1.getOperand(N2C->getValue() / Factor),
2089 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2092 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2093 // expanding large vector constants.
2094 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2095 return N1.getOperand(N2C->getValue());
2097 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2098 // operations are lowered to scalars.
2099 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2100 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2102 return N1.getOperand(1);
2104 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2107 case ISD::EXTRACT_ELEMENT:
2108 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
2109 assert(!MVT::isVector(N1.getValueType()) &&
2110 MVT::isInteger(N1.getValueType()) &&
2111 !MVT::isVector(VT) && MVT::isInteger(VT) &&
2112 "EXTRACT_ELEMENT only applies to integers!");
2114 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2115 // 64-bit integers into 32-bit parts. Instead of building the extract of
2116 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2117 if (N1.getOpcode() == ISD::BUILD_PAIR)
2118 return N1.getOperand(N2C->getValue());
2120 // EXTRACT_ELEMENT of a constant int is also very common.
2121 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2122 unsigned ElementSize = MVT::getSizeInBits(VT);
2123 unsigned Shift = ElementSize * N2C->getValue();
2124 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2125 return getConstant(ShiftedVal.trunc(ElementSize), VT);
2128 case ISD::EXTRACT_SUBVECTOR:
2129 if (N1.getValueType() == VT) // Trivial extraction.
2136 APInt C1 = N1C->getAPIntValue(), C2 = N2C->getAPIntValue();
2138 case ISD::ADD: return getConstant(C1 + C2, VT);
2139 case ISD::SUB: return getConstant(C1 - C2, VT);
2140 case ISD::MUL: return getConstant(C1 * C2, VT);
2142 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2145 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2148 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2151 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2153 case ISD::AND : return getConstant(C1 & C2, VT);
2154 case ISD::OR : return getConstant(C1 | C2, VT);
2155 case ISD::XOR : return getConstant(C1 ^ C2, VT);
2156 case ISD::SHL : return getConstant(C1 << C2, VT);
2157 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2158 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2159 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2160 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
2163 } else { // Cannonicalize constant to RHS if commutative
2164 if (isCommutativeBinOp(Opcode)) {
2165 std::swap(N1C, N2C);
2171 // Constant fold FP operations.
2172 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2173 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
2175 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2176 // Cannonicalize constant to RHS if commutative
2177 std::swap(N1CFP, N2CFP);
2179 } else if (N2CFP && VT != MVT::ppcf128) {
2180 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2181 APFloat::opStatus s;
2184 s = V1.add(V2, APFloat::rmNearestTiesToEven);
2185 if (s != APFloat::opInvalidOp)
2186 return getConstantFP(V1, VT);
2189 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2190 if (s!=APFloat::opInvalidOp)
2191 return getConstantFP(V1, VT);
2194 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2195 if (s!=APFloat::opInvalidOp)
2196 return getConstantFP(V1, VT);
2199 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2200 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2201 return getConstantFP(V1, VT);
2204 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2205 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2206 return getConstantFP(V1, VT);
2208 case ISD::FCOPYSIGN:
2210 return getConstantFP(V1, VT);
2216 // Canonicalize an UNDEF to the RHS, even over a constant.
2217 if (N1.getOpcode() == ISD::UNDEF) {
2218 if (isCommutativeBinOp(Opcode)) {
2222 case ISD::FP_ROUND_INREG:
2223 case ISD::SIGN_EXTEND_INREG:
2229 return N1; // fold op(undef, arg2) -> undef
2236 if (!MVT::isVector(VT))
2237 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2238 // For vectors, we can't easily build an all zero vector, just return
2245 // Fold a bunch of operators when the RHS is undef.
2246 if (N2.getOpcode() == ISD::UNDEF) {
2249 if (N1.getOpcode() == ISD::UNDEF)
2250 // Handle undef ^ undef -> 0 special case. This is a common
2252 return getConstant(0, VT);
2267 return N2; // fold op(arg1, undef) -> undef
2272 if (!MVT::isVector(VT))
2273 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2274 // For vectors, we can't easily build an all zero vector, just return
2278 if (!MVT::isVector(VT))
2279 return getConstant(MVT::getIntVTBitMask(VT), VT);
2280 // For vectors, we can't easily build an all one vector, just return
2288 // Memoize this node if possible.
2290 SDVTList VTs = getVTList(VT);
2291 if (VT != MVT::Flag) {
2292 SDOperand Ops[] = { N1, N2 };
2293 FoldingSetNodeID ID;
2294 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
2296 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2297 return SDOperand(E, 0);
2298 N = new BinarySDNode(Opcode, VTs, N1, N2);
2299 CSEMap.InsertNode(N, IP);
2301 N = new BinarySDNode(Opcode, VTs, N1, N2);
2304 AllNodes.push_back(N);
2305 return SDOperand(N, 0);
2308 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2309 SDOperand N1, SDOperand N2, SDOperand N3) {
2310 // Perform various simplifications.
2311 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2312 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2315 // Use FoldSetCC to simplify SETCC's.
2316 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
2317 if (Simp.Val) return Simp;
2322 if (N1C->getValue())
2323 return N2; // select true, X, Y -> X
2325 return N3; // select false, X, Y -> Y
2328 if (N2 == N3) return N2; // select C, X, X -> X
2332 if (N2C->getValue()) // Unconditional branch
2333 return getNode(ISD::BR, MVT::Other, N1, N3);
2335 return N1; // Never-taken branch
2338 case ISD::VECTOR_SHUFFLE:
2339 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2340 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2341 N3.getOpcode() == ISD::BUILD_VECTOR &&
2342 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2343 "Illegal VECTOR_SHUFFLE node!");
2345 case ISD::BIT_CONVERT:
2346 // Fold bit_convert nodes from a type to themselves.
2347 if (N1.getValueType() == VT)
2352 // Memoize node if it doesn't produce a flag.
2354 SDVTList VTs = getVTList(VT);
2355 if (VT != MVT::Flag) {
2356 SDOperand Ops[] = { N1, N2, N3 };
2357 FoldingSetNodeID ID;
2358 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2360 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2361 return SDOperand(E, 0);
2362 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2363 CSEMap.InsertNode(N, IP);
2365 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2367 AllNodes.push_back(N);
2368 return SDOperand(N, 0);
2371 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2372 SDOperand N1, SDOperand N2, SDOperand N3,
2374 SDOperand Ops[] = { N1, N2, N3, N4 };
2375 return getNode(Opcode, VT, Ops, 4);
2378 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2379 SDOperand N1, SDOperand N2, SDOperand N3,
2380 SDOperand N4, SDOperand N5) {
2381 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2382 return getNode(Opcode, VT, Ops, 5);
2385 SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dest,
2386 SDOperand Src, SDOperand Size,
2388 SDOperand AlwaysInline) {
2389 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2390 return getNode(ISD::MEMCPY, MVT::Other, Ops, 6);
2393 SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dest,
2394 SDOperand Src, SDOperand Size,
2396 SDOperand AlwaysInline) {
2397 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2398 return getNode(ISD::MEMMOVE, MVT::Other, Ops, 6);
2401 SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dest,
2402 SDOperand Src, SDOperand Size,
2404 SDOperand AlwaysInline) {
2405 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2406 return getNode(ISD::MEMSET, MVT::Other, Ops, 6);
2409 SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
2410 SDOperand Ptr, SDOperand Cmp,
2411 SDOperand Swp, MVT::ValueType VT) {
2412 assert(Opcode == ISD::ATOMIC_LCS && "Invalid Atomic Op");
2413 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
2414 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
2415 FoldingSetNodeID ID;
2416 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
2417 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
2418 ID.AddInteger((unsigned int)VT);
2420 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2421 return SDOperand(E, 0);
2422 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, VT);
2423 CSEMap.InsertNode(N, IP);
2424 AllNodes.push_back(N);
2425 return SDOperand(N, 0);
2428 SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
2429 SDOperand Ptr, SDOperand Val,
2430 MVT::ValueType VT) {
2431 assert((Opcode == ISD::ATOMIC_LAS || Opcode == ISD::ATOMIC_SWAP)
2432 && "Invalid Atomic Op");
2433 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
2434 FoldingSetNodeID ID;
2435 SDOperand Ops[] = {Chain, Ptr, Val};
2436 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2437 ID.AddInteger((unsigned int)VT);
2439 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2440 return SDOperand(E, 0);
2441 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, VT);
2442 CSEMap.InsertNode(N, IP);
2443 AllNodes.push_back(N);
2444 return SDOperand(N, 0);
2447 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2448 SDOperand Chain, SDOperand Ptr,
2449 const Value *SV, int SVOffset,
2450 bool isVolatile, unsigned Alignment) {
2451 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2453 if (VT != MVT::iPTR) {
2454 Ty = MVT::getTypeForValueType(VT);
2456 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2457 assert(PT && "Value for load must be a pointer");
2458 Ty = PT->getElementType();
2460 assert(Ty && "Could not get type information for load");
2461 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2463 SDVTList VTs = getVTList(VT, MVT::Other);
2464 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2465 SDOperand Ops[] = { Chain, Ptr, Undef };
2466 FoldingSetNodeID ID;
2467 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2468 ID.AddInteger(ISD::UNINDEXED);
2469 ID.AddInteger(ISD::NON_EXTLOAD);
2470 ID.AddInteger((unsigned int)VT);
2471 ID.AddInteger(Alignment);
2472 ID.AddInteger(isVolatile);
2474 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2475 return SDOperand(E, 0);
2476 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
2477 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2479 CSEMap.InsertNode(N, IP);
2480 AllNodes.push_back(N);
2481 return SDOperand(N, 0);
2484 SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
2485 SDOperand Chain, SDOperand Ptr,
2487 int SVOffset, MVT::ValueType EVT,
2488 bool isVolatile, unsigned Alignment) {
2489 // If they are asking for an extending load from/to the same thing, return a
2492 return getLoad(VT, Chain, Ptr, SV, SVOffset, isVolatile, Alignment);
2494 if (MVT::isVector(VT))
2495 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
2497 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2498 "Should only be an extending load, not truncating!");
2499 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2500 "Cannot sign/zero extend a FP/Vector load!");
2501 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2502 "Cannot convert from FP to Int or Int -> FP!");
2504 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2506 if (VT != MVT::iPTR) {
2507 Ty = MVT::getTypeForValueType(VT);
2509 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2510 assert(PT && "Value for load must be a pointer");
2511 Ty = PT->getElementType();
2513 assert(Ty && "Could not get type information for load");
2514 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2516 SDVTList VTs = getVTList(VT, MVT::Other);
2517 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2518 SDOperand Ops[] = { Chain, Ptr, Undef };
2519 FoldingSetNodeID ID;
2520 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2521 ID.AddInteger(ISD::UNINDEXED);
2522 ID.AddInteger(ExtType);
2523 ID.AddInteger((unsigned int)EVT);
2524 ID.AddInteger(Alignment);
2525 ID.AddInteger(isVolatile);
2527 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2528 return SDOperand(E, 0);
2529 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
2530 SV, SVOffset, Alignment, isVolatile);
2531 CSEMap.InsertNode(N, IP);
2532 AllNodes.push_back(N);
2533 return SDOperand(N, 0);
2537 SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2538 SDOperand Offset, ISD::MemIndexedMode AM) {
2539 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
2540 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2541 "Load is already a indexed load!");
2542 MVT::ValueType VT = OrigLoad.getValueType();
2543 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
2544 SDOperand Ops[] = { LD->getChain(), Base, Offset };
2545 FoldingSetNodeID ID;
2546 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2548 ID.AddInteger(LD->getExtensionType());
2549 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
2550 ID.AddInteger(LD->getAlignment());
2551 ID.AddInteger(LD->isVolatile());
2553 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2554 return SDOperand(E, 0);
2555 SDNode *N = new LoadSDNode(Ops, VTs, AM,
2556 LD->getExtensionType(), LD->getMemoryVT(),
2557 LD->getSrcValue(), LD->getSrcValueOffset(),
2558 LD->getAlignment(), LD->isVolatile());
2559 CSEMap.InsertNode(N, IP);
2560 AllNodes.push_back(N);
2561 return SDOperand(N, 0);
2564 SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
2565 SDOperand Ptr, const Value *SV, int SVOffset,
2566 bool isVolatile, unsigned Alignment) {
2567 MVT::ValueType VT = Val.getValueType();
2569 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2571 if (VT != MVT::iPTR) {
2572 Ty = MVT::getTypeForValueType(VT);
2574 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2575 assert(PT && "Value for store must be a pointer");
2576 Ty = PT->getElementType();
2578 assert(Ty && "Could not get type information for store");
2579 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2581 SDVTList VTs = getVTList(MVT::Other);
2582 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2583 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2584 FoldingSetNodeID ID;
2585 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2586 ID.AddInteger(ISD::UNINDEXED);
2587 ID.AddInteger(false);
2588 ID.AddInteger((unsigned int)VT);
2589 ID.AddInteger(Alignment);
2590 ID.AddInteger(isVolatile);
2592 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2593 return SDOperand(E, 0);
2594 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
2595 VT, SV, SVOffset, Alignment, isVolatile);
2596 CSEMap.InsertNode(N, IP);
2597 AllNodes.push_back(N);
2598 return SDOperand(N, 0);
2601 SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
2602 SDOperand Ptr, const Value *SV,
2603 int SVOffset, MVT::ValueType SVT,
2604 bool isVolatile, unsigned Alignment) {
2605 MVT::ValueType VT = Val.getValueType();
2608 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
2610 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2611 "Not a truncation?");
2612 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2613 "Can't do FP-INT conversion!");
2615 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2617 if (VT != MVT::iPTR) {
2618 Ty = MVT::getTypeForValueType(VT);
2620 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2621 assert(PT && "Value for store must be a pointer");
2622 Ty = PT->getElementType();
2624 assert(Ty && "Could not get type information for store");
2625 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2627 SDVTList VTs = getVTList(MVT::Other);
2628 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2629 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2630 FoldingSetNodeID ID;
2631 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2632 ID.AddInteger(ISD::UNINDEXED);
2634 ID.AddInteger((unsigned int)SVT);
2635 ID.AddInteger(Alignment);
2636 ID.AddInteger(isVolatile);
2638 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2639 return SDOperand(E, 0);
2640 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
2641 SVT, SV, SVOffset, Alignment, isVolatile);
2642 CSEMap.InsertNode(N, IP);
2643 AllNodes.push_back(N);
2644 return SDOperand(N, 0);
2648 SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2649 SDOperand Offset, ISD::MemIndexedMode AM) {
2650 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2651 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2652 "Store is already a indexed store!");
2653 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2654 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2655 FoldingSetNodeID ID;
2656 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2658 ID.AddInteger(ST->isTruncatingStore());
2659 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
2660 ID.AddInteger(ST->getAlignment());
2661 ID.AddInteger(ST->isVolatile());
2663 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2664 return SDOperand(E, 0);
2665 SDNode *N = new StoreSDNode(Ops, VTs, AM,
2666 ST->isTruncatingStore(), ST->getMemoryVT(),
2667 ST->getSrcValue(), ST->getSrcValueOffset(),
2668 ST->getAlignment(), ST->isVolatile());
2669 CSEMap.InsertNode(N, IP);
2670 AllNodes.push_back(N);
2671 return SDOperand(N, 0);
2674 SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2675 SDOperand Chain, SDOperand Ptr,
2677 SDOperand Ops[] = { Chain, Ptr, SV };
2678 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
2681 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2682 const SDOperand *Ops, unsigned NumOps) {
2684 case 0: return getNode(Opcode, VT);
2685 case 1: return getNode(Opcode, VT, Ops[0]);
2686 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2687 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
2693 case ISD::SELECT_CC: {
2694 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
2695 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2696 "LHS and RHS of condition must have same type!");
2697 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2698 "True and False arms of SelectCC must have same type!");
2699 assert(Ops[2].getValueType() == VT &&
2700 "select_cc node must be of same type as true and false value!");
2704 assert(NumOps == 5 && "BR_CC takes 5 operands!");
2705 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2706 "LHS/RHS of comparison should match types!");
2713 SDVTList VTs = getVTList(VT);
2714 if (VT != MVT::Flag) {
2715 FoldingSetNodeID ID;
2716 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
2718 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2719 return SDOperand(E, 0);
2720 N = new SDNode(Opcode, VTs, Ops, NumOps);
2721 CSEMap.InsertNode(N, IP);
2723 N = new SDNode(Opcode, VTs, Ops, NumOps);
2725 AllNodes.push_back(N);
2726 return SDOperand(N, 0);
2729 SDOperand SelectionDAG::getNode(unsigned Opcode,
2730 std::vector<MVT::ValueType> &ResultTys,
2731 const SDOperand *Ops, unsigned NumOps) {
2732 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2736 SDOperand SelectionDAG::getNode(unsigned Opcode,
2737 const MVT::ValueType *VTs, unsigned NumVTs,
2738 const SDOperand *Ops, unsigned NumOps) {
2740 return getNode(Opcode, VTs[0], Ops, NumOps);
2741 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2744 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2745 const SDOperand *Ops, unsigned NumOps) {
2746 if (VTList.NumVTs == 1)
2747 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
2750 // FIXME: figure out how to safely handle things like
2751 // int foo(int x) { return 1 << (x & 255); }
2752 // int bar() { return foo(256); }
2754 case ISD::SRA_PARTS:
2755 case ISD::SRL_PARTS:
2756 case ISD::SHL_PARTS:
2757 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2758 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
2759 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2760 else if (N3.getOpcode() == ISD::AND)
2761 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2762 // If the and is only masking out bits that cannot effect the shift,
2763 // eliminate the and.
2764 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2765 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2766 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2772 // Memoize the node unless it returns a flag.
2774 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
2775 FoldingSetNodeID ID;
2776 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
2778 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2779 return SDOperand(E, 0);
2781 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2782 else if (NumOps == 2)
2783 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2784 else if (NumOps == 3)
2785 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2787 N = new SDNode(Opcode, VTList, Ops, NumOps);
2788 CSEMap.InsertNode(N, IP);
2791 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2792 else if (NumOps == 2)
2793 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2794 else if (NumOps == 3)
2795 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2797 N = new SDNode(Opcode, VTList, Ops, NumOps);
2799 AllNodes.push_back(N);
2800 return SDOperand(N, 0);
2803 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2804 return getNode(Opcode, VTList, 0, 0);
2807 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2809 SDOperand Ops[] = { N1 };
2810 return getNode(Opcode, VTList, Ops, 1);
2813 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2814 SDOperand N1, SDOperand N2) {
2815 SDOperand Ops[] = { N1, N2 };
2816 return getNode(Opcode, VTList, Ops, 2);
2819 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2820 SDOperand N1, SDOperand N2, SDOperand N3) {
2821 SDOperand Ops[] = { N1, N2, N3 };
2822 return getNode(Opcode, VTList, Ops, 3);
2825 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2826 SDOperand N1, SDOperand N2, SDOperand N3,
2828 SDOperand Ops[] = { N1, N2, N3, N4 };
2829 return getNode(Opcode, VTList, Ops, 4);
2832 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2833 SDOperand N1, SDOperand N2, SDOperand N3,
2834 SDOperand N4, SDOperand N5) {
2835 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2836 return getNode(Opcode, VTList, Ops, 5);
2839 SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
2840 return makeVTList(SDNode::getValueTypeList(VT), 1);
2843 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
2844 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2845 E = VTList.end(); I != E; ++I) {
2846 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
2847 return makeVTList(&(*I)[0], 2);
2849 std::vector<MVT::ValueType> V;
2852 VTList.push_front(V);
2853 return makeVTList(&(*VTList.begin())[0], 2);
2855 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2856 MVT::ValueType VT3) {
2857 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2858 E = VTList.end(); I != E; ++I) {
2859 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2861 return makeVTList(&(*I)[0], 3);
2863 std::vector<MVT::ValueType> V;
2867 VTList.push_front(V);
2868 return makeVTList(&(*VTList.begin())[0], 3);
2871 SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2873 case 0: assert(0 && "Cannot have nodes without results!");
2874 case 1: return getVTList(VTs[0]);
2875 case 2: return getVTList(VTs[0], VTs[1]);
2876 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2880 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2881 E = VTList.end(); I != E; ++I) {
2882 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2884 bool NoMatch = false;
2885 for (unsigned i = 2; i != NumVTs; ++i)
2886 if (VTs[i] != (*I)[i]) {
2891 return makeVTList(&*I->begin(), NumVTs);
2894 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2895 return makeVTList(&*VTList.begin()->begin(), NumVTs);
2899 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2900 /// specified operands. If the resultant node already exists in the DAG,
2901 /// this does not modify the specified node, instead it returns the node that
2902 /// already exists. If the resultant node does not exist in the DAG, the
2903 /// input node is returned. As a degenerate case, if you specify the same
2904 /// input operands as the node already has, the input node is returned.
2905 SDOperand SelectionDAG::
2906 UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2907 SDNode *N = InN.Val;
2908 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2910 // Check to see if there is no change.
2911 if (Op == N->getOperand(0)) return InN;
2913 // See if the modified node already exists.
2914 void *InsertPos = 0;
2915 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2916 return SDOperand(Existing, InN.ResNo);
2918 // Nope it doesn't. Remove the node from it's current place in the maps.
2920 RemoveNodeFromCSEMaps(N);
2922 // Now we update the operands.
2923 N->OperandList[0].Val->removeUser(N);
2925 N->OperandList[0] = Op;
2927 // If this gets put into a CSE map, add it.
2928 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2932 SDOperand SelectionDAG::
2933 UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2934 SDNode *N = InN.Val;
2935 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2937 // Check to see if there is no change.
2938 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2939 return InN; // No operands changed, just return the input node.
2941 // See if the modified node already exists.
2942 void *InsertPos = 0;
2943 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2944 return SDOperand(Existing, InN.ResNo);
2946 // Nope it doesn't. Remove the node from it's current place in the maps.
2948 RemoveNodeFromCSEMaps(N);
2950 // Now we update the operands.
2951 if (N->OperandList[0] != Op1) {
2952 N->OperandList[0].Val->removeUser(N);
2953 Op1.Val->addUser(N);
2954 N->OperandList[0] = Op1;
2956 if (N->OperandList[1] != Op2) {
2957 N->OperandList[1].Val->removeUser(N);
2958 Op2.Val->addUser(N);
2959 N->OperandList[1] = Op2;
2962 // If this gets put into a CSE map, add it.
2963 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2967 SDOperand SelectionDAG::
2968 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2969 SDOperand Ops[] = { Op1, Op2, Op3 };
2970 return UpdateNodeOperands(N, Ops, 3);
2973 SDOperand SelectionDAG::
2974 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2975 SDOperand Op3, SDOperand Op4) {
2976 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2977 return UpdateNodeOperands(N, Ops, 4);
2980 SDOperand SelectionDAG::
2981 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2982 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2983 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2984 return UpdateNodeOperands(N, Ops, 5);
2988 SDOperand SelectionDAG::
2989 UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
2990 SDNode *N = InN.Val;
2991 assert(N->getNumOperands() == NumOps &&
2992 "Update with wrong number of operands");
2994 // Check to see if there is no change.
2995 bool AnyChange = false;
2996 for (unsigned i = 0; i != NumOps; ++i) {
2997 if (Ops[i] != N->getOperand(i)) {
3003 // No operands changed, just return the input node.
3004 if (!AnyChange) return InN;
3006 // See if the modified node already exists.
3007 void *InsertPos = 0;
3008 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
3009 return SDOperand(Existing, InN.ResNo);
3011 // Nope it doesn't. Remove the node from it's current place in the maps.
3013 RemoveNodeFromCSEMaps(N);
3015 // Now we update the operands.
3016 for (unsigned i = 0; i != NumOps; ++i) {
3017 if (N->OperandList[i] != Ops[i]) {
3018 N->OperandList[i].Val->removeUser(N);
3019 Ops[i].Val->addUser(N);
3020 N->OperandList[i] = Ops[i];
3024 // If this gets put into a CSE map, add it.
3025 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
3030 /// MorphNodeTo - This frees the operands of the current node, resets the
3031 /// opcode, types, and operands to the specified value. This should only be
3032 /// used by the SelectionDAG class.
3033 void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
3034 const SDOperand *Ops, unsigned NumOps) {
3037 NumValues = L.NumVTs;
3039 // Clear the operands list, updating used nodes to remove this from their
3041 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3042 I->Val->removeUser(this);
3044 // If NumOps is larger than the # of operands we currently have, reallocate
3045 // the operand list.
3046 if (NumOps > NumOperands) {
3047 if (OperandsNeedDelete)
3048 delete [] OperandList;
3049 OperandList = new SDOperand[NumOps];
3050 OperandsNeedDelete = true;
3053 // Assign the new operands.
3054 NumOperands = NumOps;
3056 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3057 OperandList[i] = Ops[i];
3058 SDNode *N = OperandList[i].Val;
3059 N->Uses.push_back(this);
3063 /// SelectNodeTo - These are used for target selectors to *mutate* the
3064 /// specified node to have the specified return type, Target opcode, and
3065 /// operands. Note that target opcodes are stored as
3066 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
3068 /// Note that SelectNodeTo returns the resultant node. If there is already a
3069 /// node of the specified opcode and operands, it returns that node instead of
3070 /// the current one.
3071 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3072 MVT::ValueType VT) {
3073 SDVTList VTs = getVTList(VT);
3074 FoldingSetNodeID ID;
3075 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
3077 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3080 RemoveNodeFromCSEMaps(N);
3082 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
3084 CSEMap.InsertNode(N, IP);
3088 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3089 MVT::ValueType VT, SDOperand Op1) {
3090 // If an identical node already exists, use it.
3091 SDVTList VTs = getVTList(VT);
3092 SDOperand Ops[] = { Op1 };
3094 FoldingSetNodeID ID;
3095 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
3097 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3100 RemoveNodeFromCSEMaps(N);
3101 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
3102 CSEMap.InsertNode(N, IP);
3106 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3107 MVT::ValueType VT, SDOperand Op1,
3109 // If an identical node already exists, use it.
3110 SDVTList VTs = getVTList(VT);
3111 SDOperand Ops[] = { Op1, Op2 };
3113 FoldingSetNodeID ID;
3114 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3116 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3119 RemoveNodeFromCSEMaps(N);
3121 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3123 CSEMap.InsertNode(N, IP); // Memoize the new node.
3127 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3128 MVT::ValueType VT, SDOperand Op1,
3129 SDOperand Op2, SDOperand Op3) {
3130 // If an identical node already exists, use it.
3131 SDVTList VTs = getVTList(VT);
3132 SDOperand Ops[] = { Op1, Op2, Op3 };
3133 FoldingSetNodeID ID;
3134 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3136 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3139 RemoveNodeFromCSEMaps(N);
3141 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3143 CSEMap.InsertNode(N, IP); // Memoize the new node.
3147 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3148 MVT::ValueType VT, const SDOperand *Ops,
3150 // If an identical node already exists, use it.
3151 SDVTList VTs = getVTList(VT);
3152 FoldingSetNodeID ID;
3153 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
3155 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3158 RemoveNodeFromCSEMaps(N);
3159 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
3161 CSEMap.InsertNode(N, IP); // Memoize the new node.
3165 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3166 MVT::ValueType VT1, MVT::ValueType VT2,
3167 SDOperand Op1, SDOperand Op2) {
3168 SDVTList VTs = getVTList(VT1, VT2);
3169 FoldingSetNodeID ID;
3170 SDOperand Ops[] = { Op1, Op2 };
3171 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3173 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3176 RemoveNodeFromCSEMaps(N);
3177 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3178 CSEMap.InsertNode(N, IP); // Memoize the new node.
3182 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3183 MVT::ValueType VT1, MVT::ValueType VT2,
3184 SDOperand Op1, SDOperand Op2,
3186 // If an identical node already exists, use it.
3187 SDVTList VTs = getVTList(VT1, VT2);
3188 SDOperand Ops[] = { Op1, Op2, Op3 };
3189 FoldingSetNodeID ID;
3190 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3192 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3195 RemoveNodeFromCSEMaps(N);
3197 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3198 CSEMap.InsertNode(N, IP); // Memoize the new node.
3203 /// getTargetNode - These are used for target selectors to create a new node
3204 /// with specified return type(s), target opcode, and operands.
3206 /// Note that getTargetNode returns the resultant node. If there is already a
3207 /// node of the specified opcode and operands, it returns that node instead of
3208 /// the current one.
3209 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3210 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3212 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3214 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3216 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3217 SDOperand Op1, SDOperand Op2) {
3218 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3220 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3221 SDOperand Op1, SDOperand Op2,
3223 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3225 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3226 const SDOperand *Ops, unsigned NumOps) {
3227 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
3229 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3230 MVT::ValueType VT2) {
3231 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3233 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3235 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3236 MVT::ValueType VT2, SDOperand Op1) {
3237 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3238 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
3240 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3241 MVT::ValueType VT2, SDOperand Op1,
3243 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3244 SDOperand Ops[] = { Op1, Op2 };
3245 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
3247 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3248 MVT::ValueType VT2, SDOperand Op1,
3249 SDOperand Op2, SDOperand Op3) {
3250 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3251 SDOperand Ops[] = { Op1, Op2, Op3 };
3252 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
3254 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3256 const SDOperand *Ops, unsigned NumOps) {
3257 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3258 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
3260 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3261 MVT::ValueType VT2, MVT::ValueType VT3,
3262 SDOperand Op1, SDOperand Op2) {
3263 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3264 SDOperand Ops[] = { Op1, Op2 };
3265 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
3267 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3268 MVT::ValueType VT2, MVT::ValueType VT3,
3269 SDOperand Op1, SDOperand Op2,
3271 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3272 SDOperand Ops[] = { Op1, Op2, Op3 };
3273 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3275 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3276 MVT::ValueType VT2, MVT::ValueType VT3,
3277 const SDOperand *Ops, unsigned NumOps) {
3278 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3279 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
3281 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3282 MVT::ValueType VT2, MVT::ValueType VT3,
3284 const SDOperand *Ops, unsigned NumOps) {
3285 std::vector<MVT::ValueType> VTList;
3286 VTList.push_back(VT1);
3287 VTList.push_back(VT2);
3288 VTList.push_back(VT3);
3289 VTList.push_back(VT4);
3290 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3291 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3293 SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3294 std::vector<MVT::ValueType> &ResultTys,
3295 const SDOperand *Ops, unsigned NumOps) {
3296 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3297 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3301 /// getNodeIfExists - Get the specified node if it's already available, or
3302 /// else return NULL.
3303 SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
3304 const SDOperand *Ops, unsigned NumOps) {
3305 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3306 FoldingSetNodeID ID;
3307 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3309 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3316 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3317 /// This can cause recursive merging of nodes in the DAG.
3319 /// This version assumes From has a single result value.
3321 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
3322 DAGUpdateListener *UpdateListener) {
3323 SDNode *From = FromN.Val;
3324 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
3325 "Cannot replace with this method!");
3326 assert(From != To.Val && "Cannot replace uses of with self");
3328 while (!From->use_empty()) {
3329 // Process users until they are all gone.
3330 SDNode *U = *From->use_begin();
3332 // This node is about to morph, remove its old self from the CSE maps.
3333 RemoveNodeFromCSEMaps(U);
3335 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3337 if (I->Val == From) {
3338 From->removeUser(U);
3343 // Now that we have modified U, add it back to the CSE maps. If it already
3344 // exists there, recursively merge the results together.
3345 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3346 ReplaceAllUsesWith(U, Existing, UpdateListener);
3347 // U is now dead. Inform the listener if it exists and delete it.
3349 UpdateListener->NodeDeleted(U);
3350 DeleteNodeNotInCSEMaps(U);
3352 // If the node doesn't already exist, we updated it. Inform a listener if
3355 UpdateListener->NodeUpdated(U);
3360 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3361 /// This can cause recursive merging of nodes in the DAG.
3363 /// This version assumes From/To have matching types and numbers of result
3366 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3367 DAGUpdateListener *UpdateListener) {
3368 assert(From != To && "Cannot replace uses of with self");
3369 assert(From->getNumValues() == To->getNumValues() &&
3370 "Cannot use this version of ReplaceAllUsesWith!");
3371 if (From->getNumValues() == 1) // If possible, use the faster version.
3372 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3375 while (!From->use_empty()) {
3376 // Process users until they are all gone.
3377 SDNode *U = *From->use_begin();
3379 // This node is about to morph, remove its old self from the CSE maps.
3380 RemoveNodeFromCSEMaps(U);
3382 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3384 if (I->Val == From) {
3385 From->removeUser(U);
3390 // Now that we have modified U, add it back to the CSE maps. If it already
3391 // exists there, recursively merge the results together.
3392 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3393 ReplaceAllUsesWith(U, Existing, UpdateListener);
3394 // U is now dead. Inform the listener if it exists and delete it.
3396 UpdateListener->NodeDeleted(U);
3397 DeleteNodeNotInCSEMaps(U);
3399 // If the node doesn't already exist, we updated it. Inform a listener if
3402 UpdateListener->NodeUpdated(U);
3407 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3408 /// This can cause recursive merging of nodes in the DAG.
3410 /// This version can replace From with any result values. To must match the
3411 /// number and types of values returned by From.
3412 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
3413 const SDOperand *To,
3414 DAGUpdateListener *UpdateListener) {
3415 if (From->getNumValues() == 1) // Handle the simple case efficiently.
3416 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
3418 while (!From->use_empty()) {
3419 // Process users until they are all gone.
3420 SDNode *U = *From->use_begin();
3422 // This node is about to morph, remove its old self from the CSE maps.
3423 RemoveNodeFromCSEMaps(U);
3425 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3427 if (I->Val == From) {
3428 const SDOperand &ToOp = To[I->ResNo];
3429 From->removeUser(U);
3431 ToOp.Val->addUser(U);
3434 // Now that we have modified U, add it back to the CSE maps. If it already
3435 // exists there, recursively merge the results together.
3436 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3437 ReplaceAllUsesWith(U, Existing, UpdateListener);
3438 // U is now dead. Inform the listener if it exists and delete it.
3440 UpdateListener->NodeDeleted(U);
3441 DeleteNodeNotInCSEMaps(U);
3443 // If the node doesn't already exist, we updated it. Inform a listener if
3446 UpdateListener->NodeUpdated(U);
3452 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
3453 /// any deleted nodes from the set passed into its constructor and recursively
3454 /// notifies another update listener if specified.
3455 class ChainedSetUpdaterListener :
3456 public SelectionDAG::DAGUpdateListener {
3457 SmallSetVector<SDNode*, 16> &Set;
3458 SelectionDAG::DAGUpdateListener *Chain;
3460 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
3461 SelectionDAG::DAGUpdateListener *chain)
3462 : Set(set), Chain(chain) {}
3464 virtual void NodeDeleted(SDNode *N) {
3466 if (Chain) Chain->NodeDeleted(N);
3468 virtual void NodeUpdated(SDNode *N) {
3469 if (Chain) Chain->NodeUpdated(N);
3474 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3475 /// uses of other values produced by From.Val alone. The Deleted vector is
3476 /// handled the same way as for ReplaceAllUsesWith.
3477 void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
3478 DAGUpdateListener *UpdateListener){
3479 assert(From != To && "Cannot replace a value with itself");
3481 // Handle the simple, trivial, case efficiently.
3482 if (From.Val->getNumValues() == 1) {
3483 ReplaceAllUsesWith(From, To, UpdateListener);
3487 if (From.use_empty()) return;
3489 // Get all of the users of From.Val. We want these in a nice,
3490 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3491 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
3493 // When one of the recursive merges deletes nodes from the graph, we need to
3494 // make sure that UpdateListener is notified *and* that the node is removed
3495 // from Users if present. CSUL does this.
3496 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
3498 while (!Users.empty()) {
3499 // We know that this user uses some value of From. If it is the right
3500 // value, update it.
3501 SDNode *User = Users.back();
3504 // Scan for an operand that matches From.
3505 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3506 for (; Op != E; ++Op)
3507 if (*Op == From) break;
3509 // If there are no matches, the user must use some other result of From.
3510 if (Op == E) continue;
3512 // Okay, we know this user needs to be updated. Remove its old self
3513 // from the CSE maps.
3514 RemoveNodeFromCSEMaps(User);
3516 // Update all operands that match "From" in case there are multiple uses.
3517 for (; Op != E; ++Op) {
3519 From.Val->removeUser(User);
3521 To.Val->addUser(User);
3525 // Now that we have modified User, add it back to the CSE maps. If it
3526 // already exists there, recursively merge the results together.
3527 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
3529 if (UpdateListener) UpdateListener->NodeUpdated(User);
3530 continue; // Continue on to next user.
3533 // If there was already an existing matching node, use ReplaceAllUsesWith
3534 // to replace the dead one with the existing one. This can cause
3535 // recursive merging of other unrelated nodes down the line. The merging
3536 // can cause deletion of nodes that used the old value. To handle this, we
3537 // use CSUL to remove them from the Users set.
3538 ReplaceAllUsesWith(User, Existing, &CSUL);
3540 // User is now dead. Notify a listener if present.
3541 if (UpdateListener) UpdateListener->NodeDeleted(User);
3542 DeleteNodeNotInCSEMaps(User);
3547 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3548 /// their allnodes order. It returns the maximum id.
3549 unsigned SelectionDAG::AssignNodeIds() {
3551 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3558 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
3559 /// based on their topological order. It returns the maximum id and a vector
3560 /// of the SDNodes* in assigned order by reference.
3561 unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
3562 unsigned DAGSize = AllNodes.size();
3563 std::vector<unsigned> InDegree(DAGSize);
3564 std::vector<SDNode*> Sources;
3566 // Use a two pass approach to avoid using a std::map which is slow.
3568 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3571 unsigned Degree = N->use_size();
3572 InDegree[N->getNodeId()] = Degree;
3574 Sources.push_back(N);
3578 while (!Sources.empty()) {
3579 SDNode *N = Sources.back();
3581 TopOrder.push_back(N);
3582 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3584 unsigned Degree = --InDegree[P->getNodeId()];
3586 Sources.push_back(P);
3590 // Second pass, assign the actual topological order as node ids.
3592 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3594 (*TI)->setNodeId(Id++);
3601 //===----------------------------------------------------------------------===//
3603 //===----------------------------------------------------------------------===//
3605 // Out-of-line virtual method to give class a home.
3606 void SDNode::ANCHOR() {}
3607 void UnarySDNode::ANCHOR() {}
3608 void BinarySDNode::ANCHOR() {}
3609 void TernarySDNode::ANCHOR() {}
3610 void HandleSDNode::ANCHOR() {}
3611 void StringSDNode::ANCHOR() {}
3612 void ConstantSDNode::ANCHOR() {}
3613 void ConstantFPSDNode::ANCHOR() {}
3614 void GlobalAddressSDNode::ANCHOR() {}
3615 void FrameIndexSDNode::ANCHOR() {}
3616 void JumpTableSDNode::ANCHOR() {}
3617 void ConstantPoolSDNode::ANCHOR() {}
3618 void BasicBlockSDNode::ANCHOR() {}
3619 void SrcValueSDNode::ANCHOR() {}
3620 void MemOperandSDNode::ANCHOR() {}
3621 void RegisterSDNode::ANCHOR() {}
3622 void ExternalSymbolSDNode::ANCHOR() {}
3623 void CondCodeSDNode::ANCHOR() {}
3624 void ARG_FLAGSSDNode::ANCHOR() {}
3625 void VTSDNode::ANCHOR() {}
3626 void LoadSDNode::ANCHOR() {}
3627 void StoreSDNode::ANCHOR() {}
3628 void AtomicSDNode::ANCHOR() {}
3630 HandleSDNode::~HandleSDNode() {
3631 SDVTList VTs = { 0, 0 };
3632 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
3635 GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3636 MVT::ValueType VT, int o)
3637 : SDNode(isa<GlobalVariable>(GA) &&
3638 cast<GlobalVariable>(GA)->isThreadLocal() ?
3640 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3642 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3643 getSDVTList(VT)), Offset(o) {
3644 TheGlobal = const_cast<GlobalValue*>(GA);
3647 /// getMemOperand - Return a MemOperand object describing the memory
3648 /// reference performed by this load or store.
3649 MemOperand LSBaseSDNode::getMemOperand() const {
3650 int Size = (MVT::getSizeInBits(getMemoryVT()) + 7) >> 3;
3652 getOpcode() == ISD::LOAD ? MemOperand::MOLoad : MemOperand::MOStore;
3653 if (IsVolatile) Flags |= MemOperand::MOVolatile;
3655 // Check if the load references a frame index, and does not have
3657 const FrameIndexSDNode *FI =
3658 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
3659 if (!getSrcValue() && FI)
3660 return MemOperand(PseudoSourceValue::getFixedStack(), Flags,
3661 FI->getIndex(), Size, Alignment);
3663 return MemOperand(getSrcValue(), Flags,
3664 getSrcValueOffset(), Size, Alignment);
3667 /// Profile - Gather unique data for the node.
3669 void SDNode::Profile(FoldingSetNodeID &ID) {
3670 AddNodeIDNode(ID, this);
3673 /// getValueTypeList - Return a pointer to the specified value type.
3675 const MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
3676 if (MVT::isExtendedVT(VT)) {
3677 static std::set<MVT::ValueType> EVTs;
3678 return &(*EVTs.insert(VT).first);
3680 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3686 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3687 /// indicated value. This method ignores uses of other values defined by this
3689 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
3690 assert(Value < getNumValues() && "Bad value!");
3692 // If there is only one value, this is easy.
3693 if (getNumValues() == 1)
3694 return use_size() == NUses;
3695 if (use_size() < NUses) return false;
3697 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3699 SmallPtrSet<SDNode*, 32> UsersHandled;
3701 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3703 if (User->getNumOperands() == 1 ||
3704 UsersHandled.insert(User)) // First time we've seen this?
3705 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3706 if (User->getOperand(i) == TheValue) {
3708 return false; // too many uses
3713 // Found exactly the right number of uses?
3718 /// hasAnyUseOfValue - Return true if there are any use of the indicated
3719 /// value. This method ignores uses of other values defined by this operation.
3720 bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3721 assert(Value < getNumValues() && "Bad value!");
3723 if (use_empty()) return false;
3725 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3727 SmallPtrSet<SDNode*, 32> UsersHandled;
3729 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3731 if (User->getNumOperands() == 1 ||
3732 UsersHandled.insert(User)) // First time we've seen this?
3733 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3734 if (User->getOperand(i) == TheValue) {
3743 /// isOnlyUseOf - Return true if this node is the only use of N.
3745 bool SDNode::isOnlyUseOf(SDNode *N) const {
3747 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3758 /// isOperand - Return true if this node is an operand of N.
3760 bool SDOperand::isOperandOf(SDNode *N) const {
3761 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3762 if (*this == N->getOperand(i))
3767 bool SDNode::isOperandOf(SDNode *N) const {
3768 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3769 if (this == N->OperandList[i].Val)
3774 /// reachesChainWithoutSideEffects - Return true if this operand (which must
3775 /// be a chain) reaches the specified operand without crossing any
3776 /// side-effecting instructions. In practice, this looks through token
3777 /// factors and non-volatile loads. In order to remain efficient, this only
3778 /// looks a couple of nodes in, it does not do an exhaustive search.
3779 bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
3780 unsigned Depth) const {
3781 if (*this == Dest) return true;
3783 // Don't search too deeply, we just want to be able to see through
3784 // TokenFactor's etc.
3785 if (Depth == 0) return false;
3787 // If this is a token factor, all inputs to the TF happen in parallel. If any
3788 // of the operands of the TF reach dest, then we can do the xform.
3789 if (getOpcode() == ISD::TokenFactor) {
3790 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
3791 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
3796 // Loads don't have side effects, look through them.
3797 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
3798 if (!Ld->isVolatile())
3799 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
3805 static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
3806 SmallPtrSet<SDNode *, 32> &Visited) {
3807 if (found || !Visited.insert(N))
3810 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3811 SDNode *Op = N->getOperand(i).Val;
3816 findPredecessor(Op, P, found, Visited);
3820 /// isPredecessorOf - Return true if this node is a predecessor of N. This node
3821 /// is either an operand of N or it can be reached by recursively traversing
3822 /// up the operands.
3823 /// NOTE: this is an expensive method. Use it carefully.
3824 bool SDNode::isPredecessorOf(SDNode *N) const {
3825 SmallPtrSet<SDNode *, 32> Visited;
3827 findPredecessor(N, this, found, Visited);
3831 uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3832 assert(Num < NumOperands && "Invalid child # of SDNode!");
3833 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3836 std::string SDNode::getOperationName(const SelectionDAG *G) const {
3837 switch (getOpcode()) {
3839 if (getOpcode() < ISD::BUILTIN_OP_END)
3840 return "<<Unknown DAG Node>>";
3843 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
3844 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
3845 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
3847 TargetLowering &TLI = G->getTargetLoweringInfo();
3849 TLI.getTargetNodeName(getOpcode());
3850 if (Name) return Name;
3853 return "<<Unknown Target Node>>";
3856 case ISD::PREFETCH: return "Prefetch";
3857 case ISD::MEMBARRIER: return "MemBarrier";
3858 case ISD::ATOMIC_LCS: return "AtomicLCS";
3859 case ISD::ATOMIC_LAS: return "AtomicLAS";
3860 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
3861 case ISD::PCMARKER: return "PCMarker";
3862 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
3863 case ISD::SRCVALUE: return "SrcValue";
3864 case ISD::MEMOPERAND: return "MemOperand";
3865 case ISD::EntryToken: return "EntryToken";
3866 case ISD::TokenFactor: return "TokenFactor";
3867 case ISD::AssertSext: return "AssertSext";
3868 case ISD::AssertZext: return "AssertZext";
3870 case ISD::STRING: return "String";
3871 case ISD::BasicBlock: return "BasicBlock";
3872 case ISD::ARG_FLAGS: return "ArgFlags";
3873 case ISD::VALUETYPE: return "ValueType";
3874 case ISD::Register: return "Register";
3876 case ISD::Constant: return "Constant";
3877 case ISD::ConstantFP: return "ConstantFP";
3878 case ISD::GlobalAddress: return "GlobalAddress";
3879 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
3880 case ISD::FrameIndex: return "FrameIndex";
3881 case ISD::JumpTable: return "JumpTable";
3882 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
3883 case ISD::RETURNADDR: return "RETURNADDR";
3884 case ISD::FRAMEADDR: return "FRAMEADDR";
3885 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
3886 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3887 case ISD::EHSELECTION: return "EHSELECTION";
3888 case ISD::EH_RETURN: return "EH_RETURN";
3889 case ISD::ConstantPool: return "ConstantPool";
3890 case ISD::ExternalSymbol: return "ExternalSymbol";
3891 case ISD::INTRINSIC_WO_CHAIN: {
3892 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3893 return Intrinsic::getName((Intrinsic::ID)IID);
3895 case ISD::INTRINSIC_VOID:
3896 case ISD::INTRINSIC_W_CHAIN: {
3897 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
3898 return Intrinsic::getName((Intrinsic::ID)IID);
3901 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
3902 case ISD::TargetConstant: return "TargetConstant";
3903 case ISD::TargetConstantFP:return "TargetConstantFP";
3904 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
3905 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
3906 case ISD::TargetFrameIndex: return "TargetFrameIndex";
3907 case ISD::TargetJumpTable: return "TargetJumpTable";
3908 case ISD::TargetConstantPool: return "TargetConstantPool";
3909 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
3911 case ISD::CopyToReg: return "CopyToReg";
3912 case ISD::CopyFromReg: return "CopyFromReg";
3913 case ISD::UNDEF: return "undef";
3914 case ISD::MERGE_VALUES: return "merge_values";
3915 case ISD::INLINEASM: return "inlineasm";
3916 case ISD::LABEL: return "label";
3917 case ISD::DECLARE: return "declare";
3918 case ISD::HANDLENODE: return "handlenode";
3919 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
3920 case ISD::CALL: return "call";
3923 case ISD::FABS: return "fabs";
3924 case ISD::FNEG: return "fneg";
3925 case ISD::FSQRT: return "fsqrt";
3926 case ISD::FSIN: return "fsin";
3927 case ISD::FCOS: return "fcos";
3928 case ISD::FPOWI: return "fpowi";
3929 case ISD::FPOW: return "fpow";
3932 case ISD::ADD: return "add";
3933 case ISD::SUB: return "sub";
3934 case ISD::MUL: return "mul";
3935 case ISD::MULHU: return "mulhu";
3936 case ISD::MULHS: return "mulhs";
3937 case ISD::SDIV: return "sdiv";
3938 case ISD::UDIV: return "udiv";
3939 case ISD::SREM: return "srem";
3940 case ISD::UREM: return "urem";
3941 case ISD::SMUL_LOHI: return "smul_lohi";
3942 case ISD::UMUL_LOHI: return "umul_lohi";
3943 case ISD::SDIVREM: return "sdivrem";
3944 case ISD::UDIVREM: return "divrem";
3945 case ISD::AND: return "and";
3946 case ISD::OR: return "or";
3947 case ISD::XOR: return "xor";
3948 case ISD::SHL: return "shl";
3949 case ISD::SRA: return "sra";
3950 case ISD::SRL: return "srl";
3951 case ISD::ROTL: return "rotl";
3952 case ISD::ROTR: return "rotr";
3953 case ISD::FADD: return "fadd";
3954 case ISD::FSUB: return "fsub";
3955 case ISD::FMUL: return "fmul";
3956 case ISD::FDIV: return "fdiv";
3957 case ISD::FREM: return "frem";
3958 case ISD::FCOPYSIGN: return "fcopysign";
3959 case ISD::FGETSIGN: return "fgetsign";
3961 case ISD::SETCC: return "setcc";
3962 case ISD::SELECT: return "select";
3963 case ISD::SELECT_CC: return "select_cc";
3964 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
3965 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
3966 case ISD::CONCAT_VECTORS: return "concat_vectors";
3967 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
3968 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
3969 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
3970 case ISD::CARRY_FALSE: return "carry_false";
3971 case ISD::ADDC: return "addc";
3972 case ISD::ADDE: return "adde";
3973 case ISD::SUBC: return "subc";
3974 case ISD::SUBE: return "sube";
3975 case ISD::SHL_PARTS: return "shl_parts";
3976 case ISD::SRA_PARTS: return "sra_parts";
3977 case ISD::SRL_PARTS: return "srl_parts";
3979 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3980 case ISD::INSERT_SUBREG: return "insert_subreg";
3982 // Conversion operators.
3983 case ISD::SIGN_EXTEND: return "sign_extend";
3984 case ISD::ZERO_EXTEND: return "zero_extend";
3985 case ISD::ANY_EXTEND: return "any_extend";
3986 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
3987 case ISD::TRUNCATE: return "truncate";
3988 case ISD::FP_ROUND: return "fp_round";
3989 case ISD::FLT_ROUNDS_: return "flt_rounds";
3990 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
3991 case ISD::FP_EXTEND: return "fp_extend";
3993 case ISD::SINT_TO_FP: return "sint_to_fp";
3994 case ISD::UINT_TO_FP: return "uint_to_fp";
3995 case ISD::FP_TO_SINT: return "fp_to_sint";
3996 case ISD::FP_TO_UINT: return "fp_to_uint";
3997 case ISD::BIT_CONVERT: return "bit_convert";
3999 // Control flow instructions
4000 case ISD::BR: return "br";
4001 case ISD::BRIND: return "brind";
4002 case ISD::BR_JT: return "br_jt";
4003 case ISD::BRCOND: return "brcond";
4004 case ISD::BR_CC: return "br_cc";
4005 case ISD::RET: return "ret";
4006 case ISD::CALLSEQ_START: return "callseq_start";
4007 case ISD::CALLSEQ_END: return "callseq_end";
4010 case ISD::LOAD: return "load";
4011 case ISD::STORE: return "store";
4012 case ISD::VAARG: return "vaarg";
4013 case ISD::VACOPY: return "vacopy";
4014 case ISD::VAEND: return "vaend";
4015 case ISD::VASTART: return "vastart";
4016 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
4017 case ISD::EXTRACT_ELEMENT: return "extract_element";
4018 case ISD::BUILD_PAIR: return "build_pair";
4019 case ISD::STACKSAVE: return "stacksave";
4020 case ISD::STACKRESTORE: return "stackrestore";
4021 case ISD::TRAP: return "trap";
4023 // Block memory operations.
4024 case ISD::MEMSET: return "memset";
4025 case ISD::MEMCPY: return "memcpy";
4026 case ISD::MEMMOVE: return "memmove";
4029 case ISD::BSWAP: return "bswap";
4030 case ISD::CTPOP: return "ctpop";
4031 case ISD::CTTZ: return "cttz";
4032 case ISD::CTLZ: return "ctlz";
4035 case ISD::LOCATION: return "location";
4036 case ISD::DEBUG_LOC: return "debug_loc";
4039 case ISD::TRAMPOLINE: return "trampoline";
4042 switch (cast<CondCodeSDNode>(this)->get()) {
4043 default: assert(0 && "Unknown setcc condition!");
4044 case ISD::SETOEQ: return "setoeq";
4045 case ISD::SETOGT: return "setogt";
4046 case ISD::SETOGE: return "setoge";
4047 case ISD::SETOLT: return "setolt";
4048 case ISD::SETOLE: return "setole";
4049 case ISD::SETONE: return "setone";
4051 case ISD::SETO: return "seto";
4052 case ISD::SETUO: return "setuo";
4053 case ISD::SETUEQ: return "setue";
4054 case ISD::SETUGT: return "setugt";
4055 case ISD::SETUGE: return "setuge";
4056 case ISD::SETULT: return "setult";
4057 case ISD::SETULE: return "setule";
4058 case ISD::SETUNE: return "setune";
4060 case ISD::SETEQ: return "seteq";
4061 case ISD::SETGT: return "setgt";
4062 case ISD::SETGE: return "setge";
4063 case ISD::SETLT: return "setlt";
4064 case ISD::SETLE: return "setle";
4065 case ISD::SETNE: return "setne";
4070 const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
4079 return "<post-inc>";
4081 return "<post-dec>";
4085 std::string ISD::ArgFlagsTy::getArgFlagsString() {
4086 std::string S = "< ";
4100 if (getByValAlign())
4101 S += "byval-align:" + utostr(getByValAlign()) + " ";
4103 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4105 S += "byval-size:" + utostr(getByValSize()) + " ";
4109 void SDNode::dump() const { dump(0); }
4110 void SDNode::dump(const SelectionDAG *G) const {
4111 cerr << (void*)this << ": ";
4113 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
4115 if (getValueType(i) == MVT::Other)
4118 cerr << MVT::getValueTypeString(getValueType(i));
4120 cerr << " = " << getOperationName(G);
4123 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
4124 if (i) cerr << ", ";
4125 cerr << (void*)getOperand(i).Val;
4126 if (unsigned RN = getOperand(i).ResNo)
4130 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4131 SDNode *Mask = getOperand(2).Val;
4133 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4135 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4138 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4143 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
4144 cerr << "<" << CSDN->getValue() << ">";
4145 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
4146 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4147 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4148 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4149 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4151 cerr << "<APFloat(";
4152 CSDN->getValueAPF().convertToAPInt().dump();
4155 } else if (const GlobalAddressSDNode *GADN =
4156 dyn_cast<GlobalAddressSDNode>(this)) {
4157 int offset = GADN->getOffset();
4159 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
4161 cerr << " + " << offset;
4163 cerr << " " << offset;
4164 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
4165 cerr << "<" << FIDN->getIndex() << ">";
4166 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
4167 cerr << "<" << JTDN->getIndex() << ">";
4168 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
4169 int offset = CP->getOffset();
4170 if (CP->isMachineConstantPoolEntry())
4171 cerr << "<" << *CP->getMachineCPVal() << ">";
4173 cerr << "<" << *CP->getConstVal() << ">";
4175 cerr << " + " << offset;
4177 cerr << " " << offset;
4178 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
4180 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4182 cerr << LBB->getName() << " ";
4183 cerr << (const void*)BBDN->getBasicBlock() << ">";
4184 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
4185 if (G && R->getReg() &&
4186 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
4187 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
4189 cerr << " #" << R->getReg();
4191 } else if (const ExternalSymbolSDNode *ES =
4192 dyn_cast<ExternalSymbolSDNode>(this)) {
4193 cerr << "'" << ES->getSymbol() << "'";
4194 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4196 cerr << "<" << M->getValue() << ">";
4199 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4200 if (M->MO.getValue())
4201 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4203 cerr << "<null:" << M->MO.getOffset() << ">";
4204 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
4205 cerr << N->getArgFlags().getArgFlagsString();
4206 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
4207 cerr << ":" << MVT::getValueTypeString(N->getVT());
4208 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
4209 const Value *SrcValue = LD->getSrcValue();
4210 int SrcOffset = LD->getSrcValueOffset();
4216 cerr << ":" << SrcOffset << ">";
4219 switch (LD->getExtensionType()) {
4220 default: doExt = false; break;
4222 cerr << " <anyext ";
4232 cerr << MVT::getValueTypeString(LD->getMemoryVT()) << ">";
4234 const char *AM = getIndexedModeName(LD->getAddressingMode());
4237 if (LD->isVolatile())
4238 cerr << " <volatile>";
4239 cerr << " alignment=" << LD->getAlignment();
4240 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
4241 const Value *SrcValue = ST->getSrcValue();
4242 int SrcOffset = ST->getSrcValueOffset();
4248 cerr << ":" << SrcOffset << ">";
4250 if (ST->isTruncatingStore())
4252 << MVT::getValueTypeString(ST->getMemoryVT()) << ">";
4254 const char *AM = getIndexedModeName(ST->getAddressingMode());
4257 if (ST->isVolatile())
4258 cerr << " <volatile>";
4259 cerr << " alignment=" << ST->getAlignment();
4263 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
4264 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4265 if (N->getOperand(i).Val->hasOneUse())
4266 DumpNodes(N->getOperand(i).Val, indent+2, G);
4268 cerr << "\n" << std::string(indent+2, ' ')
4269 << (void*)N->getOperand(i).Val << ": <multiple use>";
4272 cerr << "\n" << std::string(indent, ' ');
4276 void SelectionDAG::dump() const {
4277 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
4278 std::vector<const SDNode*> Nodes;
4279 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4283 std::sort(Nodes.begin(), Nodes.end());
4285 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
4286 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
4287 DumpNodes(Nodes[i], 2, this);
4290 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
4295 const Type *ConstantPoolSDNode::getType() const {
4296 if (isMachineConstantPoolEntry())
4297 return Val.MachineCPVal->getType();
4298 return Val.ConstVal->getType();