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 = StringNodes.erase(cast<StringSDNode>(N)->getValue());
348 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
349 "Cond code doesn't exist!");
350 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
351 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
353 case ISD::ExternalSymbol:
354 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
356 case ISD::TargetExternalSymbol:
358 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
361 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
362 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
365 // Remove it from the CSE Map.
366 Erased = CSEMap.RemoveNode(N);
370 // Verify that the node was actually in one of the CSE maps, unless it has a
371 // flag result (which cannot be CSE'd) or is one of the special cases that are
372 // not subject to CSE.
373 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
374 !N->isTargetOpcode()) {
377 assert(0 && "Node is not in map!");
382 /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
383 /// has been taken out and modified in some way. If the specified node already
384 /// exists in the CSE maps, do not modify the maps, but return the existing node
385 /// instead. If it doesn't exist, add it and return null.
387 SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
388 assert(N->getNumOperands() && "This is a leaf node!");
389 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
390 return 0; // Never add these nodes.
392 // Check that remaining values produced are not flags.
393 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
394 if (N->getValueType(i) == MVT::Flag)
395 return 0; // Never CSE anything that produces a flag.
397 SDNode *New = CSEMap.GetOrInsertNode(N);
398 if (New != N) return New; // Node already existed.
402 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
403 /// were replaced with those specified. If this node is never memoized,
404 /// return null, otherwise return a pointer to the slot it would take. If a
405 /// node already exists with these operands, the slot will be non-null.
406 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
408 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
409 return 0; // Never add these nodes.
411 // Check that remaining values produced are not flags.
412 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
413 if (N->getValueType(i) == MVT::Flag)
414 return 0; // Never CSE anything that produces a flag.
416 SelectionDAGCSEMap::NodeID ID;
417 ID.SetOpcode(N->getOpcode());
418 ID.SetValueTypes(N->value_begin());
420 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
423 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
424 /// were replaced with those specified. If this node is never memoized,
425 /// return null, otherwise return a pointer to the slot it would take. If a
426 /// node already exists with these operands, the slot will be non-null.
427 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
428 SDOperand Op1, SDOperand Op2,
430 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
431 return 0; // Never add these nodes.
433 // Check that remaining values produced are not flags.
434 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
435 if (N->getValueType(i) == MVT::Flag)
436 return 0; // Never CSE anything that produces a flag.
438 SelectionDAGCSEMap::NodeID ID;
439 ID.SetOpcode(N->getOpcode());
440 ID.SetValueTypes(N->value_begin());
441 ID.SetOperands(Op1, Op2);
442 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
446 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
447 /// were replaced with those specified. If this node is never memoized,
448 /// return null, otherwise return a pointer to the slot it would take. If a
449 /// node already exists with these operands, the slot will be non-null.
450 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
451 const SDOperand *Ops,unsigned NumOps,
453 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
454 return 0; // Never add these nodes.
456 // Check that remaining values produced are not flags.
457 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
458 if (N->getValueType(i) == MVT::Flag)
459 return 0; // Never CSE anything that produces a flag.
461 SelectionDAGCSEMap::NodeID ID;
462 ID.SetOpcode(N->getOpcode());
463 ID.SetValueTypes(N->value_begin());
464 ID.SetOperands(Ops, NumOps);
465 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
469 SelectionDAG::~SelectionDAG() {
470 while (!AllNodes.empty()) {
471 SDNode *N = AllNodes.begin();
472 delete [] N->OperandList;
475 AllNodes.pop_front();
479 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
480 if (Op.getValueType() == VT) return Op;
481 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
482 return getNode(ISD::AND, Op.getValueType(), Op,
483 getConstant(Imm, Op.getValueType()));
486 SDOperand SelectionDAG::getString(const std::string &Val) {
487 StringSDNode *&N = StringNodes[Val];
489 N = new StringSDNode(Val);
490 AllNodes.push_back(N);
492 return SDOperand(N, 0);
495 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
496 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
497 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
499 // Mask out any bits that are not valid for this constant.
500 Val &= MVT::getIntVTBitMask(VT);
502 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
503 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
506 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
507 return SDOperand(E, 0);
508 SDNode *N = new ConstantSDNode(isT, Val, VT);
509 CSEMap.InsertNode(N, IP);
510 AllNodes.push_back(N);
511 return SDOperand(N, 0);
515 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
517 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
519 Val = (float)Val; // Mask out extra precision.
521 // Do the map lookup using the actual bit pattern for the floating point
522 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
523 // we don't have issues with SNANs.
524 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
525 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
526 ID.AddInteger(DoubleToBits(Val));
528 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
529 return SDOperand(E, 0);
530 SDNode *N = new ConstantFPSDNode(isTarget, Val, VT);
531 CSEMap.InsertNode(N, IP);
532 AllNodes.push_back(N);
533 return SDOperand(N, 0);
536 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
537 MVT::ValueType VT, int Offset,
539 unsigned Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
540 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
542 ID.AddInteger(Offset);
544 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
545 return SDOperand(E, 0);
546 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
547 CSEMap.InsertNode(N, IP);
548 AllNodes.push_back(N);
549 return SDOperand(N, 0);
552 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
554 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
555 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
558 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
559 return SDOperand(E, 0);
560 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
561 CSEMap.InsertNode(N, IP);
562 AllNodes.push_back(N);
563 return SDOperand(N, 0);
566 SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
567 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
568 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
571 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
572 return SDOperand(E, 0);
573 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
574 CSEMap.InsertNode(N, IP);
575 AllNodes.push_back(N);
576 return SDOperand(N, 0);
579 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
580 unsigned Alignment, int Offset,
582 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
583 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
584 ID.AddInteger(Alignment);
585 ID.AddInteger(Offset);
588 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
589 return SDOperand(E, 0);
590 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
591 CSEMap.InsertNode(N, IP);
592 AllNodes.push_back(N);
593 return SDOperand(N, 0);
597 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
598 SelectionDAGCSEMap::NodeID ID(ISD::BasicBlock, getNodeValueTypes(MVT::Other));
601 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
602 return SDOperand(E, 0);
603 SDNode *N = new BasicBlockSDNode(MBB);
604 CSEMap.InsertNode(N, IP);
605 AllNodes.push_back(N);
606 return SDOperand(N, 0);
609 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
610 if ((unsigned)VT >= ValueTypeNodes.size())
611 ValueTypeNodes.resize(VT+1);
612 if (ValueTypeNodes[VT] == 0) {
613 ValueTypeNodes[VT] = new VTSDNode(VT);
614 AllNodes.push_back(ValueTypeNodes[VT]);
617 return SDOperand(ValueTypeNodes[VT], 0);
620 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
621 SDNode *&N = ExternalSymbols[Sym];
622 if (N) return SDOperand(N, 0);
623 N = new ExternalSymbolSDNode(false, Sym, VT);
624 AllNodes.push_back(N);
625 return SDOperand(N, 0);
628 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
630 SDNode *&N = TargetExternalSymbols[Sym];
631 if (N) return SDOperand(N, 0);
632 N = new ExternalSymbolSDNode(true, Sym, VT);
633 AllNodes.push_back(N);
634 return SDOperand(N, 0);
637 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
638 if ((unsigned)Cond >= CondCodeNodes.size())
639 CondCodeNodes.resize(Cond+1);
641 if (CondCodeNodes[Cond] == 0) {
642 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
643 AllNodes.push_back(CondCodeNodes[Cond]);
645 return SDOperand(CondCodeNodes[Cond], 0);
648 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
649 SelectionDAGCSEMap::NodeID ID(ISD::Register, getNodeValueTypes(VT));
650 ID.AddInteger(RegNo);
652 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
653 return SDOperand(E, 0);
654 SDNode *N = new RegisterSDNode(RegNo, VT);
655 CSEMap.InsertNode(N, IP);
656 AllNodes.push_back(N);
657 return SDOperand(N, 0);
660 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
661 assert((!V || isa<PointerType>(V->getType())) &&
662 "SrcValue is not a pointer?");
664 SelectionDAGCSEMap::NodeID ID(ISD::SRCVALUE, getNodeValueTypes(MVT::Other));
666 ID.AddInteger(Offset);
668 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
669 return SDOperand(E, 0);
670 SDNode *N = new SrcValueSDNode(V, Offset);
671 CSEMap.InsertNode(N, IP);
672 AllNodes.push_back(N);
673 return SDOperand(N, 0);
676 SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
677 SDOperand N2, ISD::CondCode Cond) {
678 // These setcc operations always fold.
682 case ISD::SETFALSE2: return getConstant(0, VT);
684 case ISD::SETTRUE2: return getConstant(1, VT);
696 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
700 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
701 uint64_t C2 = N2C->getValue();
702 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
703 uint64_t C1 = N1C->getValue();
705 // Sign extend the operands if required
706 if (ISD::isSignedIntSetCC(Cond)) {
707 C1 = N1C->getSignExtended();
708 C2 = N2C->getSignExtended();
712 default: assert(0 && "Unknown integer setcc!");
713 case ISD::SETEQ: return getConstant(C1 == C2, VT);
714 case ISD::SETNE: return getConstant(C1 != C2, VT);
715 case ISD::SETULT: return getConstant(C1 < C2, VT);
716 case ISD::SETUGT: return getConstant(C1 > C2, VT);
717 case ISD::SETULE: return getConstant(C1 <= C2, VT);
718 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
719 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
720 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
721 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
722 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
725 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
726 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
727 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
729 // If the comparison constant has bits in the upper part, the
730 // zero-extended value could never match.
731 if (C2 & (~0ULL << InSize)) {
732 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
736 case ISD::SETEQ: return getConstant(0, VT);
739 case ISD::SETNE: return getConstant(1, VT);
742 // True if the sign bit of C2 is set.
743 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
746 // True if the sign bit of C2 isn't set.
747 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
753 // Otherwise, we can perform the comparison with the low bits.
761 return getSetCC(VT, N1.getOperand(0),
762 getConstant(C2, N1.getOperand(0).getValueType()),
765 break; // todo, be more careful with signed comparisons
767 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
768 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
769 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
770 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
771 MVT::ValueType ExtDstTy = N1.getValueType();
772 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
774 // If the extended part has any inconsistent bits, it cannot ever
775 // compare equal. In other words, they have to be all ones or all
778 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
779 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
780 return getConstant(Cond == ISD::SETNE, VT);
782 // Otherwise, make this a use of a zext.
783 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
784 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
788 uint64_t MinVal, MaxVal;
789 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
790 if (ISD::isSignedIntSetCC(Cond)) {
791 MinVal = 1ULL << (OperandBitSize-1);
792 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
793 MaxVal = ~0ULL >> (65-OperandBitSize);
798 MaxVal = ~0ULL >> (64-OperandBitSize);
801 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
802 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
803 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
804 --C2; // X >= C1 --> X > (C1-1)
805 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
806 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
809 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
810 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
811 ++C2; // X <= C1 --> X < (C1+1)
812 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
813 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
816 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
817 return getConstant(0, VT); // X < MIN --> false
819 // Canonicalize setgt X, Min --> setne X, Min
820 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
821 return getSetCC(VT, N1, N2, ISD::SETNE);
823 // If we have setult X, 1, turn it into seteq X, 0
824 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
825 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
827 // If we have setugt X, Max-1, turn it into seteq X, Max
828 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
829 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
832 // If we have "setcc X, C1", check to see if we can shrink the immediate
835 // SETUGT X, SINTMAX -> SETLT X, 0
836 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
837 C2 == (~0ULL >> (65-OperandBitSize)))
838 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
840 // FIXME: Implement the rest of these.
843 // Fold bit comparisons when we can.
844 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
845 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
846 if (ConstantSDNode *AndRHS =
847 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
848 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
849 // Perform the xform if the AND RHS is a single bit.
850 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
851 return getNode(ISD::SRL, VT, N1,
852 getConstant(Log2_64(AndRHS->getValue()),
853 TLI.getShiftAmountTy()));
855 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
856 // (X & 8) == 8 --> (X & 8) >> 3
857 // Perform the xform if C2 is a single bit.
858 if ((C2 & (C2-1)) == 0) {
859 return getNode(ISD::SRL, VT, N1,
860 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
865 } else if (isa<ConstantSDNode>(N1.Val)) {
866 // Ensure that the constant occurs on the RHS.
867 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
870 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
871 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
872 double C1 = N1C->getValue(), C2 = N2C->getValue();
875 default: break; // FIXME: Implement the rest of these!
876 case ISD::SETEQ: return getConstant(C1 == C2, VT);
877 case ISD::SETNE: return getConstant(C1 != C2, VT);
878 case ISD::SETLT: return getConstant(C1 < C2, VT);
879 case ISD::SETGT: return getConstant(C1 > C2, VT);
880 case ISD::SETLE: return getConstant(C1 <= C2, VT);
881 case ISD::SETGE: return getConstant(C1 >= C2, VT);
884 // Ensure that the constant occurs on the RHS.
885 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
888 // Could not fold it.
892 /// getNode - Gets or creates the specified node.
894 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
895 MVT::ValueType *VTs = getNodeValueTypes(VT);
896 SelectionDAGCSEMap::NodeID ID(Opcode, VTs);
898 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
899 return SDOperand(E, 0);
900 SDNode *N = new SDNode(Opcode, VT);
901 CSEMap.InsertNode(N, IP);
903 AllNodes.push_back(N);
904 return SDOperand(N, 0);
907 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
910 // Constant fold unary operations with an integer constant operand.
911 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
912 uint64_t Val = C->getValue();
915 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
916 case ISD::ANY_EXTEND:
917 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
918 case ISD::TRUNCATE: return getConstant(Val, VT);
919 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
920 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
921 case ISD::BIT_CONVERT:
922 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
923 return getConstantFP(BitsToFloat(Val), VT);
924 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
925 return getConstantFP(BitsToDouble(Val), VT);
929 default: assert(0 && "Invalid bswap!"); break;
930 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
931 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
932 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
937 default: assert(0 && "Invalid ctpop!"); break;
938 case MVT::i1: return getConstant(Val != 0, VT);
940 Tmp1 = (unsigned)Val & 0xFF;
941 return getConstant(CountPopulation_32(Tmp1), VT);
943 Tmp1 = (unsigned)Val & 0xFFFF;
944 return getConstant(CountPopulation_32(Tmp1), VT);
946 return getConstant(CountPopulation_32((unsigned)Val), VT);
948 return getConstant(CountPopulation_64(Val), VT);
952 default: assert(0 && "Invalid ctlz!"); break;
953 case MVT::i1: return getConstant(Val == 0, VT);
955 Tmp1 = (unsigned)Val & 0xFF;
956 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
958 Tmp1 = (unsigned)Val & 0xFFFF;
959 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
961 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
963 return getConstant(CountLeadingZeros_64(Val), VT);
967 default: assert(0 && "Invalid cttz!"); break;
968 case MVT::i1: return getConstant(Val == 0, VT);
970 Tmp1 = (unsigned)Val | 0x100;
971 return getConstant(CountTrailingZeros_32(Tmp1), VT);
973 Tmp1 = (unsigned)Val | 0x10000;
974 return getConstant(CountTrailingZeros_32(Tmp1), VT);
976 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
978 return getConstant(CountTrailingZeros_64(Val), VT);
983 // Constant fold unary operations with an floating point constant operand.
984 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
987 return getConstantFP(-C->getValue(), VT);
989 return getConstantFP(fabs(C->getValue()), VT);
992 return getConstantFP(C->getValue(), VT);
993 case ISD::FP_TO_SINT:
994 return getConstant((int64_t)C->getValue(), VT);
995 case ISD::FP_TO_UINT:
996 return getConstant((uint64_t)C->getValue(), VT);
997 case ISD::BIT_CONVERT:
998 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
999 return getConstant(FloatToBits(C->getValue()), VT);
1000 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1001 return getConstant(DoubleToBits(C->getValue()), VT);
1005 unsigned OpOpcode = Operand.Val->getOpcode();
1007 case ISD::TokenFactor:
1008 return Operand; // Factor of one node? No factor.
1009 case ISD::SIGN_EXTEND:
1010 if (Operand.getValueType() == VT) return Operand; // noop extension
1011 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
1012 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1013 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1015 case ISD::ZERO_EXTEND:
1016 if (Operand.getValueType() == VT) return Operand; // noop extension
1017 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
1018 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1019 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1021 case ISD::ANY_EXTEND:
1022 if (Operand.getValueType() == VT) return Operand; // noop extension
1023 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
1024 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1025 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1026 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1029 if (Operand.getValueType() == VT) return Operand; // noop truncate
1030 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
1031 if (OpOpcode == ISD::TRUNCATE)
1032 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1033 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1034 OpOpcode == ISD::ANY_EXTEND) {
1035 // If the source is smaller than the dest, we still need an extend.
1036 if (Operand.Val->getOperand(0).getValueType() < VT)
1037 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1038 else if (Operand.Val->getOperand(0).getValueType() > VT)
1039 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1041 return Operand.Val->getOperand(0);
1044 case ISD::BIT_CONVERT:
1045 // Basic sanity checking.
1046 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
1047 && "Cannot BIT_CONVERT between two different types!");
1048 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1049 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1050 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1051 if (OpOpcode == ISD::UNDEF)
1052 return getNode(ISD::UNDEF, VT);
1054 case ISD::SCALAR_TO_VECTOR:
1055 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1056 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1057 "Illegal SCALAR_TO_VECTOR node!");
1060 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1061 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1062 Operand.Val->getOperand(0));
1063 if (OpOpcode == ISD::FNEG) // --X -> X
1064 return Operand.Val->getOperand(0);
1067 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1068 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1073 MVT::ValueType *VTs = getNodeValueTypes(VT);
1074 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1075 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Operand);
1077 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1078 return SDOperand(E, 0);
1079 N = new SDNode(Opcode, Operand);
1080 N->setValueTypes(VTs, 1);
1081 CSEMap.InsertNode(N, IP);
1083 N = new SDNode(Opcode, Operand);
1084 N->setValueTypes(VTs, 1);
1086 AllNodes.push_back(N);
1087 return SDOperand(N, 0);
1092 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1093 SDOperand N1, SDOperand N2) {
1096 case ISD::TokenFactor:
1097 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1098 N2.getValueType() == MVT::Other && "Invalid token factor!");
1107 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1114 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1121 assert(N1.getValueType() == N2.getValueType() &&
1122 N1.getValueType() == VT && "Binary operator types must match!");
1124 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1125 assert(N1.getValueType() == VT &&
1126 MVT::isFloatingPoint(N1.getValueType()) &&
1127 MVT::isFloatingPoint(N2.getValueType()) &&
1128 "Invalid FCOPYSIGN!");
1135 assert(VT == N1.getValueType() &&
1136 "Shift operators return type must be the same as their first arg");
1137 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1138 VT != MVT::i1 && "Shifts only work on integers");
1140 case ISD::FP_ROUND_INREG: {
1141 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1142 assert(VT == N1.getValueType() && "Not an inreg round!");
1143 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1144 "Cannot FP_ROUND_INREG integer types");
1145 assert(EVT <= VT && "Not rounding down!");
1148 case ISD::AssertSext:
1149 case ISD::AssertZext:
1150 case ISD::SIGN_EXTEND_INREG: {
1151 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1152 assert(VT == N1.getValueType() && "Not an inreg extend!");
1153 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1154 "Cannot *_EXTEND_INREG FP types");
1155 assert(EVT <= VT && "Not extending!");
1162 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1163 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1165 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1166 int64_t Val = N1C->getValue();
1167 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1168 Val <<= 64-FromBits;
1169 Val >>= 64-FromBits;
1170 return getConstant(Val, VT);
1174 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1176 case ISD::ADD: return getConstant(C1 + C2, VT);
1177 case ISD::SUB: return getConstant(C1 - C2, VT);
1178 case ISD::MUL: return getConstant(C1 * C2, VT);
1180 if (C2) return getConstant(C1 / C2, VT);
1183 if (C2) return getConstant(C1 % C2, VT);
1186 if (C2) return getConstant(N1C->getSignExtended() /
1187 N2C->getSignExtended(), VT);
1190 if (C2) return getConstant(N1C->getSignExtended() %
1191 N2C->getSignExtended(), VT);
1193 case ISD::AND : return getConstant(C1 & C2, VT);
1194 case ISD::OR : return getConstant(C1 | C2, VT);
1195 case ISD::XOR : return getConstant(C1 ^ C2, VT);
1196 case ISD::SHL : return getConstant(C1 << C2, VT);
1197 case ISD::SRL : return getConstant(C1 >> C2, VT);
1198 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
1200 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1203 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1207 } else { // Cannonicalize constant to RHS if commutative
1208 if (isCommutativeBinOp(Opcode)) {
1209 std::swap(N1C, N2C);
1215 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1216 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1219 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1221 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1222 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1223 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1225 if (C2) return getConstantFP(C1 / C2, VT);
1228 if (C2) return getConstantFP(fmod(C1, C2), VT);
1230 case ISD::FCOPYSIGN: {
1241 if (u2.I < 0) // Sign bit of RHS set?
1242 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1244 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1245 return getConstantFP(u1.F, VT);
1249 } else { // Cannonicalize constant to RHS if commutative
1250 if (isCommutativeBinOp(Opcode)) {
1251 std::swap(N1CFP, N2CFP);
1257 // Canonicalize an UNDEF to the RHS, even over a constant.
1258 if (N1.getOpcode() == ISD::UNDEF) {
1259 if (isCommutativeBinOp(Opcode)) {
1263 case ISD::FP_ROUND_INREG:
1264 case ISD::SIGN_EXTEND_INREG:
1270 return N1; // fold op(undef, arg2) -> undef
1277 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1282 // Fold a bunch of operators when the RHS is undef.
1283 if (N2.getOpcode() == ISD::UNDEF) {
1297 return N2; // fold op(arg1, undef) -> undef
1302 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1304 return getConstant(MVT::getIntVTBitMask(VT), VT);
1310 // Finally, fold operations that do not require constants.
1312 case ISD::FP_ROUND_INREG:
1313 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1315 case ISD::SIGN_EXTEND_INREG: {
1316 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1317 if (EVT == VT) return N1; // Not actually extending
1321 // FIXME: figure out how to safely handle things like
1322 // int foo(int x) { return 1 << (x & 255); }
1323 // int bar() { return foo(256); }
1328 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1329 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
1330 return getNode(Opcode, VT, N1, N2.getOperand(0));
1331 else if (N2.getOpcode() == ISD::AND)
1332 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1333 // If the and is only masking out bits that cannot effect the shift,
1334 // eliminate the and.
1335 unsigned NumBits = MVT::getSizeInBits(VT);
1336 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1337 return getNode(Opcode, VT, N1, N2.getOperand(0));
1343 // Memoize this node if possible.
1345 MVT::ValueType *VTs = getNodeValueTypes(VT);
1346 if (VT != MVT::Flag) {
1347 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2);
1349 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1350 return SDOperand(E, 0);
1351 N = new SDNode(Opcode, N1, N2);
1352 N->setValueTypes(VTs, 1);
1353 CSEMap.InsertNode(N, IP);
1355 N = new SDNode(Opcode, N1, N2);
1356 N->setValueTypes(VTs, 1);
1359 AllNodes.push_back(N);
1360 return SDOperand(N, 0);
1363 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1364 SDOperand N1, SDOperand N2, SDOperand N3) {
1365 // Perform various simplifications.
1366 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1367 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1368 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1371 // Use SimplifySetCC to simplify SETCC's.
1372 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
1373 if (Simp.Val) return Simp;
1378 if (N1C->getValue())
1379 return N2; // select true, X, Y -> X
1381 return N3; // select false, X, Y -> Y
1383 if (N2 == N3) return N2; // select C, X, X -> X
1387 if (N2C->getValue()) // Unconditional branch
1388 return getNode(ISD::BR, MVT::Other, N1, N3);
1390 return N1; // Never-taken branch
1392 case ISD::VECTOR_SHUFFLE:
1393 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1394 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1395 N3.getOpcode() == ISD::BUILD_VECTOR &&
1396 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1397 "Illegal VECTOR_SHUFFLE node!");
1401 // Memoize node if it doesn't produce a flag.
1403 MVT::ValueType *VTs = getNodeValueTypes(VT);
1405 if (VT != MVT::Flag) {
1406 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2, N3);
1408 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1409 return SDOperand(E, 0);
1410 N = new SDNode(Opcode, N1, N2, N3);
1411 N->setValueTypes(VTs, 1);
1412 CSEMap.InsertNode(N, IP);
1414 N = new SDNode(Opcode, N1, N2, N3);
1415 N->setValueTypes(VTs, 1);
1417 AllNodes.push_back(N);
1418 return SDOperand(N, 0);
1421 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1422 SDOperand N1, SDOperand N2, SDOperand N3,
1424 SDOperand Ops[] = { N1, N2, N3, N4 };
1425 return getNode(Opcode, VT, Ops, 4);
1428 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1429 SDOperand N1, SDOperand N2, SDOperand N3,
1430 SDOperand N4, SDOperand N5) {
1431 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1432 return getNode(Opcode, VT, Ops, 5);
1435 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1436 SDOperand Chain, SDOperand Ptr,
1438 MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other);
1440 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, SV);
1442 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1443 return SDOperand(E, 0);
1444 SDNode *N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1445 N->setValueTypes(VTs, 2);
1446 CSEMap.InsertNode(N, IP);
1447 AllNodes.push_back(N);
1448 return SDOperand(N, 0);
1451 SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1452 SDOperand Chain, SDOperand Ptr,
1454 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1455 getValueType(EVT) };
1456 std::vector<MVT::ValueType> VTs;
1458 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1459 return getNode(ISD::VLOAD, VTs, Ops, 5);
1462 SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1463 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1464 MVT::ValueType EVT) {
1465 SDOperand Ops[] = { Chain, Ptr, SV, getValueType(EVT) };
1466 std::vector<MVT::ValueType> VTs;
1468 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1469 return getNode(Opcode, VTs, Ops, 4);
1472 SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1473 SDOperand Chain, SDOperand Ptr,
1475 SDOperand Ops[] = { Chain, Ptr, SV };
1476 std::vector<MVT::ValueType> VTs;
1478 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1479 return getNode(ISD::VAARG, VTs, Ops, 3);
1482 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1483 const SDOperand *Ops, unsigned NumOps) {
1485 case 0: return getNode(Opcode, VT);
1486 case 1: return getNode(Opcode, VT, Ops[0]);
1487 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1488 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
1494 case ISD::TRUNCSTORE: {
1495 assert(NumOps == 5 && "TRUNCSTORE takes 5 operands!");
1496 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1497 #if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1498 // If this is a truncating store of a constant, convert to the desired type
1499 // and store it instead.
1500 if (isa<Constant>(Ops[0])) {
1501 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1502 if (isa<Constant>(Op))
1505 // Also for ConstantFP?
1507 if (Ops[0].getValueType() == EVT) // Normal store?
1508 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1509 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1510 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1511 "Can't do FP-INT conversion!");
1514 case ISD::SELECT_CC: {
1515 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
1516 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1517 "LHS and RHS of condition must have same type!");
1518 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1519 "True and False arms of SelectCC must have same type!");
1520 assert(Ops[2].getValueType() == VT &&
1521 "select_cc node must be of same type as true and false value!");
1525 assert(NumOps == 5 && "BR_CC takes 5 operands!");
1526 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1527 "LHS/RHS of comparison should match types!");
1534 MVT::ValueType *VTs = getNodeValueTypes(VT);
1535 if (VT != MVT::Flag) {
1536 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Ops, NumOps);
1538 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1539 return SDOperand(E, 0);
1540 N = new SDNode(Opcode, Ops, NumOps);
1541 N->setValueTypes(VTs, 1);
1542 CSEMap.InsertNode(N, IP);
1544 N = new SDNode(Opcode, Ops, NumOps);
1545 N->setValueTypes(VTs, 1);
1547 AllNodes.push_back(N);
1548 return SDOperand(N, 0);
1551 SDOperand SelectionDAG::getNode(unsigned Opcode,
1552 std::vector<MVT::ValueType> &ResultTys,
1553 const SDOperand *Ops, unsigned NumOps) {
1554 if (ResultTys.size() == 1)
1555 return getNode(Opcode, ResultTys[0], Ops, NumOps);
1560 case ISD::ZEXTLOAD: {
1561 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1562 assert(NumOps == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1563 // If they are asking for an extending load from/to the same thing, return a
1565 if (ResultTys[0] == EVT)
1566 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1567 if (MVT::isVector(ResultTys[0])) {
1568 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1569 "Invalid vector extload!");
1571 assert(EVT < ResultTys[0] &&
1572 "Should only be an extending load, not truncating!");
1574 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1575 "Cannot sign/zero extend a FP/Vector load!");
1576 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1577 "Cannot convert from FP to Int or Int -> FP!");
1581 // FIXME: figure out how to safely handle things like
1582 // int foo(int x) { return 1 << (x & 255); }
1583 // int bar() { return foo(256); }
1585 case ISD::SRA_PARTS:
1586 case ISD::SRL_PARTS:
1587 case ISD::SHL_PARTS:
1588 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1589 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
1590 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1591 else if (N3.getOpcode() == ISD::AND)
1592 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1593 // If the and is only masking out bits that cannot effect the shift,
1594 // eliminate the and.
1595 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1596 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1597 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1603 // Memoize the node unless it returns a flag.
1605 MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
1606 if (ResultTys.back() != MVT::Flag) {
1607 SelectionDAGCSEMap::NodeID ID;
1608 ID.SetOpcode(Opcode);
1609 ID.SetValueTypes(VTs);
1610 ID.SetOperands(&Ops[0], NumOps);
1612 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1613 return SDOperand(E, 0);
1614 N = new SDNode(Opcode, Ops, NumOps);
1615 N->setValueTypes(VTs, ResultTys.size());
1616 CSEMap.InsertNode(N, IP);
1618 N = new SDNode(Opcode, Ops, NumOps);
1619 N->setValueTypes(VTs, ResultTys.size());
1621 AllNodes.push_back(N);
1622 return SDOperand(N, 0);
1626 MVT::ValueType *SelectionDAG::getNodeValueTypes(MVT::ValueType VT) {
1627 return SDNode::getValueTypeList(VT);
1630 MVT::ValueType *SelectionDAG::getNodeValueTypes(
1631 std::vector<MVT::ValueType> &RetVals) {
1632 switch (RetVals.size()) {
1633 case 0: assert(0 && "Cannot have nodes without results!");
1634 case 1: return SDNode::getValueTypeList(RetVals[0]);
1635 case 2: return getNodeValueTypes(RetVals[0], RetVals[1]);
1639 std::list<std::vector<MVT::ValueType> >::iterator I =
1640 std::find(VTList.begin(), VTList.end(), RetVals);
1641 if (I == VTList.end()) {
1642 VTList.push_front(RetVals);
1649 MVT::ValueType *SelectionDAG::getNodeValueTypes(MVT::ValueType VT1,
1650 MVT::ValueType VT2) {
1651 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1652 E = VTList.end(); I != E; ++I) {
1653 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
1656 std::vector<MVT::ValueType> V;
1659 VTList.push_front(V);
1660 return &(*VTList.begin())[0];
1663 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1664 /// specified operands. If the resultant node already exists in the DAG,
1665 /// this does not modify the specified node, instead it returns the node that
1666 /// already exists. If the resultant node does not exist in the DAG, the
1667 /// input node is returned. As a degenerate case, if you specify the same
1668 /// input operands as the node already has, the input node is returned.
1669 SDOperand SelectionDAG::
1670 UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1671 SDNode *N = InN.Val;
1672 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1674 // Check to see if there is no change.
1675 if (Op == N->getOperand(0)) return InN;
1677 // See if the modified node already exists.
1678 void *InsertPos = 0;
1679 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1680 return SDOperand(Existing, InN.ResNo);
1682 // Nope it doesn't. Remove the node from it's current place in the maps.
1684 RemoveNodeFromCSEMaps(N);
1686 // Now we update the operands.
1687 N->OperandList[0].Val->removeUser(N);
1689 N->OperandList[0] = Op;
1691 // If this gets put into a CSE map, add it.
1692 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1696 SDOperand SelectionDAG::
1697 UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1698 SDNode *N = InN.Val;
1699 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1701 // Check to see if there is no change.
1702 bool AnyChange = false;
1703 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1704 return InN; // No operands changed, just return the input node.
1706 // See if the modified node already exists.
1707 void *InsertPos = 0;
1708 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1709 return SDOperand(Existing, InN.ResNo);
1711 // Nope it doesn't. Remove the node from it's current place in the maps.
1713 RemoveNodeFromCSEMaps(N);
1715 // Now we update the operands.
1716 if (N->OperandList[0] != Op1) {
1717 N->OperandList[0].Val->removeUser(N);
1718 Op1.Val->addUser(N);
1719 N->OperandList[0] = Op1;
1721 if (N->OperandList[1] != Op2) {
1722 N->OperandList[1].Val->removeUser(N);
1723 Op2.Val->addUser(N);
1724 N->OperandList[1] = Op2;
1727 // If this gets put into a CSE map, add it.
1728 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1732 SDOperand SelectionDAG::
1733 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1734 SDOperand Ops[] = { Op1, Op2, Op3 };
1735 return UpdateNodeOperands(N, Ops, 3);
1738 SDOperand SelectionDAG::
1739 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1740 SDOperand Op3, SDOperand Op4) {
1741 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
1742 return UpdateNodeOperands(N, Ops, 4);
1745 SDOperand SelectionDAG::
1746 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1747 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1748 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
1749 return UpdateNodeOperands(N, Ops, 5);
1753 SDOperand SelectionDAG::
1754 UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
1755 SDNode *N = InN.Val;
1756 assert(N->getNumOperands() == NumOps &&
1757 "Update with wrong number of operands");
1759 // Check to see if there is no change.
1760 bool AnyChange = false;
1761 for (unsigned i = 0; i != NumOps; ++i) {
1762 if (Ops[i] != N->getOperand(i)) {
1768 // No operands changed, just return the input node.
1769 if (!AnyChange) return InN;
1771 // See if the modified node already exists.
1772 void *InsertPos = 0;
1773 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
1774 return SDOperand(Existing, InN.ResNo);
1776 // Nope it doesn't. Remove the node from it's current place in the maps.
1778 RemoveNodeFromCSEMaps(N);
1780 // Now we update the operands.
1781 for (unsigned i = 0; i != NumOps; ++i) {
1782 if (N->OperandList[i] != Ops[i]) {
1783 N->OperandList[i].Val->removeUser(N);
1784 Ops[i].Val->addUser(N);
1785 N->OperandList[i] = Ops[i];
1789 // If this gets put into a CSE map, add it.
1790 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1797 /// SelectNodeTo - These are used for target selectors to *mutate* the
1798 /// specified node to have the specified return type, Target opcode, and
1799 /// operands. Note that target opcodes are stored as
1800 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1802 /// Note that SelectNodeTo returns the resultant node. If there is already a
1803 /// node of the specified opcode and operands, it returns that node instead of
1804 /// the current one.
1805 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1806 MVT::ValueType VT) {
1807 MVT::ValueType *VTs = getNodeValueTypes(VT);
1808 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1810 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1811 return SDOperand(ON, 0);
1813 RemoveNodeFromCSEMaps(N);
1815 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1816 N->setValueTypes(getNodeValueTypes(VT), 1);
1818 CSEMap.InsertNode(N, IP);
1819 return SDOperand(N, 0);
1822 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1823 MVT::ValueType VT, SDOperand Op1) {
1824 // If an identical node already exists, use it.
1825 MVT::ValueType *VTs = getNodeValueTypes(VT);
1826 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
1828 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1829 return SDOperand(ON, 0);
1831 RemoveNodeFromCSEMaps(N);
1832 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1833 N->setValueTypes(getNodeValueTypes(VT), 1);
1834 N->setOperands(Op1);
1835 CSEMap.InsertNode(N, IP);
1836 return SDOperand(N, 0);
1839 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1840 MVT::ValueType VT, SDOperand Op1,
1842 // If an identical node already exists, use it.
1843 MVT::ValueType *VTs = getNodeValueTypes(VT);
1844 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1846 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1847 return SDOperand(ON, 0);
1849 RemoveNodeFromCSEMaps(N);
1850 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1851 N->setValueTypes(VTs, 1);
1852 N->setOperands(Op1, Op2);
1854 CSEMap.InsertNode(N, IP); // Memoize the new node.
1855 return SDOperand(N, 0);
1858 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1859 MVT::ValueType VT, SDOperand Op1,
1860 SDOperand Op2, SDOperand Op3) {
1861 // If an identical node already exists, use it.
1862 MVT::ValueType *VTs = getNodeValueTypes(VT);
1863 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2, Op3);
1865 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1866 return SDOperand(ON, 0);
1868 RemoveNodeFromCSEMaps(N);
1869 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1870 N->setValueTypes(VTs, 1);
1871 N->setOperands(Op1, Op2, Op3);
1873 CSEMap.InsertNode(N, IP); // Memoize the new node.
1874 return SDOperand(N, 0);
1877 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1878 MVT::ValueType VT, SDOperand Op1,
1879 SDOperand Op2, SDOperand Op3,
1881 // If an identical node already exists, use it.
1882 MVT::ValueType *VTs = getNodeValueTypes(VT);
1883 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1889 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1890 return SDOperand(ON, 0);
1892 RemoveNodeFromCSEMaps(N);
1893 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1894 N->setValueTypes(VTs, 1);
1895 N->setOperands(Op1, Op2, Op3, Op4);
1897 CSEMap.InsertNode(N, IP); // Memoize the new node.
1898 return SDOperand(N, 0);
1901 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1902 MVT::ValueType VT, SDOperand Op1,
1903 SDOperand Op2, SDOperand Op3,
1904 SDOperand Op4, SDOperand Op5) {
1905 MVT::ValueType *VTs = getNodeValueTypes(VT);
1906 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1913 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1914 return SDOperand(ON, 0);
1916 RemoveNodeFromCSEMaps(N);
1917 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1918 N->setValueTypes(VTs, 1);
1919 N->setOperands(Op1, Op2, Op3, Op4, Op5);
1921 CSEMap.InsertNode(N, IP); // Memoize the new node.
1922 return SDOperand(N, 0);
1925 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1926 MVT::ValueType VT, SDOperand Op1,
1927 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1928 SDOperand Op5, SDOperand Op6) {
1929 MVT::ValueType *VTs = getNodeValueTypes(VT);
1930 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1938 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1939 return SDOperand(ON, 0);
1941 RemoveNodeFromCSEMaps(N);
1942 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1943 N->setValueTypes(VTs, 1);
1944 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
1946 CSEMap.InsertNode(N, IP); // Memoize the new node.
1947 return SDOperand(N, 0);
1950 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1951 MVT::ValueType VT, SDOperand Op1,
1952 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1953 SDOperand Op5, SDOperand Op6,
1955 MVT::ValueType *VTs = getNodeValueTypes(VT);
1956 // If an identical node already exists, use it.
1957 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1966 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1967 return SDOperand(ON, 0);
1969 RemoveNodeFromCSEMaps(N);
1970 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1971 N->setValueTypes(VTs, 1);
1972 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
1974 CSEMap.InsertNode(N, IP); // Memoize the new node.
1975 return SDOperand(N, 0);
1977 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1978 MVT::ValueType VT, SDOperand Op1,
1979 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1980 SDOperand Op5, SDOperand Op6,
1981 SDOperand Op7, SDOperand Op8) {
1982 // If an identical node already exists, use it.
1983 MVT::ValueType *VTs = getNodeValueTypes(VT);
1984 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1994 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1995 return SDOperand(ON, 0);
1997 RemoveNodeFromCSEMaps(N);
1998 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1999 N->setValueTypes(VTs, 1);
2000 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2002 CSEMap.InsertNode(N, IP); // Memoize the new node.
2003 return SDOperand(N, 0);
2006 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2007 MVT::ValueType VT1, MVT::ValueType VT2,
2008 SDOperand Op1, SDOperand Op2) {
2009 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2010 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
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, 2);
2018 N->setOperands(Op1, Op2);
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 VT1, MVT::ValueType VT2,
2026 SDOperand Op1, SDOperand Op2,
2028 // If an identical node already exists, use it.
2029 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2030 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
2033 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2034 return SDOperand(ON, 0);
2036 RemoveNodeFromCSEMaps(N);
2037 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2038 N->setValueTypes(VTs, 2);
2039 N->setOperands(Op1, Op2, Op3);
2041 CSEMap.InsertNode(N, IP); // Memoize the new node.
2042 return SDOperand(N, 0);
2045 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2046 MVT::ValueType VT1, MVT::ValueType VT2,
2047 SDOperand Op1, SDOperand Op2,
2048 SDOperand Op3, SDOperand Op4) {
2049 // If an identical node already exists, use it.
2050 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2051 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2057 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2058 return SDOperand(ON, 0);
2060 RemoveNodeFromCSEMaps(N);
2061 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2062 N->setValueTypes(VTs, 2);
2063 N->setOperands(Op1, Op2, Op3, Op4);
2065 CSEMap.InsertNode(N, IP); // Memoize the new node.
2066 return SDOperand(N, 0);
2069 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2070 MVT::ValueType VT1, MVT::ValueType VT2,
2071 SDOperand Op1, SDOperand Op2,
2072 SDOperand Op3, SDOperand Op4,
2074 // If an identical node already exists, use it.
2075 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2076 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2083 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2084 return SDOperand(ON, 0);
2086 RemoveNodeFromCSEMaps(N);
2087 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2088 N->setValueTypes(VTs, 2);
2089 N->setOperands(Op1, Op2, Op3, Op4, Op5);
2091 CSEMap.InsertNode(N, IP); // Memoize the new node.
2092 return SDOperand(N, 0);
2095 /// getTargetNode - These are used for target selectors to create a new node
2096 /// with specified return type(s), target opcode, and operands.
2098 /// Note that getTargetNode returns the resultant node. If there is already a
2099 /// node of the specified opcode and operands, it returns that node instead of
2100 /// the current one.
2101 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2102 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2104 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2106 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2108 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2109 SDOperand Op1, SDOperand Op2) {
2110 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2112 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2113 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2114 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2116 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2117 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2119 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2121 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2122 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2123 SDOperand Op4, SDOperand Op5) {
2124 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2126 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2127 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2128 SDOperand Op4, SDOperand Op5,
2130 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
2131 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 6).Val;
2133 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2134 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2135 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2137 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
2138 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 7).Val;
2140 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2141 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2142 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2143 SDOperand Op7, SDOperand Op8) {
2144 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8 };
2145 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 8).Val;
2147 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2148 const SDOperand *Ops, unsigned NumOps) {
2149 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
2151 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2152 MVT::ValueType VT2, SDOperand Op1) {
2153 std::vector<MVT::ValueType> ResultTys;
2154 ResultTys.push_back(VT1);
2155 ResultTys.push_back(VT2);
2156 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, &Op1, 1).Val;
2158 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2159 MVT::ValueType VT2, SDOperand Op1,
2161 std::vector<MVT::ValueType> ResultTys;
2162 ResultTys.push_back(VT1);
2163 ResultTys.push_back(VT2);
2164 SDOperand Ops[] = { Op1, Op2 };
2165 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 2).Val;
2167 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2168 MVT::ValueType VT2, SDOperand Op1,
2169 SDOperand Op2, SDOperand Op3) {
2170 std::vector<MVT::ValueType> ResultTys;
2171 ResultTys.push_back(VT1);
2172 ResultTys.push_back(VT2);
2173 SDOperand Ops[] = { Op1, Op2, Op3 };
2174 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 3).Val;
2176 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2177 MVT::ValueType VT2, SDOperand Op1,
2178 SDOperand Op2, SDOperand Op3,
2180 std::vector<MVT::ValueType> ResultTys;
2181 ResultTys.push_back(VT1);
2182 ResultTys.push_back(VT2);
2183 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2184 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 4).Val;
2186 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2187 MVT::ValueType VT2, SDOperand Op1,
2188 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2190 std::vector<MVT::ValueType> ResultTys;
2191 ResultTys.push_back(VT1);
2192 ResultTys.push_back(VT2);
2193 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2194 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 5).Val;
2196 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2197 MVT::ValueType VT2, SDOperand Op1,
2198 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2199 SDOperand Op5, SDOperand Op6) {
2200 std::vector<MVT::ValueType> ResultTys;
2201 ResultTys.push_back(VT1);
2202 ResultTys.push_back(VT2);
2203 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
2204 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 6).Val;
2206 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2207 MVT::ValueType VT2, SDOperand Op1,
2208 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2209 SDOperand Op5, SDOperand Op6,
2211 std::vector<MVT::ValueType> ResultTys;
2212 ResultTys.push_back(VT1);
2213 ResultTys.push_back(VT2);
2214 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
2215 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 7).Val;
2217 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2218 MVT::ValueType VT2, MVT::ValueType VT3,
2219 SDOperand Op1, SDOperand Op2) {
2220 std::vector<MVT::ValueType> ResultTys;
2221 ResultTys.push_back(VT1);
2222 ResultTys.push_back(VT2);
2223 ResultTys.push_back(VT3);
2224 SDOperand Ops[] = { Op1, Op2 };
2225 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 2).Val;
2227 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2228 MVT::ValueType VT2, MVT::ValueType VT3,
2229 SDOperand Op1, SDOperand Op2,
2230 SDOperand Op3, SDOperand Op4,
2232 std::vector<MVT::ValueType> ResultTys;
2233 ResultTys.push_back(VT1);
2234 ResultTys.push_back(VT2);
2235 ResultTys.push_back(VT3);
2236 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2237 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 5).Val;
2239 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2240 MVT::ValueType VT2, MVT::ValueType VT3,
2241 SDOperand Op1, SDOperand Op2,
2242 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2244 std::vector<MVT::ValueType> ResultTys;
2245 ResultTys.push_back(VT1);
2246 ResultTys.push_back(VT2);
2247 ResultTys.push_back(VT3);
2248 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
2249 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 6).Val;
2251 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2252 MVT::ValueType VT2, MVT::ValueType VT3,
2253 SDOperand Op1, SDOperand Op2,
2254 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2255 SDOperand Op6, SDOperand Op7) {
2256 std::vector<MVT::ValueType> ResultTys;
2257 ResultTys.push_back(VT1);
2258 ResultTys.push_back(VT2);
2259 ResultTys.push_back(VT3);
2260 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
2261 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 7).Val;
2263 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2265 const SDOperand *Ops, unsigned NumOps) {
2266 std::vector<MVT::ValueType> ResultTys;
2267 ResultTys.push_back(VT1);
2268 ResultTys.push_back(VT2);
2269 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, NumOps).Val;
2272 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2273 /// This can cause recursive merging of nodes in the DAG.
2275 /// This version assumes From/To have a single result value.
2277 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2278 std::vector<SDNode*> *Deleted) {
2279 SDNode *From = FromN.Val, *To = ToN.Val;
2280 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2281 "Cannot replace with this method!");
2282 assert(From != To && "Cannot replace uses of with self");
2284 while (!From->use_empty()) {
2285 // Process users until they are all gone.
2286 SDNode *U = *From->use_begin();
2288 // This node is about to morph, remove its old self from the CSE maps.
2289 RemoveNodeFromCSEMaps(U);
2291 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2293 if (I->Val == From) {
2294 From->removeUser(U);
2299 // Now that we have modified U, add it back to the CSE maps. If it already
2300 // exists there, recursively merge the results together.
2301 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2302 ReplaceAllUsesWith(U, Existing, Deleted);
2304 if (Deleted) Deleted->push_back(U);
2305 DeleteNodeNotInCSEMaps(U);
2310 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2311 /// This can cause recursive merging of nodes in the DAG.
2313 /// This version assumes From/To have matching types and numbers of result
2316 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2317 std::vector<SDNode*> *Deleted) {
2318 assert(From != To && "Cannot replace uses of with self");
2319 assert(From->getNumValues() == To->getNumValues() &&
2320 "Cannot use this version of ReplaceAllUsesWith!");
2321 if (From->getNumValues() == 1) { // If possible, use the faster version.
2322 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
2326 while (!From->use_empty()) {
2327 // Process users until they are all gone.
2328 SDNode *U = *From->use_begin();
2330 // This node is about to morph, remove its old self from the CSE maps.
2331 RemoveNodeFromCSEMaps(U);
2333 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2335 if (I->Val == From) {
2336 From->removeUser(U);
2341 // Now that we have modified U, add it back to the CSE maps. If it already
2342 // exists there, recursively merge the results together.
2343 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2344 ReplaceAllUsesWith(U, Existing, Deleted);
2346 if (Deleted) Deleted->push_back(U);
2347 DeleteNodeNotInCSEMaps(U);
2352 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2353 /// This can cause recursive merging of nodes in the DAG.
2355 /// This version can replace From with any result values. To must match the
2356 /// number and types of values returned by From.
2357 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
2358 const SDOperand *To,
2359 std::vector<SDNode*> *Deleted) {
2360 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
2361 // Degenerate case handled above.
2362 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
2366 while (!From->use_empty()) {
2367 // Process users until they are all gone.
2368 SDNode *U = *From->use_begin();
2370 // This node is about to morph, remove its old self from the CSE maps.
2371 RemoveNodeFromCSEMaps(U);
2373 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2375 if (I->Val == From) {
2376 const SDOperand &ToOp = To[I->ResNo];
2377 From->removeUser(U);
2379 ToOp.Val->addUser(U);
2382 // Now that we have modified U, add it back to the CSE maps. If it already
2383 // exists there, recursively merge the results together.
2384 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2385 ReplaceAllUsesWith(U, Existing, Deleted);
2387 if (Deleted) Deleted->push_back(U);
2388 DeleteNodeNotInCSEMaps(U);
2393 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2394 /// uses of other values produced by From.Val alone. The Deleted vector is
2395 /// handled the same was as for ReplaceAllUsesWith.
2396 void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2397 std::vector<SDNode*> &Deleted) {
2398 assert(From != To && "Cannot replace a value with itself");
2399 // Handle the simple, trivial, case efficiently.
2400 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2401 ReplaceAllUsesWith(From, To, &Deleted);
2405 // Get all of the users in a nice, deterministically ordered, uniqued set.
2406 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2408 while (!Users.empty()) {
2409 // We know that this user uses some value of From. If it is the right
2410 // value, update it.
2411 SDNode *User = Users.back();
2414 for (SDOperand *Op = User->OperandList,
2415 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2417 // Okay, we know this user needs to be updated. Remove its old self
2418 // from the CSE maps.
2419 RemoveNodeFromCSEMaps(User);
2421 // Update all operands that match "From".
2422 for (; Op != E; ++Op) {
2424 From.Val->removeUser(User);
2426 To.Val->addUser(User);
2430 // Now that we have modified User, add it back to the CSE maps. If it
2431 // already exists there, recursively merge the results together.
2432 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2433 unsigned NumDeleted = Deleted.size();
2434 ReplaceAllUsesWith(User, Existing, &Deleted);
2436 // User is now dead.
2437 Deleted.push_back(User);
2438 DeleteNodeNotInCSEMaps(User);
2440 // We have to be careful here, because ReplaceAllUsesWith could have
2441 // deleted a user of From, which means there may be dangling pointers
2442 // in the "Users" setvector. Scan over the deleted node pointers and
2443 // remove them from the setvector.
2444 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2445 Users.remove(Deleted[i]);
2447 break; // Exit the operand scanning loop.
2454 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2455 /// their allnodes order. It returns the maximum id.
2456 unsigned SelectionDAG::AssignNodeIds() {
2458 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2465 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
2466 /// based on their topological order. It returns the maximum id and a vector
2467 /// of the SDNodes* in assigned order by reference.
2468 unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
2469 unsigned DAGSize = AllNodes.size();
2470 std::vector<unsigned> InDegree(DAGSize);
2471 std::vector<SDNode*> Sources;
2473 // Use a two pass approach to avoid using a std::map which is slow.
2475 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2478 unsigned Degree = N->use_size();
2479 InDegree[N->getNodeId()] = Degree;
2481 Sources.push_back(N);
2485 while (!Sources.empty()) {
2486 SDNode *N = Sources.back();
2488 TopOrder.push_back(N);
2489 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2491 unsigned Degree = --InDegree[P->getNodeId()];
2493 Sources.push_back(P);
2497 // Second pass, assign the actual topological order as node ids.
2499 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2501 (*TI)->setNodeId(Id++);
2508 //===----------------------------------------------------------------------===//
2510 //===----------------------------------------------------------------------===//
2512 // Out-of-line virtual method to give class a home.
2513 void SDNode::ANCHOR() {
2516 /// getValueTypeList - Return a pointer to the specified value type.
2518 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2519 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2524 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2525 /// indicated value. This method ignores uses of other values defined by this
2527 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
2528 assert(Value < getNumValues() && "Bad value!");
2530 // If there is only one value, this is easy.
2531 if (getNumValues() == 1)
2532 return use_size() == NUses;
2533 if (Uses.size() < NUses) return false;
2535 SDOperand TheValue(const_cast<SDNode *>(this), Value);
2537 std::set<SDNode*> UsersHandled;
2539 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
2542 if (User->getNumOperands() == 1 ||
2543 UsersHandled.insert(User).second) // First time we've seen this?
2544 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2545 if (User->getOperand(i) == TheValue) {
2547 return false; // too many uses
2552 // Found exactly the right number of uses?
2557 // isOnlyUse - Return true if this node is the only use of N.
2558 bool SDNode::isOnlyUse(SDNode *N) const {
2560 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2571 // isOperand - Return true if this node is an operand of N.
2572 bool SDOperand::isOperand(SDNode *N) const {
2573 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2574 if (*this == N->getOperand(i))
2579 bool SDNode::isOperand(SDNode *N) const {
2580 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2581 if (this == N->OperandList[i].Val)
2586 const char *SDNode::getOperationName(const SelectionDAG *G) const {
2587 switch (getOpcode()) {
2589 if (getOpcode() < ISD::BUILTIN_OP_END)
2590 return "<<Unknown DAG Node>>";
2593 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
2594 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2595 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
2597 TargetLowering &TLI = G->getTargetLoweringInfo();
2599 TLI.getTargetNodeName(getOpcode());
2600 if (Name) return Name;
2603 return "<<Unknown Target Node>>";
2606 case ISD::PCMARKER: return "PCMarker";
2607 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
2608 case ISD::SRCVALUE: return "SrcValue";
2609 case ISD::EntryToken: return "EntryToken";
2610 case ISD::TokenFactor: return "TokenFactor";
2611 case ISD::AssertSext: return "AssertSext";
2612 case ISD::AssertZext: return "AssertZext";
2614 case ISD::STRING: return "String";
2615 case ISD::BasicBlock: return "BasicBlock";
2616 case ISD::VALUETYPE: return "ValueType";
2617 case ISD::Register: return "Register";
2619 case ISD::Constant: return "Constant";
2620 case ISD::ConstantFP: return "ConstantFP";
2621 case ISD::GlobalAddress: return "GlobalAddress";
2622 case ISD::FrameIndex: return "FrameIndex";
2623 case ISD::JumpTable: return "JumpTable";
2624 case ISD::ConstantPool: return "ConstantPool";
2625 case ISD::ExternalSymbol: return "ExternalSymbol";
2626 case ISD::INTRINSIC_WO_CHAIN: {
2627 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2628 return Intrinsic::getName((Intrinsic::ID)IID);
2630 case ISD::INTRINSIC_VOID:
2631 case ISD::INTRINSIC_W_CHAIN: {
2632 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
2633 return Intrinsic::getName((Intrinsic::ID)IID);
2636 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
2637 case ISD::TargetConstant: return "TargetConstant";
2638 case ISD::TargetConstantFP:return "TargetConstantFP";
2639 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2640 case ISD::TargetFrameIndex: return "TargetFrameIndex";
2641 case ISD::TargetJumpTable: return "TargetJumpTable";
2642 case ISD::TargetConstantPool: return "TargetConstantPool";
2643 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
2645 case ISD::CopyToReg: return "CopyToReg";
2646 case ISD::CopyFromReg: return "CopyFromReg";
2647 case ISD::UNDEF: return "undef";
2648 case ISD::MERGE_VALUES: return "mergevalues";
2649 case ISD::INLINEASM: return "inlineasm";
2650 case ISD::HANDLENODE: return "handlenode";
2651 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
2652 case ISD::CALL: return "call";
2655 case ISD::FABS: return "fabs";
2656 case ISD::FNEG: return "fneg";
2657 case ISD::FSQRT: return "fsqrt";
2658 case ISD::FSIN: return "fsin";
2659 case ISD::FCOS: return "fcos";
2662 case ISD::ADD: return "add";
2663 case ISD::SUB: return "sub";
2664 case ISD::MUL: return "mul";
2665 case ISD::MULHU: return "mulhu";
2666 case ISD::MULHS: return "mulhs";
2667 case ISD::SDIV: return "sdiv";
2668 case ISD::UDIV: return "udiv";
2669 case ISD::SREM: return "srem";
2670 case ISD::UREM: return "urem";
2671 case ISD::AND: return "and";
2672 case ISD::OR: return "or";
2673 case ISD::XOR: return "xor";
2674 case ISD::SHL: return "shl";
2675 case ISD::SRA: return "sra";
2676 case ISD::SRL: return "srl";
2677 case ISD::ROTL: return "rotl";
2678 case ISD::ROTR: return "rotr";
2679 case ISD::FADD: return "fadd";
2680 case ISD::FSUB: return "fsub";
2681 case ISD::FMUL: return "fmul";
2682 case ISD::FDIV: return "fdiv";
2683 case ISD::FREM: return "frem";
2684 case ISD::FCOPYSIGN: return "fcopysign";
2685 case ISD::VADD: return "vadd";
2686 case ISD::VSUB: return "vsub";
2687 case ISD::VMUL: return "vmul";
2688 case ISD::VSDIV: return "vsdiv";
2689 case ISD::VUDIV: return "vudiv";
2690 case ISD::VAND: return "vand";
2691 case ISD::VOR: return "vor";
2692 case ISD::VXOR: return "vxor";
2694 case ISD::SETCC: return "setcc";
2695 case ISD::SELECT: return "select";
2696 case ISD::SELECT_CC: return "select_cc";
2697 case ISD::VSELECT: return "vselect";
2698 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2699 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2700 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
2701 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
2702 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2703 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2704 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2705 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2706 case ISD::VBIT_CONVERT: return "vbit_convert";
2707 case ISD::ADDC: return "addc";
2708 case ISD::ADDE: return "adde";
2709 case ISD::SUBC: return "subc";
2710 case ISD::SUBE: return "sube";
2711 case ISD::SHL_PARTS: return "shl_parts";
2712 case ISD::SRA_PARTS: return "sra_parts";
2713 case ISD::SRL_PARTS: return "srl_parts";
2715 // Conversion operators.
2716 case ISD::SIGN_EXTEND: return "sign_extend";
2717 case ISD::ZERO_EXTEND: return "zero_extend";
2718 case ISD::ANY_EXTEND: return "any_extend";
2719 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
2720 case ISD::TRUNCATE: return "truncate";
2721 case ISD::FP_ROUND: return "fp_round";
2722 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
2723 case ISD::FP_EXTEND: return "fp_extend";
2725 case ISD::SINT_TO_FP: return "sint_to_fp";
2726 case ISD::UINT_TO_FP: return "uint_to_fp";
2727 case ISD::FP_TO_SINT: return "fp_to_sint";
2728 case ISD::FP_TO_UINT: return "fp_to_uint";
2729 case ISD::BIT_CONVERT: return "bit_convert";
2731 // Control flow instructions
2732 case ISD::BR: return "br";
2733 case ISD::BRIND: return "brind";
2734 case ISD::BRCOND: return "brcond";
2735 case ISD::BR_CC: return "br_cc";
2736 case ISD::RET: return "ret";
2737 case ISD::CALLSEQ_START: return "callseq_start";
2738 case ISD::CALLSEQ_END: return "callseq_end";
2741 case ISD::LOAD: return "load";
2742 case ISD::STORE: return "store";
2743 case ISD::VLOAD: return "vload";
2744 case ISD::EXTLOAD: return "extload";
2745 case ISD::SEXTLOAD: return "sextload";
2746 case ISD::ZEXTLOAD: return "zextload";
2747 case ISD::TRUNCSTORE: return "truncstore";
2748 case ISD::VAARG: return "vaarg";
2749 case ISD::VACOPY: return "vacopy";
2750 case ISD::VAEND: return "vaend";
2751 case ISD::VASTART: return "vastart";
2752 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2753 case ISD::EXTRACT_ELEMENT: return "extract_element";
2754 case ISD::BUILD_PAIR: return "build_pair";
2755 case ISD::STACKSAVE: return "stacksave";
2756 case ISD::STACKRESTORE: return "stackrestore";
2758 // Block memory operations.
2759 case ISD::MEMSET: return "memset";
2760 case ISD::MEMCPY: return "memcpy";
2761 case ISD::MEMMOVE: return "memmove";
2764 case ISD::BSWAP: return "bswap";
2765 case ISD::CTPOP: return "ctpop";
2766 case ISD::CTTZ: return "cttz";
2767 case ISD::CTLZ: return "ctlz";
2770 case ISD::LOCATION: return "location";
2771 case ISD::DEBUG_LOC: return "debug_loc";
2772 case ISD::DEBUG_LABEL: return "debug_label";
2775 switch (cast<CondCodeSDNode>(this)->get()) {
2776 default: assert(0 && "Unknown setcc condition!");
2777 case ISD::SETOEQ: return "setoeq";
2778 case ISD::SETOGT: return "setogt";
2779 case ISD::SETOGE: return "setoge";
2780 case ISD::SETOLT: return "setolt";
2781 case ISD::SETOLE: return "setole";
2782 case ISD::SETONE: return "setone";
2784 case ISD::SETO: return "seto";
2785 case ISD::SETUO: return "setuo";
2786 case ISD::SETUEQ: return "setue";
2787 case ISD::SETUGT: return "setugt";
2788 case ISD::SETUGE: return "setuge";
2789 case ISD::SETULT: return "setult";
2790 case ISD::SETULE: return "setule";
2791 case ISD::SETUNE: return "setune";
2793 case ISD::SETEQ: return "seteq";
2794 case ISD::SETGT: return "setgt";
2795 case ISD::SETGE: return "setge";
2796 case ISD::SETLT: return "setlt";
2797 case ISD::SETLE: return "setle";
2798 case ISD::SETNE: return "setne";
2803 void SDNode::dump() const { dump(0); }
2804 void SDNode::dump(const SelectionDAG *G) const {
2805 std::cerr << (void*)this << ": ";
2807 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2808 if (i) std::cerr << ",";
2809 if (getValueType(i) == MVT::Other)
2812 std::cerr << MVT::getValueTypeString(getValueType(i));
2814 std::cerr << " = " << getOperationName(G);
2817 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2818 if (i) std::cerr << ", ";
2819 std::cerr << (void*)getOperand(i).Val;
2820 if (unsigned RN = getOperand(i).ResNo)
2821 std::cerr << ":" << RN;
2824 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2825 std::cerr << "<" << CSDN->getValue() << ">";
2826 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2827 std::cerr << "<" << CSDN->getValue() << ">";
2828 } else if (const GlobalAddressSDNode *GADN =
2829 dyn_cast<GlobalAddressSDNode>(this)) {
2830 int offset = GADN->getOffset();
2832 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
2834 std::cerr << " + " << offset;
2836 std::cerr << " " << offset;
2837 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
2838 std::cerr << "<" << FIDN->getIndex() << ">";
2839 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
2840 int offset = CP->getOffset();
2841 std::cerr << "<" << *CP->get() << ">";
2843 std::cerr << " + " << offset;
2845 std::cerr << " " << offset;
2846 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
2848 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2850 std::cerr << LBB->getName() << " ";
2851 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
2852 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
2853 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2854 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2856 std::cerr << " #" << R->getReg();
2858 } else if (const ExternalSymbolSDNode *ES =
2859 dyn_cast<ExternalSymbolSDNode>(this)) {
2860 std::cerr << "'" << ES->getSymbol() << "'";
2861 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2863 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2865 std::cerr << "<null:" << M->getOffset() << ">";
2866 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2867 std::cerr << ":" << getValueTypeString(N->getVT());
2871 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
2872 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2873 if (N->getOperand(i).Val->hasOneUse())
2874 DumpNodes(N->getOperand(i).Val, indent+2, G);
2876 std::cerr << "\n" << std::string(indent+2, ' ')
2877 << (void*)N->getOperand(i).Val << ": <multiple use>";
2880 std::cerr << "\n" << std::string(indent, ' ');
2884 void SelectionDAG::dump() const {
2885 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
2886 std::vector<const SDNode*> Nodes;
2887 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2891 std::sort(Nodes.begin(), Nodes.end());
2893 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2894 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
2895 DumpNodes(Nodes[i], 2, this);
2898 DumpNodes(getRoot().Val, 2, this);
2900 std::cerr << "\n\n";
2903 /// InsertISelMapEntry - A helper function to insert a key / element pair
2904 /// into a SDOperand to SDOperand map. This is added to avoid the map
2905 /// insertion operator from being inlined.
2906 void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
2907 SDNode *Key, unsigned KeyResNo,
2908 SDNode *Element, unsigned ElementResNo) {
2909 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
2910 SDOperand(Element, ElementResNo)));