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 N->SetNextInBucket(0);
473 delete [] N->OperandList;
476 AllNodes.pop_front();
480 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
481 if (Op.getValueType() == VT) return Op;
482 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
483 return getNode(ISD::AND, Op.getValueType(), Op,
484 getConstant(Imm, Op.getValueType()));
487 SDOperand SelectionDAG::getString(const std::string &Val) {
488 StringSDNode *&N = StringNodes[Val];
490 N = new StringSDNode(Val);
491 AllNodes.push_back(N);
493 return SDOperand(N, 0);
496 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
497 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
498 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
500 // Mask out any bits that are not valid for this constant.
501 Val &= MVT::getIntVTBitMask(VT);
503 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
504 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
507 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
508 return SDOperand(E, 0);
509 SDNode *N = new ConstantSDNode(isT, Val, VT);
510 CSEMap.InsertNode(N, IP);
511 AllNodes.push_back(N);
512 return SDOperand(N, 0);
516 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
518 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
520 Val = (float)Val; // Mask out extra precision.
522 // Do the map lookup using the actual bit pattern for the floating point
523 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
524 // we don't have issues with SNANs.
525 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
526 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
527 ID.AddInteger(DoubleToBits(Val));
529 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
530 return SDOperand(E, 0);
531 SDNode *N = new ConstantFPSDNode(isTarget, Val, VT);
532 CSEMap.InsertNode(N, IP);
533 AllNodes.push_back(N);
534 return SDOperand(N, 0);
537 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
538 MVT::ValueType VT, int Offset,
540 unsigned Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
541 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
543 ID.AddInteger(Offset);
545 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
546 return SDOperand(E, 0);
547 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
548 CSEMap.InsertNode(N, IP);
549 AllNodes.push_back(N);
550 return SDOperand(N, 0);
553 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
555 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
556 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
559 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
560 return SDOperand(E, 0);
561 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
562 CSEMap.InsertNode(N, IP);
563 AllNodes.push_back(N);
564 return SDOperand(N, 0);
567 SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
568 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
569 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
572 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
573 return SDOperand(E, 0);
574 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
575 CSEMap.InsertNode(N, IP);
576 AllNodes.push_back(N);
577 return SDOperand(N, 0);
580 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
581 unsigned Alignment, int Offset,
583 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
584 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
585 ID.AddInteger(Alignment);
586 ID.AddInteger(Offset);
589 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
590 return SDOperand(E, 0);
591 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
592 CSEMap.InsertNode(N, IP);
593 AllNodes.push_back(N);
594 return SDOperand(N, 0);
598 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
599 SelectionDAGCSEMap::NodeID ID(ISD::BasicBlock, getNodeValueTypes(MVT::Other));
602 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
603 return SDOperand(E, 0);
604 SDNode *N = new BasicBlockSDNode(MBB);
605 CSEMap.InsertNode(N, IP);
606 AllNodes.push_back(N);
607 return SDOperand(N, 0);
610 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
611 if ((unsigned)VT >= ValueTypeNodes.size())
612 ValueTypeNodes.resize(VT+1);
613 if (ValueTypeNodes[VT] == 0) {
614 ValueTypeNodes[VT] = new VTSDNode(VT);
615 AllNodes.push_back(ValueTypeNodes[VT]);
618 return SDOperand(ValueTypeNodes[VT], 0);
621 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
622 SDNode *&N = ExternalSymbols[Sym];
623 if (N) return SDOperand(N, 0);
624 N = new ExternalSymbolSDNode(false, Sym, VT);
625 AllNodes.push_back(N);
626 return SDOperand(N, 0);
629 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
631 SDNode *&N = TargetExternalSymbols[Sym];
632 if (N) return SDOperand(N, 0);
633 N = new ExternalSymbolSDNode(true, Sym, VT);
634 AllNodes.push_back(N);
635 return SDOperand(N, 0);
638 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
639 if ((unsigned)Cond >= CondCodeNodes.size())
640 CondCodeNodes.resize(Cond+1);
642 if (CondCodeNodes[Cond] == 0) {
643 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
644 AllNodes.push_back(CondCodeNodes[Cond]);
646 return SDOperand(CondCodeNodes[Cond], 0);
649 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
650 SelectionDAGCSEMap::NodeID ID(ISD::Register, getNodeValueTypes(VT));
651 ID.AddInteger(RegNo);
653 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
654 return SDOperand(E, 0);
655 SDNode *N = new RegisterSDNode(RegNo, VT);
656 CSEMap.InsertNode(N, IP);
657 AllNodes.push_back(N);
658 return SDOperand(N, 0);
661 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
662 assert((!V || isa<PointerType>(V->getType())) &&
663 "SrcValue is not a pointer?");
665 SelectionDAGCSEMap::NodeID ID(ISD::SRCVALUE, getNodeValueTypes(MVT::Other));
667 ID.AddInteger(Offset);
669 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
670 return SDOperand(E, 0);
671 SDNode *N = new SrcValueSDNode(V, Offset);
672 CSEMap.InsertNode(N, IP);
673 AllNodes.push_back(N);
674 return SDOperand(N, 0);
677 SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
678 SDOperand N2, ISD::CondCode Cond) {
679 // These setcc operations always fold.
683 case ISD::SETFALSE2: return getConstant(0, VT);
685 case ISD::SETTRUE2: return getConstant(1, VT);
697 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
701 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
702 uint64_t C2 = N2C->getValue();
703 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
704 uint64_t C1 = N1C->getValue();
706 // Sign extend the operands if required
707 if (ISD::isSignedIntSetCC(Cond)) {
708 C1 = N1C->getSignExtended();
709 C2 = N2C->getSignExtended();
713 default: assert(0 && "Unknown integer setcc!");
714 case ISD::SETEQ: return getConstant(C1 == C2, VT);
715 case ISD::SETNE: return getConstant(C1 != C2, VT);
716 case ISD::SETULT: return getConstant(C1 < C2, VT);
717 case ISD::SETUGT: return getConstant(C1 > C2, VT);
718 case ISD::SETULE: return getConstant(C1 <= C2, VT);
719 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
720 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
721 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
722 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
723 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
726 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
727 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
728 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
730 // If the comparison constant has bits in the upper part, the
731 // zero-extended value could never match.
732 if (C2 & (~0ULL << InSize)) {
733 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
737 case ISD::SETEQ: return getConstant(0, VT);
740 case ISD::SETNE: return getConstant(1, VT);
743 // True if the sign bit of C2 is set.
744 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
747 // True if the sign bit of C2 isn't set.
748 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
754 // Otherwise, we can perform the comparison with the low bits.
762 return getSetCC(VT, N1.getOperand(0),
763 getConstant(C2, N1.getOperand(0).getValueType()),
766 break; // todo, be more careful with signed comparisons
768 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
769 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
770 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
771 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
772 MVT::ValueType ExtDstTy = N1.getValueType();
773 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
775 // If the extended part has any inconsistent bits, it cannot ever
776 // compare equal. In other words, they have to be all ones or all
779 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
780 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
781 return getConstant(Cond == ISD::SETNE, VT);
783 // Otherwise, make this a use of a zext.
784 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
785 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
789 uint64_t MinVal, MaxVal;
790 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
791 if (ISD::isSignedIntSetCC(Cond)) {
792 MinVal = 1ULL << (OperandBitSize-1);
793 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
794 MaxVal = ~0ULL >> (65-OperandBitSize);
799 MaxVal = ~0ULL >> (64-OperandBitSize);
802 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
803 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
804 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
805 --C2; // X >= C1 --> X > (C1-1)
806 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
807 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
810 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
811 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
812 ++C2; // X <= C1 --> X < (C1+1)
813 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
814 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
817 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
818 return getConstant(0, VT); // X < MIN --> false
820 // Canonicalize setgt X, Min --> setne X, Min
821 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
822 return getSetCC(VT, N1, N2, ISD::SETNE);
824 // If we have setult X, 1, turn it into seteq X, 0
825 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
826 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
828 // If we have setugt X, Max-1, turn it into seteq X, Max
829 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
830 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
833 // If we have "setcc X, C1", check to see if we can shrink the immediate
836 // SETUGT X, SINTMAX -> SETLT X, 0
837 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
838 C2 == (~0ULL >> (65-OperandBitSize)))
839 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
841 // FIXME: Implement the rest of these.
844 // Fold bit comparisons when we can.
845 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
846 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
847 if (ConstantSDNode *AndRHS =
848 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
849 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
850 // Perform the xform if the AND RHS is a single bit.
851 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
852 return getNode(ISD::SRL, VT, N1,
853 getConstant(Log2_64(AndRHS->getValue()),
854 TLI.getShiftAmountTy()));
856 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
857 // (X & 8) == 8 --> (X & 8) >> 3
858 // Perform the xform if C2 is a single bit.
859 if ((C2 & (C2-1)) == 0) {
860 return getNode(ISD::SRL, VT, N1,
861 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
866 } else if (isa<ConstantSDNode>(N1.Val)) {
867 // Ensure that the constant occurs on the RHS.
868 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
871 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
872 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
873 double C1 = N1C->getValue(), C2 = N2C->getValue();
876 default: break; // FIXME: Implement the rest of these!
877 case ISD::SETEQ: return getConstant(C1 == C2, VT);
878 case ISD::SETNE: return getConstant(C1 != C2, VT);
879 case ISD::SETLT: return getConstant(C1 < C2, VT);
880 case ISD::SETGT: return getConstant(C1 > C2, VT);
881 case ISD::SETLE: return getConstant(C1 <= C2, VT);
882 case ISD::SETGE: return getConstant(C1 >= C2, VT);
885 // Ensure that the constant occurs on the RHS.
886 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
889 // Could not fold it.
893 /// getNode - Gets or creates the specified node.
895 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
896 MVT::ValueType *VTs = getNodeValueTypes(VT);
897 SelectionDAGCSEMap::NodeID ID(Opcode, VTs);
899 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
900 return SDOperand(E, 0);
901 SDNode *N = new SDNode(Opcode, VT);
902 CSEMap.InsertNode(N, IP);
904 AllNodes.push_back(N);
905 return SDOperand(N, 0);
908 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
911 // Constant fold unary operations with an integer constant operand.
912 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
913 uint64_t Val = C->getValue();
916 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
917 case ISD::ANY_EXTEND:
918 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
919 case ISD::TRUNCATE: return getConstant(Val, VT);
920 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
921 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
922 case ISD::BIT_CONVERT:
923 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
924 return getConstantFP(BitsToFloat(Val), VT);
925 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
926 return getConstantFP(BitsToDouble(Val), VT);
930 default: assert(0 && "Invalid bswap!"); break;
931 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
932 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
933 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
938 default: assert(0 && "Invalid ctpop!"); break;
939 case MVT::i1: return getConstant(Val != 0, VT);
941 Tmp1 = (unsigned)Val & 0xFF;
942 return getConstant(CountPopulation_32(Tmp1), VT);
944 Tmp1 = (unsigned)Val & 0xFFFF;
945 return getConstant(CountPopulation_32(Tmp1), VT);
947 return getConstant(CountPopulation_32((unsigned)Val), VT);
949 return getConstant(CountPopulation_64(Val), VT);
953 default: assert(0 && "Invalid ctlz!"); break;
954 case MVT::i1: return getConstant(Val == 0, VT);
956 Tmp1 = (unsigned)Val & 0xFF;
957 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
959 Tmp1 = (unsigned)Val & 0xFFFF;
960 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
962 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
964 return getConstant(CountLeadingZeros_64(Val), VT);
968 default: assert(0 && "Invalid cttz!"); break;
969 case MVT::i1: return getConstant(Val == 0, VT);
971 Tmp1 = (unsigned)Val | 0x100;
972 return getConstant(CountTrailingZeros_32(Tmp1), VT);
974 Tmp1 = (unsigned)Val | 0x10000;
975 return getConstant(CountTrailingZeros_32(Tmp1), VT);
977 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
979 return getConstant(CountTrailingZeros_64(Val), VT);
984 // Constant fold unary operations with an floating point constant operand.
985 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
988 return getConstantFP(-C->getValue(), VT);
990 return getConstantFP(fabs(C->getValue()), VT);
993 return getConstantFP(C->getValue(), VT);
994 case ISD::FP_TO_SINT:
995 return getConstant((int64_t)C->getValue(), VT);
996 case ISD::FP_TO_UINT:
997 return getConstant((uint64_t)C->getValue(), VT);
998 case ISD::BIT_CONVERT:
999 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1000 return getConstant(FloatToBits(C->getValue()), VT);
1001 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1002 return getConstant(DoubleToBits(C->getValue()), VT);
1006 unsigned OpOpcode = Operand.Val->getOpcode();
1008 case ISD::TokenFactor:
1009 return Operand; // Factor of one node? No factor.
1010 case ISD::SIGN_EXTEND:
1011 if (Operand.getValueType() == VT) return Operand; // noop extension
1012 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
1013 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1014 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1016 case ISD::ZERO_EXTEND:
1017 if (Operand.getValueType() == VT) return Operand; // noop extension
1018 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
1019 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1020 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1022 case ISD::ANY_EXTEND:
1023 if (Operand.getValueType() == VT) return Operand; // noop extension
1024 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
1025 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1026 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1027 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1030 if (Operand.getValueType() == VT) return Operand; // noop truncate
1031 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
1032 if (OpOpcode == ISD::TRUNCATE)
1033 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1034 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1035 OpOpcode == ISD::ANY_EXTEND) {
1036 // If the source is smaller than the dest, we still need an extend.
1037 if (Operand.Val->getOperand(0).getValueType() < VT)
1038 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1039 else if (Operand.Val->getOperand(0).getValueType() > VT)
1040 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1042 return Operand.Val->getOperand(0);
1045 case ISD::BIT_CONVERT:
1046 // Basic sanity checking.
1047 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
1048 && "Cannot BIT_CONVERT between two different types!");
1049 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1050 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1051 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1052 if (OpOpcode == ISD::UNDEF)
1053 return getNode(ISD::UNDEF, VT);
1055 case ISD::SCALAR_TO_VECTOR:
1056 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1057 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1058 "Illegal SCALAR_TO_VECTOR node!");
1061 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1062 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1063 Operand.Val->getOperand(0));
1064 if (OpOpcode == ISD::FNEG) // --X -> X
1065 return Operand.Val->getOperand(0);
1068 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1069 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1074 MVT::ValueType *VTs = getNodeValueTypes(VT);
1075 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1076 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Operand);
1078 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1079 return SDOperand(E, 0);
1080 N = new SDNode(Opcode, Operand);
1081 N->setValueTypes(VTs, 1);
1082 CSEMap.InsertNode(N, IP);
1084 N = new SDNode(Opcode, Operand);
1085 N->setValueTypes(VTs, 1);
1087 AllNodes.push_back(N);
1088 return SDOperand(N, 0);
1093 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1094 SDOperand N1, SDOperand N2) {
1097 case ISD::TokenFactor:
1098 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1099 N2.getValueType() == MVT::Other && "Invalid token factor!");
1108 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1115 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1122 assert(N1.getValueType() == N2.getValueType() &&
1123 N1.getValueType() == VT && "Binary operator types must match!");
1125 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1126 assert(N1.getValueType() == VT &&
1127 MVT::isFloatingPoint(N1.getValueType()) &&
1128 MVT::isFloatingPoint(N2.getValueType()) &&
1129 "Invalid FCOPYSIGN!");
1136 assert(VT == N1.getValueType() &&
1137 "Shift operators return type must be the same as their first arg");
1138 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1139 VT != MVT::i1 && "Shifts only work on integers");
1141 case ISD::FP_ROUND_INREG: {
1142 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1143 assert(VT == N1.getValueType() && "Not an inreg round!");
1144 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1145 "Cannot FP_ROUND_INREG integer types");
1146 assert(EVT <= VT && "Not rounding down!");
1149 case ISD::AssertSext:
1150 case ISD::AssertZext:
1151 case ISD::SIGN_EXTEND_INREG: {
1152 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1153 assert(VT == N1.getValueType() && "Not an inreg extend!");
1154 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1155 "Cannot *_EXTEND_INREG FP types");
1156 assert(EVT <= VT && "Not extending!");
1163 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1164 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1166 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1167 int64_t Val = N1C->getValue();
1168 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1169 Val <<= 64-FromBits;
1170 Val >>= 64-FromBits;
1171 return getConstant(Val, VT);
1175 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1177 case ISD::ADD: return getConstant(C1 + C2, VT);
1178 case ISD::SUB: return getConstant(C1 - C2, VT);
1179 case ISD::MUL: return getConstant(C1 * C2, VT);
1181 if (C2) return getConstant(C1 / C2, VT);
1184 if (C2) return getConstant(C1 % C2, VT);
1187 if (C2) return getConstant(N1C->getSignExtended() /
1188 N2C->getSignExtended(), VT);
1191 if (C2) return getConstant(N1C->getSignExtended() %
1192 N2C->getSignExtended(), VT);
1194 case ISD::AND : return getConstant(C1 & C2, VT);
1195 case ISD::OR : return getConstant(C1 | C2, VT);
1196 case ISD::XOR : return getConstant(C1 ^ C2, VT);
1197 case ISD::SHL : return getConstant(C1 << C2, VT);
1198 case ISD::SRL : return getConstant(C1 >> C2, VT);
1199 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
1201 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1204 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1208 } else { // Cannonicalize constant to RHS if commutative
1209 if (isCommutativeBinOp(Opcode)) {
1210 std::swap(N1C, N2C);
1216 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1217 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1220 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1222 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1223 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1224 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1226 if (C2) return getConstantFP(C1 / C2, VT);
1229 if (C2) return getConstantFP(fmod(C1, C2), VT);
1231 case ISD::FCOPYSIGN: {
1242 if (u2.I < 0) // Sign bit of RHS set?
1243 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1245 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1246 return getConstantFP(u1.F, VT);
1250 } else { // Cannonicalize constant to RHS if commutative
1251 if (isCommutativeBinOp(Opcode)) {
1252 std::swap(N1CFP, N2CFP);
1258 // Canonicalize an UNDEF to the RHS, even over a constant.
1259 if (N1.getOpcode() == ISD::UNDEF) {
1260 if (isCommutativeBinOp(Opcode)) {
1264 case ISD::FP_ROUND_INREG:
1265 case ISD::SIGN_EXTEND_INREG:
1271 return N1; // fold op(undef, arg2) -> undef
1278 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1283 // Fold a bunch of operators when the RHS is undef.
1284 if (N2.getOpcode() == ISD::UNDEF) {
1298 return N2; // fold op(arg1, undef) -> undef
1303 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1305 return getConstant(MVT::getIntVTBitMask(VT), VT);
1311 // Finally, fold operations that do not require constants.
1313 case ISD::FP_ROUND_INREG:
1314 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1316 case ISD::SIGN_EXTEND_INREG: {
1317 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1318 if (EVT == VT) return N1; // Not actually extending
1322 // FIXME: figure out how to safely handle things like
1323 // int foo(int x) { return 1 << (x & 255); }
1324 // int bar() { return foo(256); }
1329 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1330 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
1331 return getNode(Opcode, VT, N1, N2.getOperand(0));
1332 else if (N2.getOpcode() == ISD::AND)
1333 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1334 // If the and is only masking out bits that cannot effect the shift,
1335 // eliminate the and.
1336 unsigned NumBits = MVT::getSizeInBits(VT);
1337 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1338 return getNode(Opcode, VT, N1, N2.getOperand(0));
1344 // Memoize this node if possible.
1346 MVT::ValueType *VTs = getNodeValueTypes(VT);
1347 if (VT != MVT::Flag) {
1348 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2);
1350 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1351 return SDOperand(E, 0);
1352 N = new SDNode(Opcode, N1, N2);
1353 N->setValueTypes(VTs, 1);
1354 CSEMap.InsertNode(N, IP);
1356 N = new SDNode(Opcode, N1, N2);
1357 N->setValueTypes(VTs, 1);
1360 AllNodes.push_back(N);
1361 return SDOperand(N, 0);
1364 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1365 SDOperand N1, SDOperand N2, SDOperand N3) {
1366 // Perform various simplifications.
1367 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1368 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1369 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1372 // Use SimplifySetCC to simplify SETCC's.
1373 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
1374 if (Simp.Val) return Simp;
1379 if (N1C->getValue())
1380 return N2; // select true, X, Y -> X
1382 return N3; // select false, X, Y -> Y
1384 if (N2 == N3) return N2; // select C, X, X -> X
1388 if (N2C->getValue()) // Unconditional branch
1389 return getNode(ISD::BR, MVT::Other, N1, N3);
1391 return N1; // Never-taken branch
1393 case ISD::VECTOR_SHUFFLE:
1394 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1395 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1396 N3.getOpcode() == ISD::BUILD_VECTOR &&
1397 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1398 "Illegal VECTOR_SHUFFLE node!");
1402 // Memoize node if it doesn't produce a flag.
1404 MVT::ValueType *VTs = getNodeValueTypes(VT);
1406 if (VT != MVT::Flag) {
1407 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2, N3);
1409 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1410 return SDOperand(E, 0);
1411 N = new SDNode(Opcode, N1, N2, N3);
1412 N->setValueTypes(VTs, 1);
1413 CSEMap.InsertNode(N, IP);
1415 N = new SDNode(Opcode, N1, N2, N3);
1416 N->setValueTypes(VTs, 1);
1418 AllNodes.push_back(N);
1419 return SDOperand(N, 0);
1422 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1423 SDOperand N1, SDOperand N2, SDOperand N3,
1425 SDOperand Ops[] = { N1, N2, N3, N4 };
1426 return getNode(Opcode, VT, Ops, 4);
1429 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1430 SDOperand N1, SDOperand N2, SDOperand N3,
1431 SDOperand N4, SDOperand N5) {
1432 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1433 return getNode(Opcode, VT, Ops, 5);
1436 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1437 SDOperand Chain, SDOperand Ptr,
1439 MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other);
1441 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, SV);
1443 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1444 return SDOperand(E, 0);
1445 SDNode *N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1446 N->setValueTypes(VTs, 2);
1447 CSEMap.InsertNode(N, IP);
1448 AllNodes.push_back(N);
1449 return SDOperand(N, 0);
1452 SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1453 SDOperand Chain, SDOperand Ptr,
1455 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1456 getValueType(EVT) };
1457 std::vector<MVT::ValueType> VTs;
1459 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1460 return getNode(ISD::VLOAD, VTs, Ops, 5);
1463 SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1464 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1465 MVT::ValueType EVT) {
1466 SDOperand Ops[] = { Chain, Ptr, SV, getValueType(EVT) };
1467 std::vector<MVT::ValueType> VTs;
1469 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1470 return getNode(Opcode, VTs, Ops, 4);
1473 SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1474 SDOperand Chain, SDOperand Ptr,
1476 SDOperand Ops[] = { Chain, Ptr, SV };
1477 std::vector<MVT::ValueType> VTs;
1479 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1480 return getNode(ISD::VAARG, VTs, Ops, 3);
1483 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1484 const SDOperand *Ops, unsigned NumOps) {
1486 case 0: return getNode(Opcode, VT);
1487 case 1: return getNode(Opcode, VT, Ops[0]);
1488 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1489 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
1495 case ISD::TRUNCSTORE: {
1496 assert(NumOps == 5 && "TRUNCSTORE takes 5 operands!");
1497 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1498 #if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1499 // If this is a truncating store of a constant, convert to the desired type
1500 // and store it instead.
1501 if (isa<Constant>(Ops[0])) {
1502 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1503 if (isa<Constant>(Op))
1506 // Also for ConstantFP?
1508 if (Ops[0].getValueType() == EVT) // Normal store?
1509 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1510 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1511 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1512 "Can't do FP-INT conversion!");
1515 case ISD::SELECT_CC: {
1516 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
1517 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1518 "LHS and RHS of condition must have same type!");
1519 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1520 "True and False arms of SelectCC must have same type!");
1521 assert(Ops[2].getValueType() == VT &&
1522 "select_cc node must be of same type as true and false value!");
1526 assert(NumOps == 5 && "BR_CC takes 5 operands!");
1527 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1528 "LHS/RHS of comparison should match types!");
1535 MVT::ValueType *VTs = getNodeValueTypes(VT);
1536 if (VT != MVT::Flag) {
1537 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Ops, NumOps);
1539 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1540 return SDOperand(E, 0);
1541 N = new SDNode(Opcode, Ops, NumOps);
1542 N->setValueTypes(VTs, 1);
1543 CSEMap.InsertNode(N, IP);
1545 N = new SDNode(Opcode, Ops, NumOps);
1546 N->setValueTypes(VTs, 1);
1548 AllNodes.push_back(N);
1549 return SDOperand(N, 0);
1552 SDOperand SelectionDAG::getNode(unsigned Opcode,
1553 std::vector<MVT::ValueType> &ResultTys,
1554 const SDOperand *Ops, unsigned NumOps) {
1555 if (ResultTys.size() == 1)
1556 return getNode(Opcode, ResultTys[0], Ops, NumOps);
1561 case ISD::ZEXTLOAD: {
1562 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1563 assert(NumOps == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1564 // If they are asking for an extending load from/to the same thing, return a
1566 if (ResultTys[0] == EVT)
1567 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1568 if (MVT::isVector(ResultTys[0])) {
1569 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1570 "Invalid vector extload!");
1572 assert(EVT < ResultTys[0] &&
1573 "Should only be an extending load, not truncating!");
1575 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1576 "Cannot sign/zero extend a FP/Vector load!");
1577 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1578 "Cannot convert from FP to Int or Int -> FP!");
1582 // FIXME: figure out how to safely handle things like
1583 // int foo(int x) { return 1 << (x & 255); }
1584 // int bar() { return foo(256); }
1586 case ISD::SRA_PARTS:
1587 case ISD::SRL_PARTS:
1588 case ISD::SHL_PARTS:
1589 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1590 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
1591 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1592 else if (N3.getOpcode() == ISD::AND)
1593 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1594 // If the and is only masking out bits that cannot effect the shift,
1595 // eliminate the and.
1596 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1597 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1598 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1604 // Memoize the node unless it returns a flag.
1606 MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
1607 if (ResultTys.back() != MVT::Flag) {
1608 SelectionDAGCSEMap::NodeID ID;
1609 ID.SetOpcode(Opcode);
1610 ID.SetValueTypes(VTs);
1611 ID.SetOperands(&Ops[0], NumOps);
1613 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1614 return SDOperand(E, 0);
1615 N = new SDNode(Opcode, Ops, NumOps);
1616 N->setValueTypes(VTs, ResultTys.size());
1617 CSEMap.InsertNode(N, IP);
1619 N = new SDNode(Opcode, Ops, NumOps);
1620 N->setValueTypes(VTs, ResultTys.size());
1622 AllNodes.push_back(N);
1623 return SDOperand(N, 0);
1627 MVT::ValueType *SelectionDAG::getNodeValueTypes(MVT::ValueType VT) {
1628 return SDNode::getValueTypeList(VT);
1631 MVT::ValueType *SelectionDAG::getNodeValueTypes(
1632 std::vector<MVT::ValueType> &RetVals) {
1633 switch (RetVals.size()) {
1634 case 0: assert(0 && "Cannot have nodes without results!");
1635 case 1: return SDNode::getValueTypeList(RetVals[0]);
1636 case 2: return getNodeValueTypes(RetVals[0], RetVals[1]);
1640 std::list<std::vector<MVT::ValueType> >::iterator I =
1641 std::find(VTList.begin(), VTList.end(), RetVals);
1642 if (I == VTList.end()) {
1643 VTList.push_front(RetVals);
1650 MVT::ValueType *SelectionDAG::getNodeValueTypes(MVT::ValueType VT1,
1651 MVT::ValueType VT2) {
1652 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1653 E = VTList.end(); I != E; ++I) {
1654 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
1657 std::vector<MVT::ValueType> V;
1660 VTList.push_front(V);
1661 return &(*VTList.begin())[0];
1664 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1665 /// specified operands. If the resultant node already exists in the DAG,
1666 /// this does not modify the specified node, instead it returns the node that
1667 /// already exists. If the resultant node does not exist in the DAG, the
1668 /// input node is returned. As a degenerate case, if you specify the same
1669 /// input operands as the node already has, the input node is returned.
1670 SDOperand SelectionDAG::
1671 UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1672 SDNode *N = InN.Val;
1673 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1675 // Check to see if there is no change.
1676 if (Op == N->getOperand(0)) return InN;
1678 // See if the modified node already exists.
1679 void *InsertPos = 0;
1680 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1681 return SDOperand(Existing, InN.ResNo);
1683 // Nope it doesn't. Remove the node from it's current place in the maps.
1685 RemoveNodeFromCSEMaps(N);
1687 // Now we update the operands.
1688 N->OperandList[0].Val->removeUser(N);
1690 N->OperandList[0] = Op;
1692 // If this gets put into a CSE map, add it.
1693 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1697 SDOperand SelectionDAG::
1698 UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1699 SDNode *N = InN.Val;
1700 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1702 // Check to see if there is no change.
1703 bool AnyChange = false;
1704 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1705 return InN; // No operands changed, just return the input node.
1707 // See if the modified node already exists.
1708 void *InsertPos = 0;
1709 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1710 return SDOperand(Existing, InN.ResNo);
1712 // Nope it doesn't. Remove the node from it's current place in the maps.
1714 RemoveNodeFromCSEMaps(N);
1716 // Now we update the operands.
1717 if (N->OperandList[0] != Op1) {
1718 N->OperandList[0].Val->removeUser(N);
1719 Op1.Val->addUser(N);
1720 N->OperandList[0] = Op1;
1722 if (N->OperandList[1] != Op2) {
1723 N->OperandList[1].Val->removeUser(N);
1724 Op2.Val->addUser(N);
1725 N->OperandList[1] = Op2;
1728 // If this gets put into a CSE map, add it.
1729 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1733 SDOperand SelectionDAG::
1734 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1735 SDOperand Ops[] = { Op1, Op2, Op3 };
1736 return UpdateNodeOperands(N, Ops, 3);
1739 SDOperand SelectionDAG::
1740 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1741 SDOperand Op3, SDOperand Op4) {
1742 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
1743 return UpdateNodeOperands(N, Ops, 4);
1746 SDOperand SelectionDAG::
1747 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1748 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1749 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
1750 return UpdateNodeOperands(N, Ops, 5);
1754 SDOperand SelectionDAG::
1755 UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
1756 SDNode *N = InN.Val;
1757 assert(N->getNumOperands() == NumOps &&
1758 "Update with wrong number of operands");
1760 // Check to see if there is no change.
1761 bool AnyChange = false;
1762 for (unsigned i = 0; i != NumOps; ++i) {
1763 if (Ops[i] != N->getOperand(i)) {
1769 // No operands changed, just return the input node.
1770 if (!AnyChange) return InN;
1772 // See if the modified node already exists.
1773 void *InsertPos = 0;
1774 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
1775 return SDOperand(Existing, InN.ResNo);
1777 // Nope it doesn't. Remove the node from it's current place in the maps.
1779 RemoveNodeFromCSEMaps(N);
1781 // Now we update the operands.
1782 for (unsigned i = 0; i != NumOps; ++i) {
1783 if (N->OperandList[i] != Ops[i]) {
1784 N->OperandList[i].Val->removeUser(N);
1785 Ops[i].Val->addUser(N);
1786 N->OperandList[i] = Ops[i];
1790 // If this gets put into a CSE map, add it.
1791 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1798 /// SelectNodeTo - These are used for target selectors to *mutate* the
1799 /// specified node to have the specified return type, Target opcode, and
1800 /// operands. Note that target opcodes are stored as
1801 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1803 /// Note that SelectNodeTo returns the resultant node. If there is already a
1804 /// node of the specified opcode and operands, it returns that node instead of
1805 /// the current one.
1806 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1807 MVT::ValueType VT) {
1808 MVT::ValueType *VTs = getNodeValueTypes(VT);
1809 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1811 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1812 return SDOperand(ON, 0);
1814 RemoveNodeFromCSEMaps(N);
1816 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1817 N->setValueTypes(getNodeValueTypes(VT), 1);
1819 CSEMap.InsertNode(N, IP);
1820 return SDOperand(N, 0);
1823 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1824 MVT::ValueType VT, SDOperand Op1) {
1825 // If an identical node already exists, use it.
1826 MVT::ValueType *VTs = getNodeValueTypes(VT);
1827 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
1829 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1830 return SDOperand(ON, 0);
1832 RemoveNodeFromCSEMaps(N);
1833 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1834 N->setValueTypes(getNodeValueTypes(VT), 1);
1835 N->setOperands(Op1);
1836 CSEMap.InsertNode(N, IP);
1837 return SDOperand(N, 0);
1840 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1841 MVT::ValueType VT, SDOperand Op1,
1843 // If an identical node already exists, use it.
1844 MVT::ValueType *VTs = getNodeValueTypes(VT);
1845 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1847 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1848 return SDOperand(ON, 0);
1850 RemoveNodeFromCSEMaps(N);
1851 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1852 N->setValueTypes(VTs, 1);
1853 N->setOperands(Op1, Op2);
1855 CSEMap.InsertNode(N, IP); // Memoize the new node.
1856 return SDOperand(N, 0);
1859 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1860 MVT::ValueType VT, SDOperand Op1,
1861 SDOperand Op2, SDOperand Op3) {
1862 // If an identical node already exists, use it.
1863 MVT::ValueType *VTs = getNodeValueTypes(VT);
1864 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2, Op3);
1866 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1867 return SDOperand(ON, 0);
1869 RemoveNodeFromCSEMaps(N);
1870 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1871 N->setValueTypes(VTs, 1);
1872 N->setOperands(Op1, Op2, Op3);
1874 CSEMap.InsertNode(N, IP); // Memoize the new node.
1875 return SDOperand(N, 0);
1878 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1879 MVT::ValueType VT, SDOperand Op1,
1880 SDOperand Op2, SDOperand Op3,
1882 // If an identical node already exists, use it.
1883 MVT::ValueType *VTs = getNodeValueTypes(VT);
1884 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1890 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1891 return SDOperand(ON, 0);
1893 RemoveNodeFromCSEMaps(N);
1894 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1895 N->setValueTypes(VTs, 1);
1896 N->setOperands(Op1, Op2, Op3, Op4);
1898 CSEMap.InsertNode(N, IP); // Memoize the new node.
1899 return SDOperand(N, 0);
1902 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1903 MVT::ValueType VT, SDOperand Op1,
1904 SDOperand Op2, SDOperand Op3,
1905 SDOperand Op4, SDOperand Op5) {
1906 MVT::ValueType *VTs = getNodeValueTypes(VT);
1907 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1914 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1915 return SDOperand(ON, 0);
1917 RemoveNodeFromCSEMaps(N);
1918 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1919 N->setValueTypes(VTs, 1);
1920 N->setOperands(Op1, Op2, Op3, Op4, Op5);
1922 CSEMap.InsertNode(N, IP); // Memoize the new node.
1923 return SDOperand(N, 0);
1926 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1927 MVT::ValueType VT, SDOperand Op1,
1928 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1929 SDOperand Op5, SDOperand Op6) {
1930 MVT::ValueType *VTs = getNodeValueTypes(VT);
1931 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1939 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1940 return SDOperand(ON, 0);
1942 RemoveNodeFromCSEMaps(N);
1943 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1944 N->setValueTypes(VTs, 1);
1945 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
1947 CSEMap.InsertNode(N, IP); // Memoize the new node.
1948 return SDOperand(N, 0);
1951 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1952 MVT::ValueType VT, SDOperand Op1,
1953 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1954 SDOperand Op5, SDOperand Op6,
1956 MVT::ValueType *VTs = getNodeValueTypes(VT);
1957 // If an identical node already exists, use it.
1958 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1967 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1968 return SDOperand(ON, 0);
1970 RemoveNodeFromCSEMaps(N);
1971 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1972 N->setValueTypes(VTs, 1);
1973 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
1975 CSEMap.InsertNode(N, IP); // Memoize the new node.
1976 return SDOperand(N, 0);
1978 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1979 MVT::ValueType VT, SDOperand Op1,
1980 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1981 SDOperand Op5, SDOperand Op6,
1982 SDOperand Op7, SDOperand Op8) {
1983 // If an identical node already exists, use it.
1984 MVT::ValueType *VTs = getNodeValueTypes(VT);
1985 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1995 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1996 return SDOperand(ON, 0);
1998 RemoveNodeFromCSEMaps(N);
1999 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2000 N->setValueTypes(VTs, 1);
2001 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2003 CSEMap.InsertNode(N, IP); // Memoize the new node.
2004 return SDOperand(N, 0);
2007 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2008 MVT::ValueType VT1, MVT::ValueType VT2,
2009 SDOperand Op1, SDOperand Op2) {
2010 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2011 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
2013 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2014 return SDOperand(ON, 0);
2016 RemoveNodeFromCSEMaps(N);
2017 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2018 N->setValueTypes(VTs, 2);
2019 N->setOperands(Op1, Op2);
2021 CSEMap.InsertNode(N, IP); // Memoize the new node.
2022 return SDOperand(N, 0);
2025 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2026 MVT::ValueType VT1, MVT::ValueType VT2,
2027 SDOperand Op1, SDOperand Op2,
2029 // If an identical node already exists, use it.
2030 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2031 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
2034 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2035 return SDOperand(ON, 0);
2037 RemoveNodeFromCSEMaps(N);
2038 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2039 N->setValueTypes(VTs, 2);
2040 N->setOperands(Op1, Op2, Op3);
2042 CSEMap.InsertNode(N, IP); // Memoize the new node.
2043 return SDOperand(N, 0);
2046 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2047 MVT::ValueType VT1, MVT::ValueType VT2,
2048 SDOperand Op1, SDOperand Op2,
2049 SDOperand Op3, SDOperand Op4) {
2050 // If an identical node already exists, use it.
2051 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2052 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2058 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2059 return SDOperand(ON, 0);
2061 RemoveNodeFromCSEMaps(N);
2062 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2063 N->setValueTypes(VTs, 2);
2064 N->setOperands(Op1, Op2, Op3, Op4);
2066 CSEMap.InsertNode(N, IP); // Memoize the new node.
2067 return SDOperand(N, 0);
2070 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2071 MVT::ValueType VT1, MVT::ValueType VT2,
2072 SDOperand Op1, SDOperand Op2,
2073 SDOperand Op3, SDOperand Op4,
2075 // If an identical node already exists, use it.
2076 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2077 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2084 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2085 return SDOperand(ON, 0);
2087 RemoveNodeFromCSEMaps(N);
2088 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2089 N->setValueTypes(VTs, 2);
2090 N->setOperands(Op1, Op2, Op3, Op4, Op5);
2092 CSEMap.InsertNode(N, IP); // Memoize the new node.
2093 return SDOperand(N, 0);
2096 /// getTargetNode - These are used for target selectors to create a new node
2097 /// with specified return type(s), target opcode, and operands.
2099 /// Note that getTargetNode returns the resultant node. If there is already a
2100 /// node of the specified opcode and operands, it returns that node instead of
2101 /// the current one.
2102 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2103 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2105 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2107 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2109 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2110 SDOperand Op1, SDOperand Op2) {
2111 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2113 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2114 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2115 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2117 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2118 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2120 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2122 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2123 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2124 SDOperand Op4, SDOperand Op5) {
2125 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2127 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2128 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2129 SDOperand Op4, SDOperand Op5,
2131 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
2132 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 6).Val;
2134 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2135 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2136 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2138 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
2139 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 7).Val;
2141 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2142 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2143 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2144 SDOperand Op7, SDOperand Op8) {
2145 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8 };
2146 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 8).Val;
2148 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2149 const SDOperand *Ops, unsigned NumOps) {
2150 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
2152 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2153 MVT::ValueType VT2, SDOperand Op1) {
2154 std::vector<MVT::ValueType> ResultTys;
2155 ResultTys.push_back(VT1);
2156 ResultTys.push_back(VT2);
2157 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, &Op1, 1).Val;
2159 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2160 MVT::ValueType VT2, SDOperand Op1,
2162 std::vector<MVT::ValueType> ResultTys;
2163 ResultTys.push_back(VT1);
2164 ResultTys.push_back(VT2);
2165 SDOperand Ops[] = { Op1, Op2 };
2166 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 2).Val;
2168 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2169 MVT::ValueType VT2, SDOperand Op1,
2170 SDOperand Op2, SDOperand Op3) {
2171 std::vector<MVT::ValueType> ResultTys;
2172 ResultTys.push_back(VT1);
2173 ResultTys.push_back(VT2);
2174 SDOperand Ops[] = { Op1, Op2, Op3 };
2175 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 3).Val;
2177 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2178 MVT::ValueType VT2, SDOperand Op1,
2179 SDOperand Op2, SDOperand Op3,
2181 std::vector<MVT::ValueType> ResultTys;
2182 ResultTys.push_back(VT1);
2183 ResultTys.push_back(VT2);
2184 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2185 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 4).Val;
2187 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2188 MVT::ValueType VT2, SDOperand Op1,
2189 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2191 std::vector<MVT::ValueType> ResultTys;
2192 ResultTys.push_back(VT1);
2193 ResultTys.push_back(VT2);
2194 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2195 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 5).Val;
2197 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2198 MVT::ValueType VT2, SDOperand Op1,
2199 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2200 SDOperand Op5, SDOperand Op6) {
2201 std::vector<MVT::ValueType> ResultTys;
2202 ResultTys.push_back(VT1);
2203 ResultTys.push_back(VT2);
2204 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
2205 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 6).Val;
2207 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2208 MVT::ValueType VT2, SDOperand Op1,
2209 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2210 SDOperand Op5, SDOperand Op6,
2212 std::vector<MVT::ValueType> ResultTys;
2213 ResultTys.push_back(VT1);
2214 ResultTys.push_back(VT2);
2215 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
2216 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 7).Val;
2218 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2219 MVT::ValueType VT2, MVT::ValueType VT3,
2220 SDOperand Op1, SDOperand Op2) {
2221 std::vector<MVT::ValueType> ResultTys;
2222 ResultTys.push_back(VT1);
2223 ResultTys.push_back(VT2);
2224 ResultTys.push_back(VT3);
2225 SDOperand Ops[] = { Op1, Op2 };
2226 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 2).Val;
2228 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2229 MVT::ValueType VT2, MVT::ValueType VT3,
2230 SDOperand Op1, SDOperand Op2,
2231 SDOperand Op3, SDOperand Op4,
2233 std::vector<MVT::ValueType> ResultTys;
2234 ResultTys.push_back(VT1);
2235 ResultTys.push_back(VT2);
2236 ResultTys.push_back(VT3);
2237 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2238 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 5).Val;
2240 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2241 MVT::ValueType VT2, MVT::ValueType VT3,
2242 SDOperand Op1, SDOperand Op2,
2243 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2245 std::vector<MVT::ValueType> ResultTys;
2246 ResultTys.push_back(VT1);
2247 ResultTys.push_back(VT2);
2248 ResultTys.push_back(VT3);
2249 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
2250 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 6).Val;
2252 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2253 MVT::ValueType VT2, MVT::ValueType VT3,
2254 SDOperand Op1, SDOperand Op2,
2255 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2256 SDOperand Op6, SDOperand Op7) {
2257 std::vector<MVT::ValueType> ResultTys;
2258 ResultTys.push_back(VT1);
2259 ResultTys.push_back(VT2);
2260 ResultTys.push_back(VT3);
2261 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
2262 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 7).Val;
2264 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2266 const SDOperand *Ops, unsigned NumOps) {
2267 std::vector<MVT::ValueType> ResultTys;
2268 ResultTys.push_back(VT1);
2269 ResultTys.push_back(VT2);
2270 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, NumOps).Val;
2273 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2274 /// This can cause recursive merging of nodes in the DAG.
2276 /// This version assumes From/To have a single result value.
2278 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2279 std::vector<SDNode*> *Deleted) {
2280 SDNode *From = FromN.Val, *To = ToN.Val;
2281 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2282 "Cannot replace with this method!");
2283 assert(From != To && "Cannot replace uses of with self");
2285 while (!From->use_empty()) {
2286 // Process users until they are all gone.
2287 SDNode *U = *From->use_begin();
2289 // This node is about to morph, remove its old self from the CSE maps.
2290 RemoveNodeFromCSEMaps(U);
2292 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2294 if (I->Val == From) {
2295 From->removeUser(U);
2300 // Now that we have modified U, add it back to the CSE maps. If it already
2301 // exists there, recursively merge the results together.
2302 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2303 ReplaceAllUsesWith(U, Existing, Deleted);
2305 if (Deleted) Deleted->push_back(U);
2306 DeleteNodeNotInCSEMaps(U);
2311 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2312 /// This can cause recursive merging of nodes in the DAG.
2314 /// This version assumes From/To have matching types and numbers of result
2317 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2318 std::vector<SDNode*> *Deleted) {
2319 assert(From != To && "Cannot replace uses of with self");
2320 assert(From->getNumValues() == To->getNumValues() &&
2321 "Cannot use this version of ReplaceAllUsesWith!");
2322 if (From->getNumValues() == 1) { // If possible, use the faster version.
2323 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
2327 while (!From->use_empty()) {
2328 // Process users until they are all gone.
2329 SDNode *U = *From->use_begin();
2331 // This node is about to morph, remove its old self from the CSE maps.
2332 RemoveNodeFromCSEMaps(U);
2334 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2336 if (I->Val == From) {
2337 From->removeUser(U);
2342 // Now that we have modified U, add it back to the CSE maps. If it already
2343 // exists there, recursively merge the results together.
2344 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2345 ReplaceAllUsesWith(U, Existing, Deleted);
2347 if (Deleted) Deleted->push_back(U);
2348 DeleteNodeNotInCSEMaps(U);
2353 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2354 /// This can cause recursive merging of nodes in the DAG.
2356 /// This version can replace From with any result values. To must match the
2357 /// number and types of values returned by From.
2358 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
2359 const SDOperand *To,
2360 std::vector<SDNode*> *Deleted) {
2361 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
2362 // Degenerate case handled above.
2363 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
2367 while (!From->use_empty()) {
2368 // Process users until they are all gone.
2369 SDNode *U = *From->use_begin();
2371 // This node is about to morph, remove its old self from the CSE maps.
2372 RemoveNodeFromCSEMaps(U);
2374 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2376 if (I->Val == From) {
2377 const SDOperand &ToOp = To[I->ResNo];
2378 From->removeUser(U);
2380 ToOp.Val->addUser(U);
2383 // Now that we have modified U, add it back to the CSE maps. If it already
2384 // exists there, recursively merge the results together.
2385 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2386 ReplaceAllUsesWith(U, Existing, Deleted);
2388 if (Deleted) Deleted->push_back(U);
2389 DeleteNodeNotInCSEMaps(U);
2394 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2395 /// uses of other values produced by From.Val alone. The Deleted vector is
2396 /// handled the same was as for ReplaceAllUsesWith.
2397 void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2398 std::vector<SDNode*> &Deleted) {
2399 assert(From != To && "Cannot replace a value with itself");
2400 // Handle the simple, trivial, case efficiently.
2401 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2402 ReplaceAllUsesWith(From, To, &Deleted);
2406 // Get all of the users in a nice, deterministically ordered, uniqued set.
2407 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2409 while (!Users.empty()) {
2410 // We know that this user uses some value of From. If it is the right
2411 // value, update it.
2412 SDNode *User = Users.back();
2415 for (SDOperand *Op = User->OperandList,
2416 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2418 // Okay, we know this user needs to be updated. Remove its old self
2419 // from the CSE maps.
2420 RemoveNodeFromCSEMaps(User);
2422 // Update all operands that match "From".
2423 for (; Op != E; ++Op) {
2425 From.Val->removeUser(User);
2427 To.Val->addUser(User);
2431 // Now that we have modified User, add it back to the CSE maps. If it
2432 // already exists there, recursively merge the results together.
2433 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2434 unsigned NumDeleted = Deleted.size();
2435 ReplaceAllUsesWith(User, Existing, &Deleted);
2437 // User is now dead.
2438 Deleted.push_back(User);
2439 DeleteNodeNotInCSEMaps(User);
2441 // We have to be careful here, because ReplaceAllUsesWith could have
2442 // deleted a user of From, which means there may be dangling pointers
2443 // in the "Users" setvector. Scan over the deleted node pointers and
2444 // remove them from the setvector.
2445 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2446 Users.remove(Deleted[i]);
2448 break; // Exit the operand scanning loop.
2455 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2456 /// their allnodes order. It returns the maximum id.
2457 unsigned SelectionDAG::AssignNodeIds() {
2459 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2466 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
2467 /// based on their topological order. It returns the maximum id and a vector
2468 /// of the SDNodes* in assigned order by reference.
2469 unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
2470 unsigned DAGSize = AllNodes.size();
2471 std::vector<unsigned> InDegree(DAGSize);
2472 std::vector<SDNode*> Sources;
2474 // Use a two pass approach to avoid using a std::map which is slow.
2476 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2479 unsigned Degree = N->use_size();
2480 InDegree[N->getNodeId()] = Degree;
2482 Sources.push_back(N);
2486 while (!Sources.empty()) {
2487 SDNode *N = Sources.back();
2489 TopOrder.push_back(N);
2490 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2492 unsigned Degree = --InDegree[P->getNodeId()];
2494 Sources.push_back(P);
2498 // Second pass, assign the actual topological order as node ids.
2500 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2502 (*TI)->setNodeId(Id++);
2509 //===----------------------------------------------------------------------===//
2511 //===----------------------------------------------------------------------===//
2513 // Out-of-line virtual method to give class a home.
2514 void SDNode::ANCHOR() {
2517 /// getValueTypeList - Return a pointer to the specified value type.
2519 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2520 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2525 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2526 /// indicated value. This method ignores uses of other values defined by this
2528 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
2529 assert(Value < getNumValues() && "Bad value!");
2531 // If there is only one value, this is easy.
2532 if (getNumValues() == 1)
2533 return use_size() == NUses;
2534 if (Uses.size() < NUses) return false;
2536 SDOperand TheValue(const_cast<SDNode *>(this), Value);
2538 std::set<SDNode*> UsersHandled;
2540 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
2543 if (User->getNumOperands() == 1 ||
2544 UsersHandled.insert(User).second) // First time we've seen this?
2545 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2546 if (User->getOperand(i) == TheValue) {
2548 return false; // too many uses
2553 // Found exactly the right number of uses?
2558 // isOnlyUse - Return true if this node is the only use of N.
2559 bool SDNode::isOnlyUse(SDNode *N) const {
2561 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2572 // isOperand - Return true if this node is an operand of N.
2573 bool SDOperand::isOperand(SDNode *N) const {
2574 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2575 if (*this == N->getOperand(i))
2580 bool SDNode::isOperand(SDNode *N) const {
2581 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2582 if (this == N->OperandList[i].Val)
2587 const char *SDNode::getOperationName(const SelectionDAG *G) const {
2588 switch (getOpcode()) {
2590 if (getOpcode() < ISD::BUILTIN_OP_END)
2591 return "<<Unknown DAG Node>>";
2594 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
2595 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2596 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
2598 TargetLowering &TLI = G->getTargetLoweringInfo();
2600 TLI.getTargetNodeName(getOpcode());
2601 if (Name) return Name;
2604 return "<<Unknown Target Node>>";
2607 case ISD::PCMARKER: return "PCMarker";
2608 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
2609 case ISD::SRCVALUE: return "SrcValue";
2610 case ISD::EntryToken: return "EntryToken";
2611 case ISD::TokenFactor: return "TokenFactor";
2612 case ISD::AssertSext: return "AssertSext";
2613 case ISD::AssertZext: return "AssertZext";
2615 case ISD::STRING: return "String";
2616 case ISD::BasicBlock: return "BasicBlock";
2617 case ISD::VALUETYPE: return "ValueType";
2618 case ISD::Register: return "Register";
2620 case ISD::Constant: return "Constant";
2621 case ISD::ConstantFP: return "ConstantFP";
2622 case ISD::GlobalAddress: return "GlobalAddress";
2623 case ISD::FrameIndex: return "FrameIndex";
2624 case ISD::JumpTable: return "JumpTable";
2625 case ISD::ConstantPool: return "ConstantPool";
2626 case ISD::ExternalSymbol: return "ExternalSymbol";
2627 case ISD::INTRINSIC_WO_CHAIN: {
2628 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2629 return Intrinsic::getName((Intrinsic::ID)IID);
2631 case ISD::INTRINSIC_VOID:
2632 case ISD::INTRINSIC_W_CHAIN: {
2633 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
2634 return Intrinsic::getName((Intrinsic::ID)IID);
2637 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
2638 case ISD::TargetConstant: return "TargetConstant";
2639 case ISD::TargetConstantFP:return "TargetConstantFP";
2640 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2641 case ISD::TargetFrameIndex: return "TargetFrameIndex";
2642 case ISD::TargetJumpTable: return "TargetJumpTable";
2643 case ISD::TargetConstantPool: return "TargetConstantPool";
2644 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
2646 case ISD::CopyToReg: return "CopyToReg";
2647 case ISD::CopyFromReg: return "CopyFromReg";
2648 case ISD::UNDEF: return "undef";
2649 case ISD::MERGE_VALUES: return "mergevalues";
2650 case ISD::INLINEASM: return "inlineasm";
2651 case ISD::HANDLENODE: return "handlenode";
2652 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
2653 case ISD::CALL: return "call";
2656 case ISD::FABS: return "fabs";
2657 case ISD::FNEG: return "fneg";
2658 case ISD::FSQRT: return "fsqrt";
2659 case ISD::FSIN: return "fsin";
2660 case ISD::FCOS: return "fcos";
2663 case ISD::ADD: return "add";
2664 case ISD::SUB: return "sub";
2665 case ISD::MUL: return "mul";
2666 case ISD::MULHU: return "mulhu";
2667 case ISD::MULHS: return "mulhs";
2668 case ISD::SDIV: return "sdiv";
2669 case ISD::UDIV: return "udiv";
2670 case ISD::SREM: return "srem";
2671 case ISD::UREM: return "urem";
2672 case ISD::AND: return "and";
2673 case ISD::OR: return "or";
2674 case ISD::XOR: return "xor";
2675 case ISD::SHL: return "shl";
2676 case ISD::SRA: return "sra";
2677 case ISD::SRL: return "srl";
2678 case ISD::ROTL: return "rotl";
2679 case ISD::ROTR: return "rotr";
2680 case ISD::FADD: return "fadd";
2681 case ISD::FSUB: return "fsub";
2682 case ISD::FMUL: return "fmul";
2683 case ISD::FDIV: return "fdiv";
2684 case ISD::FREM: return "frem";
2685 case ISD::FCOPYSIGN: return "fcopysign";
2686 case ISD::VADD: return "vadd";
2687 case ISD::VSUB: return "vsub";
2688 case ISD::VMUL: return "vmul";
2689 case ISD::VSDIV: return "vsdiv";
2690 case ISD::VUDIV: return "vudiv";
2691 case ISD::VAND: return "vand";
2692 case ISD::VOR: return "vor";
2693 case ISD::VXOR: return "vxor";
2695 case ISD::SETCC: return "setcc";
2696 case ISD::SELECT: return "select";
2697 case ISD::SELECT_CC: return "select_cc";
2698 case ISD::VSELECT: return "vselect";
2699 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2700 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2701 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
2702 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
2703 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2704 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2705 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2706 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2707 case ISD::VBIT_CONVERT: return "vbit_convert";
2708 case ISD::ADDC: return "addc";
2709 case ISD::ADDE: return "adde";
2710 case ISD::SUBC: return "subc";
2711 case ISD::SUBE: return "sube";
2712 case ISD::SHL_PARTS: return "shl_parts";
2713 case ISD::SRA_PARTS: return "sra_parts";
2714 case ISD::SRL_PARTS: return "srl_parts";
2716 // Conversion operators.
2717 case ISD::SIGN_EXTEND: return "sign_extend";
2718 case ISD::ZERO_EXTEND: return "zero_extend";
2719 case ISD::ANY_EXTEND: return "any_extend";
2720 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
2721 case ISD::TRUNCATE: return "truncate";
2722 case ISD::FP_ROUND: return "fp_round";
2723 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
2724 case ISD::FP_EXTEND: return "fp_extend";
2726 case ISD::SINT_TO_FP: return "sint_to_fp";
2727 case ISD::UINT_TO_FP: return "uint_to_fp";
2728 case ISD::FP_TO_SINT: return "fp_to_sint";
2729 case ISD::FP_TO_UINT: return "fp_to_uint";
2730 case ISD::BIT_CONVERT: return "bit_convert";
2732 // Control flow instructions
2733 case ISD::BR: return "br";
2734 case ISD::BRIND: return "brind";
2735 case ISD::BRCOND: return "brcond";
2736 case ISD::BR_CC: return "br_cc";
2737 case ISD::RET: return "ret";
2738 case ISD::CALLSEQ_START: return "callseq_start";
2739 case ISD::CALLSEQ_END: return "callseq_end";
2742 case ISD::LOAD: return "load";
2743 case ISD::STORE: return "store";
2744 case ISD::VLOAD: return "vload";
2745 case ISD::EXTLOAD: return "extload";
2746 case ISD::SEXTLOAD: return "sextload";
2747 case ISD::ZEXTLOAD: return "zextload";
2748 case ISD::TRUNCSTORE: return "truncstore";
2749 case ISD::VAARG: return "vaarg";
2750 case ISD::VACOPY: return "vacopy";
2751 case ISD::VAEND: return "vaend";
2752 case ISD::VASTART: return "vastart";
2753 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2754 case ISD::EXTRACT_ELEMENT: return "extract_element";
2755 case ISD::BUILD_PAIR: return "build_pair";
2756 case ISD::STACKSAVE: return "stacksave";
2757 case ISD::STACKRESTORE: return "stackrestore";
2759 // Block memory operations.
2760 case ISD::MEMSET: return "memset";
2761 case ISD::MEMCPY: return "memcpy";
2762 case ISD::MEMMOVE: return "memmove";
2765 case ISD::BSWAP: return "bswap";
2766 case ISD::CTPOP: return "ctpop";
2767 case ISD::CTTZ: return "cttz";
2768 case ISD::CTLZ: return "ctlz";
2771 case ISD::LOCATION: return "location";
2772 case ISD::DEBUG_LOC: return "debug_loc";
2773 case ISD::DEBUG_LABEL: return "debug_label";
2776 switch (cast<CondCodeSDNode>(this)->get()) {
2777 default: assert(0 && "Unknown setcc condition!");
2778 case ISD::SETOEQ: return "setoeq";
2779 case ISD::SETOGT: return "setogt";
2780 case ISD::SETOGE: return "setoge";
2781 case ISD::SETOLT: return "setolt";
2782 case ISD::SETOLE: return "setole";
2783 case ISD::SETONE: return "setone";
2785 case ISD::SETO: return "seto";
2786 case ISD::SETUO: return "setuo";
2787 case ISD::SETUEQ: return "setue";
2788 case ISD::SETUGT: return "setugt";
2789 case ISD::SETUGE: return "setuge";
2790 case ISD::SETULT: return "setult";
2791 case ISD::SETULE: return "setule";
2792 case ISD::SETUNE: return "setune";
2794 case ISD::SETEQ: return "seteq";
2795 case ISD::SETGT: return "setgt";
2796 case ISD::SETGE: return "setge";
2797 case ISD::SETLT: return "setlt";
2798 case ISD::SETLE: return "setle";
2799 case ISD::SETNE: return "setne";
2804 void SDNode::dump() const { dump(0); }
2805 void SDNode::dump(const SelectionDAG *G) const {
2806 std::cerr << (void*)this << ": ";
2808 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2809 if (i) std::cerr << ",";
2810 if (getValueType(i) == MVT::Other)
2813 std::cerr << MVT::getValueTypeString(getValueType(i));
2815 std::cerr << " = " << getOperationName(G);
2818 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2819 if (i) std::cerr << ", ";
2820 std::cerr << (void*)getOperand(i).Val;
2821 if (unsigned RN = getOperand(i).ResNo)
2822 std::cerr << ":" << RN;
2825 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2826 std::cerr << "<" << CSDN->getValue() << ">";
2827 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2828 std::cerr << "<" << CSDN->getValue() << ">";
2829 } else if (const GlobalAddressSDNode *GADN =
2830 dyn_cast<GlobalAddressSDNode>(this)) {
2831 int offset = GADN->getOffset();
2833 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
2835 std::cerr << " + " << offset;
2837 std::cerr << " " << offset;
2838 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
2839 std::cerr << "<" << FIDN->getIndex() << ">";
2840 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
2841 int offset = CP->getOffset();
2842 std::cerr << "<" << *CP->get() << ">";
2844 std::cerr << " + " << offset;
2846 std::cerr << " " << offset;
2847 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
2849 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2851 std::cerr << LBB->getName() << " ";
2852 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
2853 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
2854 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2855 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2857 std::cerr << " #" << R->getReg();
2859 } else if (const ExternalSymbolSDNode *ES =
2860 dyn_cast<ExternalSymbolSDNode>(this)) {
2861 std::cerr << "'" << ES->getSymbol() << "'";
2862 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2864 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2866 std::cerr << "<null:" << M->getOffset() << ">";
2867 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2868 std::cerr << ":" << getValueTypeString(N->getVT());
2872 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
2873 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2874 if (N->getOperand(i).Val->hasOneUse())
2875 DumpNodes(N->getOperand(i).Val, indent+2, G);
2877 std::cerr << "\n" << std::string(indent+2, ' ')
2878 << (void*)N->getOperand(i).Val << ": <multiple use>";
2881 std::cerr << "\n" << std::string(indent, ' ');
2885 void SelectionDAG::dump() const {
2886 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
2887 std::vector<const SDNode*> Nodes;
2888 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2892 std::sort(Nodes.begin(), Nodes.end());
2894 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2895 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
2896 DumpNodes(Nodes[i], 2, this);
2899 DumpNodes(getRoot().Val, 2, this);
2901 std::cerr << "\n\n";
2904 /// InsertISelMapEntry - A helper function to insert a key / element pair
2905 /// into a SDOperand to SDOperand map. This is added to avoid the map
2906 /// insertion operator from being inlined.
2907 void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
2908 SDNode *Key, unsigned KeyResNo,
2909 SDNode *Element, unsigned ElementResNo) {
2910 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
2911 SDOperand(Element, ElementResNo)));