1 //===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/GlobalValue.h"
17 #include "llvm/Intrinsics.h"
18 #include "llvm/Assembly/Writer.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/Support/MathExtras.h"
21 #include "llvm/Target/MRegisterInfo.h"
22 #include "llvm/Target/TargetLowering.h"
23 #include "llvm/Target/TargetInstrInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/ADT/SetVector.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/StringExtras.h"
34 static bool isCommutativeBinOp(unsigned Opcode) {
44 case ISD::XOR: return true;
45 default: return false; // FIXME: Need commutative info for user ops!
49 // isInvertibleForFree - Return true if there is no cost to emitting the logical
50 // inverse of this node.
51 static bool isInvertibleForFree(SDOperand N) {
52 if (isa<ConstantSDNode>(N.Val)) return true;
53 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
58 //===----------------------------------------------------------------------===//
59 // ConstantFPSDNode Class
60 //===----------------------------------------------------------------------===//
62 /// isExactlyValue - We don't rely on operator== working on double values, as
63 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
64 /// As such, this method can be used to do an exact bit-for-bit comparison of
65 /// two floating point values.
66 bool ConstantFPSDNode::isExactlyValue(double V) const {
67 return DoubleToBits(V) == DoubleToBits(Value);
70 //===----------------------------------------------------------------------===//
72 //===----------------------------------------------------------------------===//
74 /// isBuildVectorAllOnes - Return true if the specified node is a
75 /// BUILD_VECTOR where all of the elements are ~0 or undef.
76 bool ISD::isBuildVectorAllOnes(const SDNode *N) {
77 // Look through a bit convert.
78 if (N->getOpcode() == ISD::BIT_CONVERT)
79 N = N->getOperand(0).Val;
81 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
83 unsigned i = 0, e = N->getNumOperands();
85 // Skip over all of the undef values.
86 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
89 // Do not accept an all-undef vector.
90 if (i == e) return false;
92 // Do not accept build_vectors that aren't all constants or which have non-~0
94 SDOperand NotZero = N->getOperand(i);
95 if (isa<ConstantSDNode>(NotZero)) {
96 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
98 } else if (isa<ConstantFPSDNode>(NotZero)) {
99 MVT::ValueType VT = NotZero.getValueType();
101 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
105 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
112 // Okay, we have at least one ~0 value, check to see if the rest match or are
114 for (++i; i != e; ++i)
115 if (N->getOperand(i) != NotZero &&
116 N->getOperand(i).getOpcode() != ISD::UNDEF)
122 /// isBuildVectorAllZeros - Return true if the specified node is a
123 /// BUILD_VECTOR where all of the elements are 0 or undef.
124 bool ISD::isBuildVectorAllZeros(const SDNode *N) {
125 // Look through a bit convert.
126 if (N->getOpcode() == ISD::BIT_CONVERT)
127 N = N->getOperand(0).Val;
129 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
131 unsigned i = 0, e = N->getNumOperands();
133 // Skip over all of the undef values.
134 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
137 // Do not accept an all-undef vector.
138 if (i == e) return false;
140 // Do not accept build_vectors that aren't all constants or which have non-~0
142 SDOperand Zero = N->getOperand(i);
143 if (isa<ConstantSDNode>(Zero)) {
144 if (!cast<ConstantSDNode>(Zero)->isNullValue())
146 } else if (isa<ConstantFPSDNode>(Zero)) {
147 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
152 // Okay, we have at least one ~0 value, check to see if the rest match or are
154 for (++i; i != e; ++i)
155 if (N->getOperand(i) != Zero &&
156 N->getOperand(i).getOpcode() != ISD::UNDEF)
161 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
162 /// when given the operation for (X op Y).
163 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
164 // To perform this operation, we just need to swap the L and G bits of the
166 unsigned OldL = (Operation >> 2) & 1;
167 unsigned OldG = (Operation >> 1) & 1;
168 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
169 (OldL << 1) | // New G bit
170 (OldG << 2)); // New L bit.
173 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
174 /// 'op' is a valid SetCC operation.
175 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
176 unsigned Operation = Op;
178 Operation ^= 7; // Flip L, G, E bits, but not U.
180 Operation ^= 15; // Flip all of the condition bits.
181 if (Operation > ISD::SETTRUE2)
182 Operation &= ~8; // Don't let N and U bits get set.
183 return ISD::CondCode(Operation);
187 /// isSignedOp - For an integer comparison, return 1 if the comparison is a
188 /// signed operation and 2 if the result is an unsigned comparison. Return zero
189 /// if the operation does not depend on the sign of the input (setne and seteq).
190 static int isSignedOp(ISD::CondCode Opcode) {
192 default: assert(0 && "Illegal integer setcc operation!");
194 case ISD::SETNE: return 0;
198 case ISD::SETGE: return 1;
202 case ISD::SETUGE: return 2;
206 /// getSetCCOrOperation - Return the result of a logical OR between different
207 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
208 /// returns SETCC_INVALID if it is not possible to represent the resultant
210 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
212 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
213 // Cannot fold a signed integer setcc with an unsigned integer setcc.
214 return ISD::SETCC_INVALID;
216 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
218 // If the N and U bits get set then the resultant comparison DOES suddenly
219 // care about orderedness, and is true when ordered.
220 if (Op > ISD::SETTRUE2)
221 Op &= ~16; // Clear the U bit if the N bit is set.
223 // Canonicalize illegal integer setcc's.
224 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
227 return ISD::CondCode(Op);
230 /// getSetCCAndOperation - Return the result of a logical AND between different
231 /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
232 /// function returns zero if it is not possible to represent the resultant
234 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
236 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
237 // Cannot fold a signed setcc with an unsigned setcc.
238 return ISD::SETCC_INVALID;
240 // Combine all of the condition bits.
241 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
243 // Canonicalize illegal integer setcc's.
247 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
248 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
249 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
250 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
257 const TargetMachine &SelectionDAG::getTarget() const {
258 return TLI.getTargetMachine();
261 //===----------------------------------------------------------------------===//
262 // SelectionDAG Class
263 //===----------------------------------------------------------------------===//
265 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
267 void SelectionDAG::RemoveDeadNodes() {
268 // Create a dummy node (which is not added to allnodes), that adds a reference
269 // to the root node, preventing it from being deleted.
270 HandleSDNode Dummy(getRoot());
272 SmallVector<SDNode*, 128> DeadNodes;
274 // Add all obviously-dead nodes to the DeadNodes worklist.
275 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
277 DeadNodes.push_back(I);
279 // Process the worklist, deleting the nodes and adding their uses to the
281 while (!DeadNodes.empty()) {
282 SDNode *N = DeadNodes.back();
283 DeadNodes.pop_back();
285 // Take the node out of the appropriate CSE map.
286 RemoveNodeFromCSEMaps(N);
288 // Next, brutally remove the operand list. This is safe to do, as there are
289 // no cycles in the graph.
290 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
291 SDNode *Operand = I->Val;
292 Operand->removeUser(N);
294 // Now that we removed this operand, see if there are no uses of it left.
295 if (Operand->use_empty())
296 DeadNodes.push_back(Operand);
298 delete[] N->OperandList;
302 // Finally, remove N itself.
306 // If the root changed (e.g. it was a dead load, update the root).
307 setRoot(Dummy.getValue());
310 void SelectionDAG::DeleteNode(SDNode *N) {
311 assert(N->use_empty() && "Cannot delete a node that is not dead!");
313 // First take this out of the appropriate CSE map.
314 RemoveNodeFromCSEMaps(N);
316 // Finally, remove uses due to operands of this node, remove from the
317 // AllNodes list, and delete the node.
318 DeleteNodeNotInCSEMaps(N);
321 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
323 // Remove it from the AllNodes list.
326 // Drop all of the operands and decrement used nodes use counts.
327 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
328 I->Val->removeUser(N);
329 delete[] N->OperandList;
336 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
337 /// correspond to it. This is useful when we're about to delete or repurpose
338 /// the node. We don't want future request for structurally identical nodes
339 /// to return N anymore.
340 void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
342 switch (N->getOpcode()) {
343 case ISD::HANDLENODE: return; // noop.
345 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
346 N->getValueType(0)));
348 case ISD::TargetConstant:
349 Erased = TargetConstants.erase(std::make_pair(
350 cast<ConstantSDNode>(N)->getValue(),
351 N->getValueType(0)));
353 case ISD::ConstantFP: {
354 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
355 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
358 case ISD::TargetConstantFP: {
359 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
360 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
364 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
367 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
368 "Cond code doesn't exist!");
369 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
370 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
372 case ISD::GlobalAddress: {
373 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
374 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
378 case ISD::TargetGlobalAddress: {
379 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
380 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
384 case ISD::FrameIndex:
385 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
387 case ISD::TargetFrameIndex:
388 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
391 Erased = JumpTableIndices.erase(cast<JumpTableSDNode>(N)->getIndex());
393 case ISD::TargetJumpTable:
395 TargetJumpTableIndices.erase(cast<JumpTableSDNode>(N)->getIndex());
397 case ISD::ConstantPool:
398 Erased = ConstantPoolIndices.
399 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
400 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
401 cast<ConstantPoolSDNode>(N)->getAlignment())));
403 case ISD::TargetConstantPool:
404 Erased = TargetConstantPoolIndices.
405 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
406 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
407 cast<ConstantPoolSDNode>(N)->getAlignment())));
409 case ISD::BasicBlock:
410 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
412 case ISD::ExternalSymbol:
413 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
415 case ISD::TargetExternalSymbol:
417 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
420 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
421 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
424 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
425 N->getValueType(0)));
427 case ISD::SRCVALUE: {
428 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
429 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
433 if (N->getNumValues() == 1) {
434 if (N->getNumOperands() == 0) {
435 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
436 N->getValueType(0)));
438 // Remove it from the CSE Map.
439 Erased = CSEMap.RemoveNode(N);
442 // Remove it from the CSE Map.
443 Erased = CSEMap.RemoveNode(N);
448 // Verify that the node was actually in one of the CSE maps, unless it has a
449 // flag result (which cannot be CSE'd) or is one of the special cases that are
450 // not subject to CSE.
451 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
452 !N->isTargetOpcode()) {
455 assert(0 && "Node is not in map!");
460 /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
461 /// has been taken out and modified in some way. If the specified node already
462 /// exists in the CSE maps, do not modify the maps, but return the existing node
463 /// instead. If it doesn't exist, add it and return null.
465 SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
466 assert(N->getNumOperands() && "This is a leaf node!");
467 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
468 return 0; // Never add these nodes.
470 // Check that remaining values produced are not flags.
471 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
472 if (N->getValueType(i) == MVT::Flag)
473 return 0; // Never CSE anything that produces a flag.
475 SDNode *New = CSEMap.GetOrInsertNode(N);
476 if (New != N) return New; // Node already existed.
480 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
481 /// were replaced with those specified. If this node is never memoized,
482 /// return null, otherwise return a pointer to the slot it would take. If a
483 /// node already exists with these operands, the slot will be non-null.
484 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
486 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
487 return 0; // Never add these nodes.
489 // Check that remaining values produced are not flags.
490 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
491 if (N->getValueType(i) == MVT::Flag)
492 return 0; // Never CSE anything that produces a flag.
494 SelectionDAGCSEMap::NodeID ID;
495 ID.SetOpcode(N->getOpcode());
496 ID.SetValueTypes(N->value_begin());
498 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
501 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
502 /// were replaced with those specified. If this node is never memoized,
503 /// return null, otherwise return a pointer to the slot it would take. If a
504 /// node already exists with these operands, the slot will be non-null.
505 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
506 SDOperand Op1, SDOperand Op2,
508 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
509 return 0; // Never add these nodes.
511 // Check that remaining values produced are not flags.
512 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
513 if (N->getValueType(i) == MVT::Flag)
514 return 0; // Never CSE anything that produces a flag.
516 SelectionDAGCSEMap::NodeID ID;
517 ID.SetOpcode(N->getOpcode());
518 ID.SetValueTypes(N->value_begin());
519 ID.SetOperands(Op1, Op2);
520 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
524 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
525 /// were replaced with those specified. If this node is never memoized,
526 /// return null, otherwise return a pointer to the slot it would take. If a
527 /// node already exists with these operands, the slot will be non-null.
528 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
529 const std::vector<SDOperand> &Ops,
531 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
532 return 0; // Never add these nodes.
534 // Check that remaining values produced are not flags.
535 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
536 if (N->getValueType(i) == MVT::Flag)
537 return 0; // Never CSE anything that produces a flag.
539 SelectionDAGCSEMap::NodeID ID;
540 ID.SetOpcode(N->getOpcode());
541 ID.SetValueTypes(N->value_begin());
542 ID.SetOperands(&Ops[0], Ops.size());
543 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
547 SelectionDAG::~SelectionDAG() {
548 while (!AllNodes.empty()) {
549 SDNode *N = AllNodes.begin();
550 delete [] N->OperandList;
553 AllNodes.pop_front();
557 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
558 if (Op.getValueType() == VT) return Op;
559 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
560 return getNode(ISD::AND, Op.getValueType(), Op,
561 getConstant(Imm, Op.getValueType()));
564 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
565 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
566 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
568 // Mask out any bits that are not valid for this constant.
570 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
572 SDNode *&N = Constants[std::make_pair(Val, VT)];
573 if (N) return SDOperand(N, 0);
574 N = new ConstantSDNode(false, Val, VT);
575 AllNodes.push_back(N);
576 return SDOperand(N, 0);
579 SDOperand SelectionDAG::getString(const std::string &Val) {
580 StringSDNode *&N = StringNodes[Val];
582 N = new StringSDNode(Val);
583 AllNodes.push_back(N);
585 return SDOperand(N, 0);
588 SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
589 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
590 // Mask out any bits that are not valid for this constant.
592 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
594 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
595 if (N) return SDOperand(N, 0);
596 N = new ConstantSDNode(true, Val, VT);
597 AllNodes.push_back(N);
598 return SDOperand(N, 0);
601 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
602 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
604 Val = (float)Val; // Mask out extra precision.
606 // Do the map lookup using the actual bit pattern for the floating point
607 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
608 // we don't have issues with SNANs.
609 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
610 if (N) return SDOperand(N, 0);
611 N = new ConstantFPSDNode(false, Val, VT);
612 AllNodes.push_back(N);
613 return SDOperand(N, 0);
616 SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
617 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
619 Val = (float)Val; // Mask out extra precision.
621 // Do the map lookup using the actual bit pattern for the floating point
622 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
623 // we don't have issues with SNANs.
624 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
625 if (N) return SDOperand(N, 0);
626 N = new ConstantFPSDNode(true, Val, VT);
627 AllNodes.push_back(N);
628 return SDOperand(N, 0);
631 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
632 MVT::ValueType VT, int offset) {
633 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
634 if (N) return SDOperand(N, 0);
635 N = new GlobalAddressSDNode(false, GV, VT, offset);
636 AllNodes.push_back(N);
637 return SDOperand(N, 0);
640 SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
641 MVT::ValueType VT, int offset) {
642 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
643 if (N) return SDOperand(N, 0);
644 N = new GlobalAddressSDNode(true, GV, VT, offset);
645 AllNodes.push_back(N);
646 return SDOperand(N, 0);
649 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
650 SDNode *&N = FrameIndices[FI];
651 if (N) return SDOperand(N, 0);
652 N = new FrameIndexSDNode(FI, VT, false);
653 AllNodes.push_back(N);
654 return SDOperand(N, 0);
657 SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
658 SDNode *&N = TargetFrameIndices[FI];
659 if (N) return SDOperand(N, 0);
660 N = new FrameIndexSDNode(FI, VT, true);
661 AllNodes.push_back(N);
662 return SDOperand(N, 0);
665 SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT) {
666 SDNode *&N = JumpTableIndices[JTI];
667 if (N) return SDOperand(N, 0);
668 N = new JumpTableSDNode(JTI, VT, false);
669 AllNodes.push_back(N);
670 return SDOperand(N, 0);
673 SDOperand SelectionDAG::getTargetJumpTable(int JTI, MVT::ValueType VT) {
674 SDNode *&N = TargetJumpTableIndices[JTI];
675 if (N) return SDOperand(N, 0);
676 N = new JumpTableSDNode(JTI, VT, true);
677 AllNodes.push_back(N);
678 return SDOperand(N, 0);
681 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
682 unsigned Alignment, int Offset) {
683 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
684 std::make_pair(Offset, Alignment))];
685 if (N) return SDOperand(N, 0);
686 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
687 AllNodes.push_back(N);
688 return SDOperand(N, 0);
691 SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
692 unsigned Alignment, int Offset) {
693 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
694 std::make_pair(Offset, Alignment))];
695 if (N) return SDOperand(N, 0);
696 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
697 AllNodes.push_back(N);
698 return SDOperand(N, 0);
701 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
702 SDNode *&N = BBNodes[MBB];
703 if (N) return SDOperand(N, 0);
704 N = new BasicBlockSDNode(MBB);
705 AllNodes.push_back(N);
706 return SDOperand(N, 0);
709 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
710 if ((unsigned)VT >= ValueTypeNodes.size())
711 ValueTypeNodes.resize(VT+1);
712 if (ValueTypeNodes[VT] == 0) {
713 ValueTypeNodes[VT] = new VTSDNode(VT);
714 AllNodes.push_back(ValueTypeNodes[VT]);
717 return SDOperand(ValueTypeNodes[VT], 0);
720 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
721 SDNode *&N = ExternalSymbols[Sym];
722 if (N) return SDOperand(N, 0);
723 N = new ExternalSymbolSDNode(false, Sym, VT);
724 AllNodes.push_back(N);
725 return SDOperand(N, 0);
728 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
730 SDNode *&N = TargetExternalSymbols[Sym];
731 if (N) return SDOperand(N, 0);
732 N = new ExternalSymbolSDNode(true, Sym, VT);
733 AllNodes.push_back(N);
734 return SDOperand(N, 0);
737 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
738 if ((unsigned)Cond >= CondCodeNodes.size())
739 CondCodeNodes.resize(Cond+1);
741 if (CondCodeNodes[Cond] == 0) {
742 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
743 AllNodes.push_back(CondCodeNodes[Cond]);
745 return SDOperand(CondCodeNodes[Cond], 0);
748 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
749 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
751 Reg = new RegisterSDNode(RegNo, VT);
752 AllNodes.push_back(Reg);
754 return SDOperand(Reg, 0);
757 SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
758 SDOperand N2, ISD::CondCode Cond) {
759 // These setcc operations always fold.
763 case ISD::SETFALSE2: return getConstant(0, VT);
765 case ISD::SETTRUE2: return getConstant(1, VT);
777 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
781 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
782 uint64_t C2 = N2C->getValue();
783 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
784 uint64_t C1 = N1C->getValue();
786 // Sign extend the operands if required
787 if (ISD::isSignedIntSetCC(Cond)) {
788 C1 = N1C->getSignExtended();
789 C2 = N2C->getSignExtended();
793 default: assert(0 && "Unknown integer setcc!");
794 case ISD::SETEQ: return getConstant(C1 == C2, VT);
795 case ISD::SETNE: return getConstant(C1 != C2, VT);
796 case ISD::SETULT: return getConstant(C1 < C2, VT);
797 case ISD::SETUGT: return getConstant(C1 > C2, VT);
798 case ISD::SETULE: return getConstant(C1 <= C2, VT);
799 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
800 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
801 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
802 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
803 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
806 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
807 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
808 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
810 // If the comparison constant has bits in the upper part, the
811 // zero-extended value could never match.
812 if (C2 & (~0ULL << InSize)) {
813 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
817 case ISD::SETEQ: return getConstant(0, VT);
820 case ISD::SETNE: return getConstant(1, VT);
823 // True if the sign bit of C2 is set.
824 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
827 // True if the sign bit of C2 isn't set.
828 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
834 // Otherwise, we can perform the comparison with the low bits.
842 return getSetCC(VT, N1.getOperand(0),
843 getConstant(C2, N1.getOperand(0).getValueType()),
846 break; // todo, be more careful with signed comparisons
848 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
849 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
850 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
851 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
852 MVT::ValueType ExtDstTy = N1.getValueType();
853 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
855 // If the extended part has any inconsistent bits, it cannot ever
856 // compare equal. In other words, they have to be all ones or all
859 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
860 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
861 return getConstant(Cond == ISD::SETNE, VT);
863 // Otherwise, make this a use of a zext.
864 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
865 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
869 uint64_t MinVal, MaxVal;
870 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
871 if (ISD::isSignedIntSetCC(Cond)) {
872 MinVal = 1ULL << (OperandBitSize-1);
873 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
874 MaxVal = ~0ULL >> (65-OperandBitSize);
879 MaxVal = ~0ULL >> (64-OperandBitSize);
882 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
883 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
884 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
885 --C2; // X >= C1 --> X > (C1-1)
886 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
887 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
890 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
891 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
892 ++C2; // X <= C1 --> X < (C1+1)
893 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
894 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
897 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
898 return getConstant(0, VT); // X < MIN --> false
900 // Canonicalize setgt X, Min --> setne X, Min
901 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
902 return getSetCC(VT, N1, N2, ISD::SETNE);
904 // If we have setult X, 1, turn it into seteq X, 0
905 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
906 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
908 // If we have setugt X, Max-1, turn it into seteq X, Max
909 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
910 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
913 // If we have "setcc X, C1", check to see if we can shrink the immediate
916 // SETUGT X, SINTMAX -> SETLT X, 0
917 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
918 C2 == (~0ULL >> (65-OperandBitSize)))
919 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
921 // FIXME: Implement the rest of these.
924 // Fold bit comparisons when we can.
925 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
926 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
927 if (ConstantSDNode *AndRHS =
928 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
929 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
930 // Perform the xform if the AND RHS is a single bit.
931 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
932 return getNode(ISD::SRL, VT, N1,
933 getConstant(Log2_64(AndRHS->getValue()),
934 TLI.getShiftAmountTy()));
936 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
937 // (X & 8) == 8 --> (X & 8) >> 3
938 // Perform the xform if C2 is a single bit.
939 if ((C2 & (C2-1)) == 0) {
940 return getNode(ISD::SRL, VT, N1,
941 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
946 } else if (isa<ConstantSDNode>(N1.Val)) {
947 // Ensure that the constant occurs on the RHS.
948 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
951 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
952 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
953 double C1 = N1C->getValue(), C2 = N2C->getValue();
956 default: break; // FIXME: Implement the rest of these!
957 case ISD::SETEQ: return getConstant(C1 == C2, VT);
958 case ISD::SETNE: return getConstant(C1 != C2, VT);
959 case ISD::SETLT: return getConstant(C1 < C2, VT);
960 case ISD::SETGT: return getConstant(C1 > C2, VT);
961 case ISD::SETLE: return getConstant(C1 <= C2, VT);
962 case ISD::SETGE: return getConstant(C1 >= C2, VT);
965 // Ensure that the constant occurs on the RHS.
966 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
969 // Could not fold it.
973 /// getNode - Gets or creates the specified node.
975 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
976 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
978 N = new SDNode(Opcode, VT);
979 AllNodes.push_back(N);
981 return SDOperand(N, 0);
984 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
987 // Constant fold unary operations with an integer constant operand.
988 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
989 uint64_t Val = C->getValue();
992 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
993 case ISD::ANY_EXTEND:
994 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
995 case ISD::TRUNCATE: return getConstant(Val, VT);
996 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
997 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
998 case ISD::BIT_CONVERT:
999 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
1000 return getConstantFP(BitsToFloat(Val), VT);
1001 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
1002 return getConstantFP(BitsToDouble(Val), VT);
1006 default: assert(0 && "Invalid bswap!"); break;
1007 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1008 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1009 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1014 default: assert(0 && "Invalid ctpop!"); break;
1015 case MVT::i1: return getConstant(Val != 0, VT);
1017 Tmp1 = (unsigned)Val & 0xFF;
1018 return getConstant(CountPopulation_32(Tmp1), VT);
1020 Tmp1 = (unsigned)Val & 0xFFFF;
1021 return getConstant(CountPopulation_32(Tmp1), VT);
1023 return getConstant(CountPopulation_32((unsigned)Val), VT);
1025 return getConstant(CountPopulation_64(Val), VT);
1029 default: assert(0 && "Invalid ctlz!"); break;
1030 case MVT::i1: return getConstant(Val == 0, VT);
1032 Tmp1 = (unsigned)Val & 0xFF;
1033 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1035 Tmp1 = (unsigned)Val & 0xFFFF;
1036 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1038 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1040 return getConstant(CountLeadingZeros_64(Val), VT);
1044 default: assert(0 && "Invalid cttz!"); break;
1045 case MVT::i1: return getConstant(Val == 0, VT);
1047 Tmp1 = (unsigned)Val | 0x100;
1048 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1050 Tmp1 = (unsigned)Val | 0x10000;
1051 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1053 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1055 return getConstant(CountTrailingZeros_64(Val), VT);
1060 // Constant fold unary operations with an floating point constant operand.
1061 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1064 return getConstantFP(-C->getValue(), VT);
1066 return getConstantFP(fabs(C->getValue()), VT);
1068 case ISD::FP_EXTEND:
1069 return getConstantFP(C->getValue(), VT);
1070 case ISD::FP_TO_SINT:
1071 return getConstant((int64_t)C->getValue(), VT);
1072 case ISD::FP_TO_UINT:
1073 return getConstant((uint64_t)C->getValue(), VT);
1074 case ISD::BIT_CONVERT:
1075 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1076 return getConstant(FloatToBits(C->getValue()), VT);
1077 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1078 return getConstant(DoubleToBits(C->getValue()), VT);
1082 unsigned OpOpcode = Operand.Val->getOpcode();
1084 case ISD::TokenFactor:
1085 return Operand; // Factor of one node? No factor.
1086 case ISD::SIGN_EXTEND:
1087 if (Operand.getValueType() == VT) return Operand; // noop extension
1088 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
1089 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1090 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1092 case ISD::ZERO_EXTEND:
1093 if (Operand.getValueType() == VT) return Operand; // noop extension
1094 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
1095 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1096 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1098 case ISD::ANY_EXTEND:
1099 if (Operand.getValueType() == VT) return Operand; // noop extension
1100 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
1101 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1102 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1103 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1106 if (Operand.getValueType() == VT) return Operand; // noop truncate
1107 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
1108 if (OpOpcode == ISD::TRUNCATE)
1109 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1110 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1111 OpOpcode == ISD::ANY_EXTEND) {
1112 // If the source is smaller than the dest, we still need an extend.
1113 if (Operand.Val->getOperand(0).getValueType() < VT)
1114 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1115 else if (Operand.Val->getOperand(0).getValueType() > VT)
1116 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1118 return Operand.Val->getOperand(0);
1121 case ISD::BIT_CONVERT:
1122 // Basic sanity checking.
1123 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
1124 && "Cannot BIT_CONVERT between two different types!");
1125 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1126 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1127 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1128 if (OpOpcode == ISD::UNDEF)
1129 return getNode(ISD::UNDEF, VT);
1131 case ISD::SCALAR_TO_VECTOR:
1132 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1133 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1134 "Illegal SCALAR_TO_VECTOR node!");
1137 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1138 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1139 Operand.Val->getOperand(0));
1140 if (OpOpcode == ISD::FNEG) // --X -> X
1141 return Operand.Val->getOperand(0);
1144 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1145 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1150 MVT::ValueType *VTs = getNodeValueTypes(VT);
1151 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1152 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Operand);
1154 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1155 return SDOperand(E, 0);
1156 N = new SDNode(Opcode, Operand);
1157 N->setValueTypes(VTs, 1);
1158 CSEMap.InsertNode(N, IP);
1160 N = new SDNode(Opcode, Operand);
1161 N->setValueTypes(VTs, 1);
1163 AllNodes.push_back(N);
1164 return SDOperand(N, 0);
1169 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1170 SDOperand N1, SDOperand N2) {
1173 case ISD::TokenFactor:
1174 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1175 N2.getValueType() == MVT::Other && "Invalid token factor!");
1184 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1191 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1198 assert(N1.getValueType() == N2.getValueType() &&
1199 N1.getValueType() == VT && "Binary operator types must match!");
1201 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1202 assert(N1.getValueType() == VT &&
1203 MVT::isFloatingPoint(N1.getValueType()) &&
1204 MVT::isFloatingPoint(N2.getValueType()) &&
1205 "Invalid FCOPYSIGN!");
1212 assert(VT == N1.getValueType() &&
1213 "Shift operators return type must be the same as their first arg");
1214 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1215 VT != MVT::i1 && "Shifts only work on integers");
1217 case ISD::FP_ROUND_INREG: {
1218 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1219 assert(VT == N1.getValueType() && "Not an inreg round!");
1220 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1221 "Cannot FP_ROUND_INREG integer types");
1222 assert(EVT <= VT && "Not rounding down!");
1225 case ISD::AssertSext:
1226 case ISD::AssertZext:
1227 case ISD::SIGN_EXTEND_INREG: {
1228 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1229 assert(VT == N1.getValueType() && "Not an inreg extend!");
1230 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1231 "Cannot *_EXTEND_INREG FP types");
1232 assert(EVT <= VT && "Not extending!");
1239 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1240 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1242 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1243 int64_t Val = N1C->getValue();
1244 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1245 Val <<= 64-FromBits;
1246 Val >>= 64-FromBits;
1247 return getConstant(Val, VT);
1251 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1253 case ISD::ADD: return getConstant(C1 + C2, VT);
1254 case ISD::SUB: return getConstant(C1 - C2, VT);
1255 case ISD::MUL: return getConstant(C1 * C2, VT);
1257 if (C2) return getConstant(C1 / C2, VT);
1260 if (C2) return getConstant(C1 % C2, VT);
1263 if (C2) return getConstant(N1C->getSignExtended() /
1264 N2C->getSignExtended(), VT);
1267 if (C2) return getConstant(N1C->getSignExtended() %
1268 N2C->getSignExtended(), VT);
1270 case ISD::AND : return getConstant(C1 & C2, VT);
1271 case ISD::OR : return getConstant(C1 | C2, VT);
1272 case ISD::XOR : return getConstant(C1 ^ C2, VT);
1273 case ISD::SHL : return getConstant(C1 << C2, VT);
1274 case ISD::SRL : return getConstant(C1 >> C2, VT);
1275 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
1277 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1280 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1284 } else { // Cannonicalize constant to RHS if commutative
1285 if (isCommutativeBinOp(Opcode)) {
1286 std::swap(N1C, N2C);
1292 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1293 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1296 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1298 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1299 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1300 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1302 if (C2) return getConstantFP(C1 / C2, VT);
1305 if (C2) return getConstantFP(fmod(C1, C2), VT);
1307 case ISD::FCOPYSIGN: {
1318 if (u2.I < 0) // Sign bit of RHS set?
1319 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1321 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1322 return getConstantFP(u1.F, VT);
1326 } else { // Cannonicalize constant to RHS if commutative
1327 if (isCommutativeBinOp(Opcode)) {
1328 std::swap(N1CFP, N2CFP);
1334 // Canonicalize an UNDEF to the RHS, even over a constant.
1335 if (N1.getOpcode() == ISD::UNDEF) {
1336 if (isCommutativeBinOp(Opcode)) {
1340 case ISD::FP_ROUND_INREG:
1341 case ISD::SIGN_EXTEND_INREG:
1347 return N1; // fold op(undef, arg2) -> undef
1354 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1359 // Fold a bunch of operators when the RHS is undef.
1360 if (N2.getOpcode() == ISD::UNDEF) {
1374 return N2; // fold op(arg1, undef) -> undef
1379 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1381 return getConstant(MVT::getIntVTBitMask(VT), VT);
1387 // Finally, fold operations that do not require constants.
1389 case ISD::FP_ROUND_INREG:
1390 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1392 case ISD::SIGN_EXTEND_INREG: {
1393 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1394 if (EVT == VT) return N1; // Not actually extending
1398 // FIXME: figure out how to safely handle things like
1399 // int foo(int x) { return 1 << (x & 255); }
1400 // int bar() { return foo(256); }
1405 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1406 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
1407 return getNode(Opcode, VT, N1, N2.getOperand(0));
1408 else if (N2.getOpcode() == ISD::AND)
1409 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1410 // If the and is only masking out bits that cannot effect the shift,
1411 // eliminate the and.
1412 unsigned NumBits = MVT::getSizeInBits(VT);
1413 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1414 return getNode(Opcode, VT, N1, N2.getOperand(0));
1420 // Memoize this node if possible.
1422 MVT::ValueType *VTs = getNodeValueTypes(VT);
1423 if (VT != MVT::Flag) {
1424 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2);
1426 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1427 return SDOperand(E, 0);
1428 N = new SDNode(Opcode, N1, N2);
1429 N->setValueTypes(VTs, 1);
1430 CSEMap.InsertNode(N, IP);
1432 N = new SDNode(Opcode, N1, N2);
1433 N->setValueTypes(VTs, 1);
1436 AllNodes.push_back(N);
1437 return SDOperand(N, 0);
1440 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1441 SDOperand N1, SDOperand N2, SDOperand N3) {
1442 // Perform various simplifications.
1443 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1444 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1445 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1448 // Use SimplifySetCC to simplify SETCC's.
1449 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
1450 if (Simp.Val) return Simp;
1455 if (N1C->getValue())
1456 return N2; // select true, X, Y -> X
1458 return N3; // select false, X, Y -> Y
1460 if (N2 == N3) return N2; // select C, X, X -> X
1464 if (N2C->getValue()) // Unconditional branch
1465 return getNode(ISD::BR, MVT::Other, N1, N3);
1467 return N1; // Never-taken branch
1469 case ISD::VECTOR_SHUFFLE:
1470 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1471 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1472 N3.getOpcode() == ISD::BUILD_VECTOR &&
1473 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1474 "Illegal VECTOR_SHUFFLE node!");
1478 // Memoize node if it doesn't produce a flag.
1480 MVT::ValueType *VTs = getNodeValueTypes(VT);
1482 if (VT != MVT::Flag) {
1483 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2, N3);
1485 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1486 return SDOperand(E, 0);
1487 N = new SDNode(Opcode, N1, N2, N3);
1488 N->setValueTypes(VTs, 1);
1489 CSEMap.InsertNode(N, IP);
1491 N = new SDNode(Opcode, N1, N2, N3);
1492 N->setValueTypes(VTs, 1);
1494 AllNodes.push_back(N);
1495 return SDOperand(N, 0);
1498 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1499 SDOperand N1, SDOperand N2, SDOperand N3,
1501 std::vector<SDOperand> Ops;
1507 return getNode(Opcode, VT, Ops);
1510 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1511 SDOperand N1, SDOperand N2, SDOperand N3,
1512 SDOperand N4, SDOperand N5) {
1513 std::vector<SDOperand> Ops;
1520 return getNode(Opcode, VT, Ops);
1523 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1524 SDOperand Chain, SDOperand Ptr,
1526 MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other);
1528 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, SV);
1530 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1531 return SDOperand(E, 0);
1532 SDNode *N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1533 N->setValueTypes(VTs, 2);
1534 CSEMap.InsertNode(N, IP);
1535 AllNodes.push_back(N);
1536 return SDOperand(N, 0);
1539 SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1540 SDOperand Chain, SDOperand Ptr,
1542 std::vector<SDOperand> Ops;
1544 Ops.push_back(Chain);
1547 Ops.push_back(getConstant(Count, MVT::i32));
1548 Ops.push_back(getValueType(EVT));
1549 std::vector<MVT::ValueType> VTs;
1551 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1552 return getNode(ISD::VLOAD, VTs, Ops);
1555 SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1556 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1557 MVT::ValueType EVT) {
1558 std::vector<SDOperand> Ops;
1560 Ops.push_back(Chain);
1563 Ops.push_back(getValueType(EVT));
1564 std::vector<MVT::ValueType> VTs;
1566 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1567 return getNode(Opcode, VTs, Ops);
1570 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
1571 assert((!V || isa<PointerType>(V->getType())) &&
1572 "SrcValue is not a pointer?");
1573 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1574 if (N) return SDOperand(N, 0);
1576 N = new SrcValueSDNode(V, Offset);
1577 AllNodes.push_back(N);
1578 return SDOperand(N, 0);
1581 SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1582 SDOperand Chain, SDOperand Ptr,
1584 std::vector<SDOperand> Ops;
1586 Ops.push_back(Chain);
1589 std::vector<MVT::ValueType> VTs;
1591 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1592 return getNode(ISD::VAARG, VTs, Ops);
1595 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1596 std::vector<SDOperand> &Ops) {
1597 switch (Ops.size()) {
1598 case 0: return getNode(Opcode, VT);
1599 case 1: return getNode(Opcode, VT, Ops[0]);
1600 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1601 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
1607 case ISD::TRUNCSTORE: {
1608 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1609 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1610 #if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1611 // If this is a truncating store of a constant, convert to the desired type
1612 // and store it instead.
1613 if (isa<Constant>(Ops[0])) {
1614 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1615 if (isa<Constant>(Op))
1618 // Also for ConstantFP?
1620 if (Ops[0].getValueType() == EVT) // Normal store?
1621 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1622 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1623 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1624 "Can't do FP-INT conversion!");
1627 case ISD::SELECT_CC: {
1628 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
1629 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1630 "LHS and RHS of condition must have same type!");
1631 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1632 "True and False arms of SelectCC must have same type!");
1633 assert(Ops[2].getValueType() == VT &&
1634 "select_cc node must be of same type as true and false value!");
1638 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
1639 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1640 "LHS/RHS of comparison should match types!");
1647 MVT::ValueType *VTs = getNodeValueTypes(VT);
1648 if (VT != MVT::Flag) {
1649 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, &Ops[0], Ops.size());
1651 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1652 return SDOperand(E, 0);
1653 N = new SDNode(Opcode, Ops);
1654 N->setValueTypes(VTs, 1);
1655 CSEMap.InsertNode(N, IP);
1657 N = new SDNode(Opcode, Ops);
1658 N->setValueTypes(VTs, 1);
1660 AllNodes.push_back(N);
1661 return SDOperand(N, 0);
1664 SDOperand SelectionDAG::getNode(unsigned Opcode,
1665 std::vector<MVT::ValueType> &ResultTys,
1666 std::vector<SDOperand> &Ops) {
1667 if (ResultTys.size() == 1)
1668 return getNode(Opcode, ResultTys[0], Ops);
1673 case ISD::ZEXTLOAD: {
1674 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1675 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1676 // If they are asking for an extending load from/to the same thing, return a
1678 if (ResultTys[0] == EVT)
1679 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1680 if (MVT::isVector(ResultTys[0])) {
1681 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1682 "Invalid vector extload!");
1684 assert(EVT < ResultTys[0] &&
1685 "Should only be an extending load, not truncating!");
1687 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1688 "Cannot sign/zero extend a FP/Vector load!");
1689 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1690 "Cannot convert from FP to Int or Int -> FP!");
1694 // FIXME: figure out how to safely handle things like
1695 // int foo(int x) { return 1 << (x & 255); }
1696 // int bar() { return foo(256); }
1698 case ISD::SRA_PARTS:
1699 case ISD::SRL_PARTS:
1700 case ISD::SHL_PARTS:
1701 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1702 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
1703 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1704 else if (N3.getOpcode() == ISD::AND)
1705 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1706 // If the and is only masking out bits that cannot effect the shift,
1707 // eliminate the and.
1708 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1709 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1710 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1716 // Memoize the node unless it returns a flag.
1718 MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
1719 if (ResultTys.back() != MVT::Flag) {
1720 SelectionDAGCSEMap::NodeID ID;
1721 ID.SetOpcode(Opcode);
1722 ID.SetValueTypes(VTs);
1723 ID.SetOperands(&Ops[0], Ops.size());
1725 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1726 return SDOperand(E, 0);
1727 N = new SDNode(Opcode, Ops);
1728 N->setValueTypes(VTs, ResultTys.size());
1729 CSEMap.InsertNode(N, IP);
1731 N = new SDNode(Opcode, Ops);
1732 N->setValueTypes(VTs, ResultTys.size());
1734 AllNodes.push_back(N);
1735 return SDOperand(N, 0);
1738 MVT::ValueType *SelectionDAG::getNodeValueTypes(MVT::ValueType VT) {
1739 return SDNode::getValueTypeList(VT);
1742 MVT::ValueType *SelectionDAG::getNodeValueTypes(
1743 std::vector<MVT::ValueType> &RetVals) {
1744 switch (RetVals.size()) {
1745 case 0: assert(0 && "Cannot have nodes without results!");
1746 case 1: return SDNode::getValueTypeList(RetVals[0]);
1747 case 2: return getNodeValueTypes(RetVals[0], RetVals[1]);
1751 std::list<std::vector<MVT::ValueType> >::iterator I =
1752 std::find(VTList.begin(), VTList.end(), RetVals);
1753 if (I == VTList.end()) {
1754 VTList.push_front(RetVals);
1761 MVT::ValueType *SelectionDAG::getNodeValueTypes(MVT::ValueType VT1,
1762 MVT::ValueType VT2) {
1763 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1764 E = VTList.end(); I != E; ++I) {
1765 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
1768 std::vector<MVT::ValueType> V;
1771 VTList.push_front(V);
1772 return &(*VTList.begin())[0];
1775 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1776 /// specified operands. If the resultant node already exists in the DAG,
1777 /// this does not modify the specified node, instead it returns the node that
1778 /// already exists. If the resultant node does not exist in the DAG, the
1779 /// input node is returned. As a degenerate case, if you specify the same
1780 /// input operands as the node already has, the input node is returned.
1781 SDOperand SelectionDAG::
1782 UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1783 SDNode *N = InN.Val;
1784 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1786 // Check to see if there is no change.
1787 if (Op == N->getOperand(0)) return InN;
1789 // See if the modified node already exists.
1790 void *InsertPos = 0;
1791 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1792 return SDOperand(Existing, InN.ResNo);
1794 // Nope it doesn't. Remove the node from it's current place in the maps.
1796 RemoveNodeFromCSEMaps(N);
1798 // Now we update the operands.
1799 N->OperandList[0].Val->removeUser(N);
1801 N->OperandList[0] = Op;
1803 // If this gets put into a CSE map, add it.
1804 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1808 SDOperand SelectionDAG::
1809 UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1810 SDNode *N = InN.Val;
1811 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1813 // Check to see if there is no change.
1814 bool AnyChange = false;
1815 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1816 return InN; // No operands changed, just return the input node.
1818 // See if the modified node already exists.
1819 void *InsertPos = 0;
1820 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1821 return SDOperand(Existing, InN.ResNo);
1823 // Nope it doesn't. Remove the node from it's current place in the maps.
1825 RemoveNodeFromCSEMaps(N);
1827 // Now we update the operands.
1828 if (N->OperandList[0] != Op1) {
1829 N->OperandList[0].Val->removeUser(N);
1830 Op1.Val->addUser(N);
1831 N->OperandList[0] = Op1;
1833 if (N->OperandList[1] != Op2) {
1834 N->OperandList[1].Val->removeUser(N);
1835 Op2.Val->addUser(N);
1836 N->OperandList[1] = Op2;
1839 // If this gets put into a CSE map, add it.
1840 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1844 SDOperand SelectionDAG::
1845 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1846 std::vector<SDOperand> Ops;
1850 return UpdateNodeOperands(N, Ops);
1853 SDOperand SelectionDAG::
1854 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1855 SDOperand Op3, SDOperand Op4) {
1856 std::vector<SDOperand> Ops;
1861 return UpdateNodeOperands(N, Ops);
1864 SDOperand SelectionDAG::
1865 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1866 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1867 std::vector<SDOperand> Ops;
1873 return UpdateNodeOperands(N, Ops);
1877 SDOperand SelectionDAG::
1878 UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1879 SDNode *N = InN.Val;
1880 assert(N->getNumOperands() == Ops.size() &&
1881 "Update with wrong number of operands");
1883 // Check to see if there is no change.
1884 unsigned NumOps = Ops.size();
1885 bool AnyChange = false;
1886 for (unsigned i = 0; i != NumOps; ++i) {
1887 if (Ops[i] != N->getOperand(i)) {
1893 // No operands changed, just return the input node.
1894 if (!AnyChange) return InN;
1896 // See if the modified node already exists.
1897 void *InsertPos = 0;
1898 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
1899 return SDOperand(Existing, InN.ResNo);
1901 // Nope it doesn't. Remove the node from it's current place in the maps.
1903 RemoveNodeFromCSEMaps(N);
1905 // Now we update the operands.
1906 for (unsigned i = 0; i != NumOps; ++i) {
1907 if (N->OperandList[i] != Ops[i]) {
1908 N->OperandList[i].Val->removeUser(N);
1909 Ops[i].Val->addUser(N);
1910 N->OperandList[i] = Ops[i];
1914 // If this gets put into a CSE map, add it.
1915 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1922 /// SelectNodeTo - These are used for target selectors to *mutate* the
1923 /// specified node to have the specified return type, Target opcode, and
1924 /// operands. Note that target opcodes are stored as
1925 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1927 /// Note that SelectNodeTo returns the resultant node. If there is already a
1928 /// node of the specified opcode and operands, it returns that node instead of
1929 /// the current one.
1930 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1931 MVT::ValueType VT) {
1932 // If an identical node already exists, use it.
1933 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1934 if (ON) return SDOperand(ON, 0);
1936 RemoveNodeFromCSEMaps(N);
1938 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1939 N->setValueTypes(getNodeValueTypes(VT), 1);
1941 ON = N; // Memoize the new node.
1942 return SDOperand(N, 0);
1945 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1946 MVT::ValueType VT, SDOperand Op1) {
1947 // If an identical node already exists, use it.
1948 MVT::ValueType *VTs = getNodeValueTypes(VT);
1949 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
1951 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1952 return SDOperand(ON, 0);
1954 RemoveNodeFromCSEMaps(N);
1955 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1956 N->setValueTypes(getNodeValueTypes(VT), 1);
1957 N->setOperands(Op1);
1958 CSEMap.InsertNode(N, IP);
1959 return SDOperand(N, 0);
1962 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1963 MVT::ValueType VT, SDOperand Op1,
1965 // If an identical node already exists, use it.
1966 MVT::ValueType *VTs = getNodeValueTypes(VT);
1967 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1969 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1970 return SDOperand(ON, 0);
1972 RemoveNodeFromCSEMaps(N);
1973 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1974 N->setValueTypes(VTs, 1);
1975 N->setOperands(Op1, Op2);
1977 CSEMap.InsertNode(N, IP); // Memoize the new node.
1978 return SDOperand(N, 0);
1981 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1982 MVT::ValueType VT, SDOperand Op1,
1983 SDOperand Op2, SDOperand Op3) {
1984 // If an identical node already exists, use it.
1985 MVT::ValueType *VTs = getNodeValueTypes(VT);
1986 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2, Op3);
1988 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1989 return SDOperand(ON, 0);
1991 RemoveNodeFromCSEMaps(N);
1992 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1993 N->setValueTypes(VTs, 1);
1994 N->setOperands(Op1, Op2, Op3);
1996 CSEMap.InsertNode(N, IP); // Memoize the new node.
1997 return SDOperand(N, 0);
2000 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2001 MVT::ValueType VT, SDOperand Op1,
2002 SDOperand Op2, SDOperand Op3,
2004 // If an identical node already exists, use it.
2005 MVT::ValueType *VTs = getNodeValueTypes(VT);
2006 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2012 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2013 return SDOperand(ON, 0);
2015 RemoveNodeFromCSEMaps(N);
2016 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2017 N->setValueTypes(VTs, 1);
2018 N->setOperands(Op1, Op2, Op3, Op4);
2020 CSEMap.InsertNode(N, IP); // Memoize the new node.
2021 return SDOperand(N, 0);
2024 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2025 MVT::ValueType VT, SDOperand Op1,
2026 SDOperand Op2, SDOperand Op3,
2027 SDOperand Op4, SDOperand Op5) {
2028 MVT::ValueType *VTs = getNodeValueTypes(VT);
2029 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2036 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2037 return SDOperand(ON, 0);
2039 RemoveNodeFromCSEMaps(N);
2040 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2041 N->setValueTypes(VTs, 1);
2042 N->setOperands(Op1, Op2, Op3, Op4, Op5);
2044 CSEMap.InsertNode(N, IP); // Memoize the new node.
2045 return SDOperand(N, 0);
2048 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2049 MVT::ValueType VT, SDOperand Op1,
2050 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2051 SDOperand Op5, SDOperand Op6) {
2052 MVT::ValueType *VTs = getNodeValueTypes(VT);
2053 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2061 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2062 return SDOperand(ON, 0);
2064 RemoveNodeFromCSEMaps(N);
2065 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2066 N->setValueTypes(VTs, 1);
2067 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
2069 CSEMap.InsertNode(N, IP); // Memoize the new node.
2070 return SDOperand(N, 0);
2073 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2074 MVT::ValueType VT, SDOperand Op1,
2075 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2076 SDOperand Op5, SDOperand Op6,
2078 MVT::ValueType *VTs = getNodeValueTypes(VT);
2079 // If an identical node already exists, use it.
2080 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2089 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2090 return SDOperand(ON, 0);
2092 RemoveNodeFromCSEMaps(N);
2093 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2094 N->setValueTypes(VTs, 1);
2095 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
2097 CSEMap.InsertNode(N, IP); // Memoize the new node.
2098 return SDOperand(N, 0);
2100 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2101 MVT::ValueType VT, SDOperand Op1,
2102 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2103 SDOperand Op5, SDOperand Op6,
2104 SDOperand Op7, SDOperand Op8) {
2105 // If an identical node already exists, use it.
2106 MVT::ValueType *VTs = getNodeValueTypes(VT);
2107 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2117 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2118 return SDOperand(ON, 0);
2120 RemoveNodeFromCSEMaps(N);
2121 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2122 N->setValueTypes(VTs, 1);
2123 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2125 CSEMap.InsertNode(N, IP); // Memoize the new node.
2126 return SDOperand(N, 0);
2129 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2130 MVT::ValueType VT1, MVT::ValueType VT2,
2131 SDOperand Op1, SDOperand Op2) {
2132 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2133 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
2135 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2136 return SDOperand(ON, 0);
2138 RemoveNodeFromCSEMaps(N);
2139 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2140 N->setValueTypes(VTs, 2);
2141 N->setOperands(Op1, Op2);
2143 CSEMap.InsertNode(N, IP); // Memoize the new node.
2144 return SDOperand(N, 0);
2147 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2148 MVT::ValueType VT1, MVT::ValueType VT2,
2149 SDOperand Op1, SDOperand Op2,
2151 // If an identical node already exists, use it.
2152 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2153 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
2156 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2157 return SDOperand(ON, 0);
2159 RemoveNodeFromCSEMaps(N);
2160 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2161 N->setValueTypes(VTs, 2);
2162 N->setOperands(Op1, Op2, Op3);
2164 CSEMap.InsertNode(N, IP); // Memoize the new node.
2165 return SDOperand(N, 0);
2168 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2169 MVT::ValueType VT1, MVT::ValueType VT2,
2170 SDOperand Op1, SDOperand Op2,
2171 SDOperand Op3, SDOperand Op4) {
2172 // If an identical node already exists, use it.
2173 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2174 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2180 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2181 return SDOperand(ON, 0);
2183 RemoveNodeFromCSEMaps(N);
2184 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2185 N->setValueTypes(VTs, 2);
2186 N->setOperands(Op1, Op2, Op3, Op4);
2188 CSEMap.InsertNode(N, IP); // Memoize the new node.
2189 return SDOperand(N, 0);
2192 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2193 MVT::ValueType VT1, MVT::ValueType VT2,
2194 SDOperand Op1, SDOperand Op2,
2195 SDOperand Op3, SDOperand Op4,
2197 // If an identical node already exists, use it.
2198 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2199 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2206 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2207 return SDOperand(ON, 0);
2209 RemoveNodeFromCSEMaps(N);
2210 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2211 N->setValueTypes(VTs, 2);
2212 N->setOperands(Op1, Op2, Op3, Op4, Op5);
2214 CSEMap.InsertNode(N, IP); // Memoize the new node.
2215 return SDOperand(N, 0);
2218 /// getTargetNode - These are used for target selectors to create a new node
2219 /// with specified return type(s), target opcode, and operands.
2221 /// Note that getTargetNode returns the resultant node. If there is already a
2222 /// node of the specified opcode and operands, it returns that node instead of
2223 /// the current one.
2224 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2225 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2227 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2229 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2231 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2232 SDOperand Op1, SDOperand Op2) {
2233 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2235 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2236 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2237 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2239 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2240 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2242 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2244 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2245 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2246 SDOperand Op4, SDOperand Op5) {
2247 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2249 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2250 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2251 SDOperand Op4, SDOperand Op5,
2253 std::vector<SDOperand> Ops;
2261 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2263 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2264 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2265 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2267 std::vector<SDOperand> Ops;
2276 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2278 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2279 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2280 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2281 SDOperand Op7, SDOperand Op8) {
2282 std::vector<SDOperand> Ops;
2292 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2294 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2295 std::vector<SDOperand> &Ops) {
2296 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2298 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2299 MVT::ValueType VT2, SDOperand Op1) {
2300 std::vector<MVT::ValueType> ResultTys;
2301 ResultTys.push_back(VT1);
2302 ResultTys.push_back(VT2);
2303 std::vector<SDOperand> Ops;
2305 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2307 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2308 MVT::ValueType VT2, SDOperand Op1,
2310 std::vector<MVT::ValueType> ResultTys;
2311 ResultTys.push_back(VT1);
2312 ResultTys.push_back(VT2);
2313 std::vector<SDOperand> Ops;
2316 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2318 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2319 MVT::ValueType VT2, SDOperand Op1,
2320 SDOperand Op2, SDOperand Op3) {
2321 std::vector<MVT::ValueType> ResultTys;
2322 ResultTys.push_back(VT1);
2323 ResultTys.push_back(VT2);
2324 std::vector<SDOperand> Ops;
2328 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2330 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2331 MVT::ValueType VT2, SDOperand Op1,
2332 SDOperand Op2, SDOperand Op3,
2334 std::vector<MVT::ValueType> ResultTys;
2335 ResultTys.push_back(VT1);
2336 ResultTys.push_back(VT2);
2337 std::vector<SDOperand> Ops;
2342 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2344 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2345 MVT::ValueType VT2, SDOperand Op1,
2346 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2348 std::vector<MVT::ValueType> ResultTys;
2349 ResultTys.push_back(VT1);
2350 ResultTys.push_back(VT2);
2351 std::vector<SDOperand> Ops;
2357 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2359 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2360 MVT::ValueType VT2, SDOperand Op1,
2361 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2362 SDOperand Op5, SDOperand Op6) {
2363 std::vector<MVT::ValueType> ResultTys;
2364 ResultTys.push_back(VT1);
2365 ResultTys.push_back(VT2);
2366 std::vector<SDOperand> Ops;
2373 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2375 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2376 MVT::ValueType VT2, SDOperand Op1,
2377 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2378 SDOperand Op5, SDOperand Op6,
2380 std::vector<MVT::ValueType> ResultTys;
2381 ResultTys.push_back(VT1);
2382 ResultTys.push_back(VT2);
2383 std::vector<SDOperand> Ops;
2391 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2393 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2394 MVT::ValueType VT2, MVT::ValueType VT3,
2395 SDOperand Op1, SDOperand Op2) {
2396 std::vector<MVT::ValueType> ResultTys;
2397 ResultTys.push_back(VT1);
2398 ResultTys.push_back(VT2);
2399 ResultTys.push_back(VT3);
2400 std::vector<SDOperand> Ops;
2403 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2405 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2406 MVT::ValueType VT2, MVT::ValueType VT3,
2407 SDOperand Op1, SDOperand Op2,
2408 SDOperand Op3, SDOperand Op4,
2410 std::vector<MVT::ValueType> ResultTys;
2411 ResultTys.push_back(VT1);
2412 ResultTys.push_back(VT2);
2413 ResultTys.push_back(VT3);
2414 std::vector<SDOperand> Ops;
2420 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2422 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2423 MVT::ValueType VT2, MVT::ValueType VT3,
2424 SDOperand Op1, SDOperand Op2,
2425 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2427 std::vector<MVT::ValueType> ResultTys;
2428 ResultTys.push_back(VT1);
2429 ResultTys.push_back(VT2);
2430 ResultTys.push_back(VT3);
2431 std::vector<SDOperand> Ops;
2438 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2440 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2441 MVT::ValueType VT2, MVT::ValueType VT3,
2442 SDOperand Op1, SDOperand Op2,
2443 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2444 SDOperand Op6, SDOperand Op7) {
2445 std::vector<MVT::ValueType> ResultTys;
2446 ResultTys.push_back(VT1);
2447 ResultTys.push_back(VT2);
2448 ResultTys.push_back(VT3);
2449 std::vector<SDOperand> Ops;
2457 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2459 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2461 std::vector<SDOperand> &Ops) {
2462 std::vector<MVT::ValueType> ResultTys;
2463 ResultTys.push_back(VT1);
2464 ResultTys.push_back(VT2);
2465 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2468 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2469 /// This can cause recursive merging of nodes in the DAG.
2471 /// This version assumes From/To have a single result value.
2473 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2474 std::vector<SDNode*> *Deleted) {
2475 SDNode *From = FromN.Val, *To = ToN.Val;
2476 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2477 "Cannot replace with this method!");
2478 assert(From != To && "Cannot replace uses of with self");
2480 while (!From->use_empty()) {
2481 // Process users until they are all gone.
2482 SDNode *U = *From->use_begin();
2484 // This node is about to morph, remove its old self from the CSE maps.
2485 RemoveNodeFromCSEMaps(U);
2487 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2489 if (I->Val == From) {
2490 From->removeUser(U);
2495 // Now that we have modified U, add it back to the CSE maps. If it already
2496 // exists there, recursively merge the results together.
2497 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2498 ReplaceAllUsesWith(U, Existing, Deleted);
2500 if (Deleted) Deleted->push_back(U);
2501 DeleteNodeNotInCSEMaps(U);
2506 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2507 /// This can cause recursive merging of nodes in the DAG.
2509 /// This version assumes From/To have matching types and numbers of result
2512 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2513 std::vector<SDNode*> *Deleted) {
2514 assert(From != To && "Cannot replace uses of with self");
2515 assert(From->getNumValues() == To->getNumValues() &&
2516 "Cannot use this version of ReplaceAllUsesWith!");
2517 if (From->getNumValues() == 1) { // If possible, use the faster version.
2518 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
2522 while (!From->use_empty()) {
2523 // Process users until they are all gone.
2524 SDNode *U = *From->use_begin();
2526 // This node is about to morph, remove its old self from the CSE maps.
2527 RemoveNodeFromCSEMaps(U);
2529 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2531 if (I->Val == From) {
2532 From->removeUser(U);
2537 // Now that we have modified U, add it back to the CSE maps. If it already
2538 // exists there, recursively merge the results together.
2539 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2540 ReplaceAllUsesWith(U, Existing, Deleted);
2542 if (Deleted) Deleted->push_back(U);
2543 DeleteNodeNotInCSEMaps(U);
2548 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2549 /// This can cause recursive merging of nodes in the DAG.
2551 /// This version can replace From with any result values. To must match the
2552 /// number and types of values returned by From.
2553 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
2554 const std::vector<SDOperand> &To,
2555 std::vector<SDNode*> *Deleted) {
2556 assert(From->getNumValues() == To.size() &&
2557 "Incorrect number of values to replace with!");
2558 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
2559 // Degenerate case handled above.
2560 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
2564 while (!From->use_empty()) {
2565 // Process users until they are all gone.
2566 SDNode *U = *From->use_begin();
2568 // This node is about to morph, remove its old self from the CSE maps.
2569 RemoveNodeFromCSEMaps(U);
2571 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2573 if (I->Val == From) {
2574 const SDOperand &ToOp = To[I->ResNo];
2575 From->removeUser(U);
2577 ToOp.Val->addUser(U);
2580 // Now that we have modified U, add it back to the CSE maps. If it already
2581 // exists there, recursively merge the results together.
2582 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2583 ReplaceAllUsesWith(U, Existing, Deleted);
2585 if (Deleted) Deleted->push_back(U);
2586 DeleteNodeNotInCSEMaps(U);
2591 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2592 /// uses of other values produced by From.Val alone. The Deleted vector is
2593 /// handled the same was as for ReplaceAllUsesWith.
2594 void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2595 std::vector<SDNode*> &Deleted) {
2596 assert(From != To && "Cannot replace a value with itself");
2597 // Handle the simple, trivial, case efficiently.
2598 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2599 ReplaceAllUsesWith(From, To, &Deleted);
2603 // Get all of the users in a nice, deterministically ordered, uniqued set.
2604 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2606 while (!Users.empty()) {
2607 // We know that this user uses some value of From. If it is the right
2608 // value, update it.
2609 SDNode *User = Users.back();
2612 for (SDOperand *Op = User->OperandList,
2613 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2615 // Okay, we know this user needs to be updated. Remove its old self
2616 // from the CSE maps.
2617 RemoveNodeFromCSEMaps(User);
2619 // Update all operands that match "From".
2620 for (; Op != E; ++Op) {
2622 From.Val->removeUser(User);
2624 To.Val->addUser(User);
2628 // Now that we have modified User, add it back to the CSE maps. If it
2629 // already exists there, recursively merge the results together.
2630 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2631 unsigned NumDeleted = Deleted.size();
2632 ReplaceAllUsesWith(User, Existing, &Deleted);
2634 // User is now dead.
2635 Deleted.push_back(User);
2636 DeleteNodeNotInCSEMaps(User);
2638 // We have to be careful here, because ReplaceAllUsesWith could have
2639 // deleted a user of From, which means there may be dangling pointers
2640 // in the "Users" setvector. Scan over the deleted node pointers and
2641 // remove them from the setvector.
2642 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2643 Users.remove(Deleted[i]);
2645 break; // Exit the operand scanning loop.
2652 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2653 /// their allnodes order. It returns the maximum id.
2654 unsigned SelectionDAG::AssignNodeIds() {
2656 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2663 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
2664 /// based on their topological order. It returns the maximum id and a vector
2665 /// of the SDNodes* in assigned order by reference.
2666 unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
2667 unsigned DAGSize = AllNodes.size();
2668 std::vector<unsigned> InDegree(DAGSize);
2669 std::vector<SDNode*> Sources;
2671 // Use a two pass approach to avoid using a std::map which is slow.
2673 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2676 unsigned Degree = N->use_size();
2677 InDegree[N->getNodeId()] = Degree;
2679 Sources.push_back(N);
2683 while (!Sources.empty()) {
2684 SDNode *N = Sources.back();
2686 TopOrder.push_back(N);
2687 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2689 unsigned Degree = --InDegree[P->getNodeId()];
2691 Sources.push_back(P);
2695 // Second pass, assign the actual topological order as node ids.
2697 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2699 (*TI)->setNodeId(Id++);
2706 //===----------------------------------------------------------------------===//
2708 //===----------------------------------------------------------------------===//
2710 // Out-of-line virtual method to give class a home.
2711 void SDNode::ANCHOR() {
2714 /// getValueTypeList - Return a pointer to the specified value type.
2716 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2717 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2722 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2723 /// indicated value. This method ignores uses of other values defined by this
2725 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
2726 assert(Value < getNumValues() && "Bad value!");
2728 // If there is only one value, this is easy.
2729 if (getNumValues() == 1)
2730 return use_size() == NUses;
2731 if (Uses.size() < NUses) return false;
2733 SDOperand TheValue(const_cast<SDNode *>(this), Value);
2735 std::set<SDNode*> UsersHandled;
2737 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
2740 if (User->getNumOperands() == 1 ||
2741 UsersHandled.insert(User).second) // First time we've seen this?
2742 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2743 if (User->getOperand(i) == TheValue) {
2745 return false; // too many uses
2750 // Found exactly the right number of uses?
2755 // isOnlyUse - Return true if this node is the only use of N.
2756 bool SDNode::isOnlyUse(SDNode *N) const {
2758 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2769 // isOperand - Return true if this node is an operand of N.
2770 bool SDOperand::isOperand(SDNode *N) const {
2771 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2772 if (*this == N->getOperand(i))
2777 bool SDNode::isOperand(SDNode *N) const {
2778 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2779 if (this == N->OperandList[i].Val)
2784 const char *SDNode::getOperationName(const SelectionDAG *G) const {
2785 switch (getOpcode()) {
2787 if (getOpcode() < ISD::BUILTIN_OP_END)
2788 return "<<Unknown DAG Node>>";
2791 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
2792 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2793 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
2795 TargetLowering &TLI = G->getTargetLoweringInfo();
2797 TLI.getTargetNodeName(getOpcode());
2798 if (Name) return Name;
2801 return "<<Unknown Target Node>>";
2804 case ISD::PCMARKER: return "PCMarker";
2805 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
2806 case ISD::SRCVALUE: return "SrcValue";
2807 case ISD::EntryToken: return "EntryToken";
2808 case ISD::TokenFactor: return "TokenFactor";
2809 case ISD::AssertSext: return "AssertSext";
2810 case ISD::AssertZext: return "AssertZext";
2812 case ISD::STRING: return "String";
2813 case ISD::BasicBlock: return "BasicBlock";
2814 case ISD::VALUETYPE: return "ValueType";
2815 case ISD::Register: return "Register";
2817 case ISD::Constant: return "Constant";
2818 case ISD::ConstantFP: return "ConstantFP";
2819 case ISD::GlobalAddress: return "GlobalAddress";
2820 case ISD::FrameIndex: return "FrameIndex";
2821 case ISD::JumpTable: return "JumpTable";
2822 case ISD::ConstantPool: return "ConstantPool";
2823 case ISD::ExternalSymbol: return "ExternalSymbol";
2824 case ISD::INTRINSIC_WO_CHAIN: {
2825 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2826 return Intrinsic::getName((Intrinsic::ID)IID);
2828 case ISD::INTRINSIC_VOID:
2829 case ISD::INTRINSIC_W_CHAIN: {
2830 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
2831 return Intrinsic::getName((Intrinsic::ID)IID);
2834 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
2835 case ISD::TargetConstant: return "TargetConstant";
2836 case ISD::TargetConstantFP:return "TargetConstantFP";
2837 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2838 case ISD::TargetFrameIndex: return "TargetFrameIndex";
2839 case ISD::TargetJumpTable: return "TargetJumpTable";
2840 case ISD::TargetConstantPool: return "TargetConstantPool";
2841 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
2843 case ISD::CopyToReg: return "CopyToReg";
2844 case ISD::CopyFromReg: return "CopyFromReg";
2845 case ISD::UNDEF: return "undef";
2846 case ISD::MERGE_VALUES: return "mergevalues";
2847 case ISD::INLINEASM: return "inlineasm";
2848 case ISD::HANDLENODE: return "handlenode";
2849 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
2850 case ISD::CALL: return "call";
2853 case ISD::FABS: return "fabs";
2854 case ISD::FNEG: return "fneg";
2855 case ISD::FSQRT: return "fsqrt";
2856 case ISD::FSIN: return "fsin";
2857 case ISD::FCOS: return "fcos";
2860 case ISD::ADD: return "add";
2861 case ISD::SUB: return "sub";
2862 case ISD::MUL: return "mul";
2863 case ISD::MULHU: return "mulhu";
2864 case ISD::MULHS: return "mulhs";
2865 case ISD::SDIV: return "sdiv";
2866 case ISD::UDIV: return "udiv";
2867 case ISD::SREM: return "srem";
2868 case ISD::UREM: return "urem";
2869 case ISD::AND: return "and";
2870 case ISD::OR: return "or";
2871 case ISD::XOR: return "xor";
2872 case ISD::SHL: return "shl";
2873 case ISD::SRA: return "sra";
2874 case ISD::SRL: return "srl";
2875 case ISD::ROTL: return "rotl";
2876 case ISD::ROTR: return "rotr";
2877 case ISD::FADD: return "fadd";
2878 case ISD::FSUB: return "fsub";
2879 case ISD::FMUL: return "fmul";
2880 case ISD::FDIV: return "fdiv";
2881 case ISD::FREM: return "frem";
2882 case ISD::FCOPYSIGN: return "fcopysign";
2883 case ISD::VADD: return "vadd";
2884 case ISD::VSUB: return "vsub";
2885 case ISD::VMUL: return "vmul";
2886 case ISD::VSDIV: return "vsdiv";
2887 case ISD::VUDIV: return "vudiv";
2888 case ISD::VAND: return "vand";
2889 case ISD::VOR: return "vor";
2890 case ISD::VXOR: return "vxor";
2892 case ISD::SETCC: return "setcc";
2893 case ISD::SELECT: return "select";
2894 case ISD::SELECT_CC: return "select_cc";
2895 case ISD::VSELECT: return "vselect";
2896 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2897 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2898 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
2899 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
2900 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2901 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2902 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2903 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2904 case ISD::VBIT_CONVERT: return "vbit_convert";
2905 case ISD::ADDC: return "addc";
2906 case ISD::ADDE: return "adde";
2907 case ISD::SUBC: return "subc";
2908 case ISD::SUBE: return "sube";
2909 case ISD::SHL_PARTS: return "shl_parts";
2910 case ISD::SRA_PARTS: return "sra_parts";
2911 case ISD::SRL_PARTS: return "srl_parts";
2913 // Conversion operators.
2914 case ISD::SIGN_EXTEND: return "sign_extend";
2915 case ISD::ZERO_EXTEND: return "zero_extend";
2916 case ISD::ANY_EXTEND: return "any_extend";
2917 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
2918 case ISD::TRUNCATE: return "truncate";
2919 case ISD::FP_ROUND: return "fp_round";
2920 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
2921 case ISD::FP_EXTEND: return "fp_extend";
2923 case ISD::SINT_TO_FP: return "sint_to_fp";
2924 case ISD::UINT_TO_FP: return "uint_to_fp";
2925 case ISD::FP_TO_SINT: return "fp_to_sint";
2926 case ISD::FP_TO_UINT: return "fp_to_uint";
2927 case ISD::BIT_CONVERT: return "bit_convert";
2929 // Control flow instructions
2930 case ISD::BR: return "br";
2931 case ISD::BRIND: return "brind";
2932 case ISD::BRCOND: return "brcond";
2933 case ISD::BR_CC: return "br_cc";
2934 case ISD::RET: return "ret";
2935 case ISD::CALLSEQ_START: return "callseq_start";
2936 case ISD::CALLSEQ_END: return "callseq_end";
2939 case ISD::LOAD: return "load";
2940 case ISD::STORE: return "store";
2941 case ISD::VLOAD: return "vload";
2942 case ISD::EXTLOAD: return "extload";
2943 case ISD::SEXTLOAD: return "sextload";
2944 case ISD::ZEXTLOAD: return "zextload";
2945 case ISD::TRUNCSTORE: return "truncstore";
2946 case ISD::VAARG: return "vaarg";
2947 case ISD::VACOPY: return "vacopy";
2948 case ISD::VAEND: return "vaend";
2949 case ISD::VASTART: return "vastart";
2950 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2951 case ISD::EXTRACT_ELEMENT: return "extract_element";
2952 case ISD::BUILD_PAIR: return "build_pair";
2953 case ISD::STACKSAVE: return "stacksave";
2954 case ISD::STACKRESTORE: return "stackrestore";
2956 // Block memory operations.
2957 case ISD::MEMSET: return "memset";
2958 case ISD::MEMCPY: return "memcpy";
2959 case ISD::MEMMOVE: return "memmove";
2962 case ISD::BSWAP: return "bswap";
2963 case ISD::CTPOP: return "ctpop";
2964 case ISD::CTTZ: return "cttz";
2965 case ISD::CTLZ: return "ctlz";
2968 case ISD::LOCATION: return "location";
2969 case ISD::DEBUG_LOC: return "debug_loc";
2970 case ISD::DEBUG_LABEL: return "debug_label";
2973 switch (cast<CondCodeSDNode>(this)->get()) {
2974 default: assert(0 && "Unknown setcc condition!");
2975 case ISD::SETOEQ: return "setoeq";
2976 case ISD::SETOGT: return "setogt";
2977 case ISD::SETOGE: return "setoge";
2978 case ISD::SETOLT: return "setolt";
2979 case ISD::SETOLE: return "setole";
2980 case ISD::SETONE: return "setone";
2982 case ISD::SETO: return "seto";
2983 case ISD::SETUO: return "setuo";
2984 case ISD::SETUEQ: return "setue";
2985 case ISD::SETUGT: return "setugt";
2986 case ISD::SETUGE: return "setuge";
2987 case ISD::SETULT: return "setult";
2988 case ISD::SETULE: return "setule";
2989 case ISD::SETUNE: return "setune";
2991 case ISD::SETEQ: return "seteq";
2992 case ISD::SETGT: return "setgt";
2993 case ISD::SETGE: return "setge";
2994 case ISD::SETLT: return "setlt";
2995 case ISD::SETLE: return "setle";
2996 case ISD::SETNE: return "setne";
3001 void SDNode::dump() const { dump(0); }
3002 void SDNode::dump(const SelectionDAG *G) const {
3003 std::cerr << (void*)this << ": ";
3005 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
3006 if (i) std::cerr << ",";
3007 if (getValueType(i) == MVT::Other)
3010 std::cerr << MVT::getValueTypeString(getValueType(i));
3012 std::cerr << " = " << getOperationName(G);
3015 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
3016 if (i) std::cerr << ", ";
3017 std::cerr << (void*)getOperand(i).Val;
3018 if (unsigned RN = getOperand(i).ResNo)
3019 std::cerr << ":" << RN;
3022 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
3023 std::cerr << "<" << CSDN->getValue() << ">";
3024 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
3025 std::cerr << "<" << CSDN->getValue() << ">";
3026 } else if (const GlobalAddressSDNode *GADN =
3027 dyn_cast<GlobalAddressSDNode>(this)) {
3028 int offset = GADN->getOffset();
3030 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
3032 std::cerr << " + " << offset;
3034 std::cerr << " " << offset;
3035 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
3036 std::cerr << "<" << FIDN->getIndex() << ">";
3037 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
3038 int offset = CP->getOffset();
3039 std::cerr << "<" << *CP->get() << ">";
3041 std::cerr << " + " << offset;
3043 std::cerr << " " << offset;
3044 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
3046 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3048 std::cerr << LBB->getName() << " ";
3049 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
3050 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
3051 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
3052 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
3054 std::cerr << " #" << R->getReg();
3056 } else if (const ExternalSymbolSDNode *ES =
3057 dyn_cast<ExternalSymbolSDNode>(this)) {
3058 std::cerr << "'" << ES->getSymbol() << "'";
3059 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3061 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
3063 std::cerr << "<null:" << M->getOffset() << ">";
3064 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
3065 std::cerr << ":" << getValueTypeString(N->getVT());
3069 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
3070 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3071 if (N->getOperand(i).Val->hasOneUse())
3072 DumpNodes(N->getOperand(i).Val, indent+2, G);
3074 std::cerr << "\n" << std::string(indent+2, ' ')
3075 << (void*)N->getOperand(i).Val << ": <multiple use>";
3078 std::cerr << "\n" << std::string(indent, ' ');
3082 void SelectionDAG::dump() const {
3083 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
3084 std::vector<const SDNode*> Nodes;
3085 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3089 std::sort(Nodes.begin(), Nodes.end());
3091 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
3092 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
3093 DumpNodes(Nodes[i], 2, this);
3096 DumpNodes(getRoot().Val, 2, this);
3098 std::cerr << "\n\n";
3101 /// InsertISelMapEntry - A helper function to insert a key / element pair
3102 /// into a SDOperand to SDOperand map. This is added to avoid the map
3103 /// insertion operator from being inlined.
3104 void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
3105 SDNode *Key, unsigned KeyResNo,
3106 SDNode *Element, unsigned ElementResNo) {
3107 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
3108 SDOperand(Element, ElementResNo)));