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/CodeGen/MachineConstantPool.h"
21 #include "llvm/Support/MathExtras.h"
22 #include "llvm/Target/MRegisterInfo.h"
23 #include "llvm/Target/TargetLowering.h"
24 #include "llvm/Target/TargetInstrInfo.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/ADT/SetVector.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/StringExtras.h"
35 /// makeVTList - Return an instance of the SDVTList struct initialized with the
36 /// specified members.
37 static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
38 SDVTList Res = {VTs, NumVTs};
42 // isInvertibleForFree - Return true if there is no cost to emitting the logical
43 // inverse of this node.
44 static bool isInvertibleForFree(SDOperand N) {
45 if (isa<ConstantSDNode>(N.Val)) return true;
46 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
51 //===----------------------------------------------------------------------===//
52 // ConstantFPSDNode Class
53 //===----------------------------------------------------------------------===//
55 /// isExactlyValue - We don't rely on operator== working on double values, as
56 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
57 /// As such, this method can be used to do an exact bit-for-bit comparison of
58 /// two floating point values.
59 bool ConstantFPSDNode::isExactlyValue(double V) const {
60 return DoubleToBits(V) == DoubleToBits(Value);
63 //===----------------------------------------------------------------------===//
65 //===----------------------------------------------------------------------===//
67 /// isBuildVectorAllOnes - Return true if the specified node is a
68 /// BUILD_VECTOR where all of the elements are ~0 or undef.
69 bool ISD::isBuildVectorAllOnes(const SDNode *N) {
70 // Look through a bit convert.
71 if (N->getOpcode() == ISD::BIT_CONVERT)
72 N = N->getOperand(0).Val;
74 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
76 unsigned i = 0, e = N->getNumOperands();
78 // Skip over all of the undef values.
79 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
82 // Do not accept an all-undef vector.
83 if (i == e) return false;
85 // Do not accept build_vectors that aren't all constants or which have non-~0
87 SDOperand NotZero = N->getOperand(i);
88 if (isa<ConstantSDNode>(NotZero)) {
89 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
91 } else if (isa<ConstantFPSDNode>(NotZero)) {
92 MVT::ValueType VT = NotZero.getValueType();
94 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
98 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
105 // Okay, we have at least one ~0 value, check to see if the rest match or are
107 for (++i; i != e; ++i)
108 if (N->getOperand(i) != NotZero &&
109 N->getOperand(i).getOpcode() != ISD::UNDEF)
115 /// isBuildVectorAllZeros - Return true if the specified node is a
116 /// BUILD_VECTOR where all of the elements are 0 or undef.
117 bool ISD::isBuildVectorAllZeros(const SDNode *N) {
118 // Look through a bit convert.
119 if (N->getOpcode() == ISD::BIT_CONVERT)
120 N = N->getOperand(0).Val;
122 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
124 unsigned i = 0, e = N->getNumOperands();
126 // Skip over all of the undef values.
127 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
130 // Do not accept an all-undef vector.
131 if (i == e) return false;
133 // Do not accept build_vectors that aren't all constants or which have non-~0
135 SDOperand Zero = N->getOperand(i);
136 if (isa<ConstantSDNode>(Zero)) {
137 if (!cast<ConstantSDNode>(Zero)->isNullValue())
139 } else if (isa<ConstantFPSDNode>(Zero)) {
140 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
145 // Okay, we have at least one ~0 value, check to see if the rest match or are
147 for (++i; i != e; ++i)
148 if (N->getOperand(i) != Zero &&
149 N->getOperand(i).getOpcode() != ISD::UNDEF)
154 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
155 /// when given the operation for (X op Y).
156 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
157 // To perform this operation, we just need to swap the L and G bits of the
159 unsigned OldL = (Operation >> 2) & 1;
160 unsigned OldG = (Operation >> 1) & 1;
161 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
162 (OldL << 1) | // New G bit
163 (OldG << 2)); // New L bit.
166 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
167 /// 'op' is a valid SetCC operation.
168 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
169 unsigned Operation = Op;
171 Operation ^= 7; // Flip L, G, E bits, but not U.
173 Operation ^= 15; // Flip all of the condition bits.
174 if (Operation > ISD::SETTRUE2)
175 Operation &= ~8; // Don't let N and U bits get set.
176 return ISD::CondCode(Operation);
180 /// isSignedOp - For an integer comparison, return 1 if the comparison is a
181 /// signed operation and 2 if the result is an unsigned comparison. Return zero
182 /// if the operation does not depend on the sign of the input (setne and seteq).
183 static int isSignedOp(ISD::CondCode Opcode) {
185 default: assert(0 && "Illegal integer setcc operation!");
187 case ISD::SETNE: return 0;
191 case ISD::SETGE: return 1;
195 case ISD::SETUGE: return 2;
199 /// getSetCCOrOperation - Return the result of a logical OR between different
200 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
201 /// returns SETCC_INVALID if it is not possible to represent the resultant
203 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
205 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
206 // Cannot fold a signed integer setcc with an unsigned integer setcc.
207 return ISD::SETCC_INVALID;
209 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
211 // If the N and U bits get set then the resultant comparison DOES suddenly
212 // care about orderedness, and is true when ordered.
213 if (Op > ISD::SETTRUE2)
214 Op &= ~16; // Clear the U bit if the N bit is set.
216 // Canonicalize illegal integer setcc's.
217 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
220 return ISD::CondCode(Op);
223 /// getSetCCAndOperation - Return the result of a logical AND between different
224 /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
225 /// function returns zero if it is not possible to represent the resultant
227 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
229 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
230 // Cannot fold a signed setcc with an unsigned setcc.
231 return ISD::SETCC_INVALID;
233 // Combine all of the condition bits.
234 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
236 // Canonicalize illegal integer setcc's.
240 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
241 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
242 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
243 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
250 const TargetMachine &SelectionDAG::getTarget() const {
251 return TLI.getTargetMachine();
254 //===----------------------------------------------------------------------===//
255 // SelectionDAG Class
256 //===----------------------------------------------------------------------===//
258 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
260 void SelectionDAG::RemoveDeadNodes() {
261 // Create a dummy node (which is not added to allnodes), that adds a reference
262 // to the root node, preventing it from being deleted.
263 HandleSDNode Dummy(getRoot());
265 SmallVector<SDNode*, 128> DeadNodes;
267 // Add all obviously-dead nodes to the DeadNodes worklist.
268 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
270 DeadNodes.push_back(I);
272 // Process the worklist, deleting the nodes and adding their uses to the
274 while (!DeadNodes.empty()) {
275 SDNode *N = DeadNodes.back();
276 DeadNodes.pop_back();
278 // Take the node out of the appropriate CSE map.
279 RemoveNodeFromCSEMaps(N);
281 // Next, brutally remove the operand list. This is safe to do, as there are
282 // no cycles in the graph.
283 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
284 SDNode *Operand = I->Val;
285 Operand->removeUser(N);
287 // Now that we removed this operand, see if there are no uses of it left.
288 if (Operand->use_empty())
289 DeadNodes.push_back(Operand);
291 delete[] N->OperandList;
295 // Finally, remove N itself.
299 // If the root changed (e.g. it was a dead load, update the root).
300 setRoot(Dummy.getValue());
303 void SelectionDAG::DeleteNode(SDNode *N) {
304 assert(N->use_empty() && "Cannot delete a node that is not dead!");
306 // First take this out of the appropriate CSE map.
307 RemoveNodeFromCSEMaps(N);
309 // Finally, remove uses due to operands of this node, remove from the
310 // AllNodes list, and delete the node.
311 DeleteNodeNotInCSEMaps(N);
314 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
316 // Remove it from the AllNodes list.
319 // Drop all of the operands and decrement used nodes use counts.
320 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
321 I->Val->removeUser(N);
322 delete[] N->OperandList;
329 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
330 /// correspond to it. This is useful when we're about to delete or repurpose
331 /// the node. We don't want future request for structurally identical nodes
332 /// to return N anymore.
333 void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
335 switch (N->getOpcode()) {
336 case ISD::HANDLENODE: return; // noop.
338 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
341 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
342 "Cond code doesn't exist!");
343 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
344 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
346 case ISD::ExternalSymbol:
347 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
349 case ISD::TargetExternalSymbol:
351 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
354 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
355 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
358 // Remove it from the CSE Map.
359 Erased = CSEMap.RemoveNode(N);
363 // Verify that the node was actually in one of the CSE maps, unless it has a
364 // flag result (which cannot be CSE'd) or is one of the special cases that are
365 // not subject to CSE.
366 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
367 !N->isTargetOpcode()) {
370 assert(0 && "Node is not in map!");
375 /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
376 /// has been taken out and modified in some way. If the specified node already
377 /// exists in the CSE maps, do not modify the maps, but return the existing node
378 /// instead. If it doesn't exist, add it and return null.
380 SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
381 assert(N->getNumOperands() && "This is a leaf node!");
382 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
383 return 0; // Never add these nodes.
385 // Check that remaining values produced are not flags.
386 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
387 if (N->getValueType(i) == MVT::Flag)
388 return 0; // Never CSE anything that produces a flag.
390 SDNode *New = CSEMap.GetOrInsertNode(N);
391 if (New != N) return New; // Node already existed.
395 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
396 /// were replaced with those specified. If this node is never memoized,
397 /// return null, otherwise return a pointer to the slot it would take. If a
398 /// node already exists with these operands, the slot will be non-null.
399 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
401 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
402 return 0; // Never add these nodes.
404 // Check that remaining values produced are not flags.
405 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
406 if (N->getValueType(i) == MVT::Flag)
407 return 0; // Never CSE anything that produces a flag.
409 SelectionDAGCSEMap::NodeID ID;
410 ID.SetOpcode(N->getOpcode());
411 ID.SetValueTypes(N->getVTList());
413 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
416 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
417 /// were replaced with those specified. If this node is never memoized,
418 /// return null, otherwise return a pointer to the slot it would take. If a
419 /// node already exists with these operands, the slot will be non-null.
420 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
421 SDOperand Op1, SDOperand Op2,
423 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
424 return 0; // Never add these nodes.
426 // Check that remaining values produced are not flags.
427 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
428 if (N->getValueType(i) == MVT::Flag)
429 return 0; // Never CSE anything that produces a flag.
431 SelectionDAGCSEMap::NodeID ID;
432 ID.SetOpcode(N->getOpcode());
433 ID.SetValueTypes(N->getVTList());
434 ID.SetOperands(Op1, Op2);
435 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
439 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
440 /// were replaced with those specified. If this node is never memoized,
441 /// return null, otherwise return a pointer to the slot it would take. If a
442 /// node already exists with these operands, the slot will be non-null.
443 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
444 const SDOperand *Ops,unsigned NumOps,
446 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
447 return 0; // Never add these nodes.
449 // Check that remaining values produced are not flags.
450 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
451 if (N->getValueType(i) == MVT::Flag)
452 return 0; // Never CSE anything that produces a flag.
454 SelectionDAGCSEMap::NodeID ID;
455 ID.SetOpcode(N->getOpcode());
456 ID.SetValueTypes(N->getVTList());
457 ID.SetOperands(Ops, NumOps);
458 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
462 SelectionDAG::~SelectionDAG() {
463 while (!AllNodes.empty()) {
464 SDNode *N = AllNodes.begin();
465 N->SetNextInBucket(0);
466 delete [] N->OperandList;
469 AllNodes.pop_front();
473 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
474 if (Op.getValueType() == VT) return Op;
475 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
476 return getNode(ISD::AND, Op.getValueType(), Op,
477 getConstant(Imm, Op.getValueType()));
480 SDOperand SelectionDAG::getString(const std::string &Val) {
481 StringSDNode *&N = StringNodes[Val];
483 N = new StringSDNode(Val);
484 AllNodes.push_back(N);
486 return SDOperand(N, 0);
489 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
490 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
491 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
493 // Mask out any bits that are not valid for this constant.
494 Val &= MVT::getIntVTBitMask(VT);
496 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
497 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
500 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
501 return SDOperand(E, 0);
502 SDNode *N = new ConstantSDNode(isT, Val, VT);
503 CSEMap.InsertNode(N, IP);
504 AllNodes.push_back(N);
505 return SDOperand(N, 0);
509 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
511 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
513 Val = (float)Val; // Mask out extra precision.
515 // Do the map lookup using the actual bit pattern for the floating point
516 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
517 // we don't have issues with SNANs.
518 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
519 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
520 ID.AddInteger(DoubleToBits(Val));
522 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
523 return SDOperand(E, 0);
524 SDNode *N = new ConstantFPSDNode(isTarget, Val, VT);
525 CSEMap.InsertNode(N, IP);
526 AllNodes.push_back(N);
527 return SDOperand(N, 0);
530 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
531 MVT::ValueType VT, int Offset,
533 unsigned Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
534 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
536 ID.AddInteger(Offset);
538 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
539 return SDOperand(E, 0);
540 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
541 CSEMap.InsertNode(N, IP);
542 AllNodes.push_back(N);
543 return SDOperand(N, 0);
546 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
548 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
549 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
552 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
553 return SDOperand(E, 0);
554 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
555 CSEMap.InsertNode(N, IP);
556 AllNodes.push_back(N);
557 return SDOperand(N, 0);
560 SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
561 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
562 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
565 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
566 return SDOperand(E, 0);
567 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
568 CSEMap.InsertNode(N, IP);
569 AllNodes.push_back(N);
570 return SDOperand(N, 0);
573 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
574 unsigned Alignment, int Offset,
576 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
577 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
578 ID.AddInteger(Alignment);
579 ID.AddInteger(Offset);
582 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
583 return SDOperand(E, 0);
584 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
585 CSEMap.InsertNode(N, IP);
586 AllNodes.push_back(N);
587 return SDOperand(N, 0);
591 SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
593 unsigned Alignment, int Offset,
595 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
596 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
597 ID.AddInteger(Alignment);
598 ID.AddInteger(Offset);
599 C->AddSelectionDAGCSEId(&ID);
601 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
602 return SDOperand(E, 0);
603 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
604 CSEMap.InsertNode(N, IP);
605 AllNodes.push_back(N);
606 return SDOperand(N, 0);
610 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
611 SelectionDAGCSEMap::NodeID ID(ISD::BasicBlock, getVTList(MVT::Other));
614 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
615 return SDOperand(E, 0);
616 SDNode *N = new BasicBlockSDNode(MBB);
617 CSEMap.InsertNode(N, IP);
618 AllNodes.push_back(N);
619 return SDOperand(N, 0);
622 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
623 if ((unsigned)VT >= ValueTypeNodes.size())
624 ValueTypeNodes.resize(VT+1);
625 if (ValueTypeNodes[VT] == 0) {
626 ValueTypeNodes[VT] = new VTSDNode(VT);
627 AllNodes.push_back(ValueTypeNodes[VT]);
630 return SDOperand(ValueTypeNodes[VT], 0);
633 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
634 SDNode *&N = ExternalSymbols[Sym];
635 if (N) return SDOperand(N, 0);
636 N = new ExternalSymbolSDNode(false, Sym, VT);
637 AllNodes.push_back(N);
638 return SDOperand(N, 0);
641 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
643 SDNode *&N = TargetExternalSymbols[Sym];
644 if (N) return SDOperand(N, 0);
645 N = new ExternalSymbolSDNode(true, Sym, VT);
646 AllNodes.push_back(N);
647 return SDOperand(N, 0);
650 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
651 if ((unsigned)Cond >= CondCodeNodes.size())
652 CondCodeNodes.resize(Cond+1);
654 if (CondCodeNodes[Cond] == 0) {
655 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
656 AllNodes.push_back(CondCodeNodes[Cond]);
658 return SDOperand(CondCodeNodes[Cond], 0);
661 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
662 SelectionDAGCSEMap::NodeID ID(ISD::Register, getVTList(VT));
663 ID.AddInteger(RegNo);
665 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
666 return SDOperand(E, 0);
667 SDNode *N = new RegisterSDNode(RegNo, VT);
668 CSEMap.InsertNode(N, IP);
669 AllNodes.push_back(N);
670 return SDOperand(N, 0);
673 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
674 assert((!V || isa<PointerType>(V->getType())) &&
675 "SrcValue is not a pointer?");
677 SelectionDAGCSEMap::NodeID ID(ISD::SRCVALUE, getVTList(MVT::Other));
679 ID.AddInteger(Offset);
681 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
682 return SDOperand(E, 0);
683 SDNode *N = new SrcValueSDNode(V, Offset);
684 CSEMap.InsertNode(N, IP);
685 AllNodes.push_back(N);
686 return SDOperand(N, 0);
689 SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
690 SDOperand N2, ISD::CondCode Cond) {
691 // These setcc operations always fold.
695 case ISD::SETFALSE2: return getConstant(0, VT);
697 case ISD::SETTRUE2: return getConstant(1, VT);
709 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
713 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
714 uint64_t C2 = N2C->getValue();
715 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
716 uint64_t C1 = N1C->getValue();
718 // Sign extend the operands if required
719 if (ISD::isSignedIntSetCC(Cond)) {
720 C1 = N1C->getSignExtended();
721 C2 = N2C->getSignExtended();
725 default: assert(0 && "Unknown integer setcc!");
726 case ISD::SETEQ: return getConstant(C1 == C2, VT);
727 case ISD::SETNE: return getConstant(C1 != C2, VT);
728 case ISD::SETULT: return getConstant(C1 < C2, VT);
729 case ISD::SETUGT: return getConstant(C1 > C2, VT);
730 case ISD::SETULE: return getConstant(C1 <= C2, VT);
731 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
732 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
733 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
734 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
735 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
738 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
739 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
740 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
742 // If the comparison constant has bits in the upper part, the
743 // zero-extended value could never match.
744 if (C2 & (~0ULL << InSize)) {
745 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
749 case ISD::SETEQ: return getConstant(0, VT);
752 case ISD::SETNE: return getConstant(1, VT);
755 // True if the sign bit of C2 is set.
756 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
759 // True if the sign bit of C2 isn't set.
760 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
766 // Otherwise, we can perform the comparison with the low bits.
774 return getSetCC(VT, N1.getOperand(0),
775 getConstant(C2, N1.getOperand(0).getValueType()),
778 break; // todo, be more careful with signed comparisons
780 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
781 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
782 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
783 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
784 MVT::ValueType ExtDstTy = N1.getValueType();
785 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
787 // If the extended part has any inconsistent bits, it cannot ever
788 // compare equal. In other words, they have to be all ones or all
791 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
792 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
793 return getConstant(Cond == ISD::SETNE, VT);
795 // Otherwise, make this a use of a zext.
796 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
797 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
801 uint64_t MinVal, MaxVal;
802 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
803 if (ISD::isSignedIntSetCC(Cond)) {
804 MinVal = 1ULL << (OperandBitSize-1);
805 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
806 MaxVal = ~0ULL >> (65-OperandBitSize);
811 MaxVal = ~0ULL >> (64-OperandBitSize);
814 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
815 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
816 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
817 --C2; // X >= C1 --> X > (C1-1)
818 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
819 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
822 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
823 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
824 ++C2; // X <= C1 --> X < (C1+1)
825 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
826 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
829 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
830 return getConstant(0, VT); // X < MIN --> false
832 // Canonicalize setgt X, Min --> setne X, Min
833 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
834 return getSetCC(VT, N1, N2, ISD::SETNE);
836 // If we have setult X, 1, turn it into seteq X, 0
837 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
838 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
840 // If we have setugt X, Max-1, turn it into seteq X, Max
841 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
842 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
845 // If we have "setcc X, C1", check to see if we can shrink the immediate
848 // SETUGT X, SINTMAX -> SETLT X, 0
849 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
850 C2 == (~0ULL >> (65-OperandBitSize)))
851 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
853 // FIXME: Implement the rest of these.
856 // Fold bit comparisons when we can.
857 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
858 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
859 if (ConstantSDNode *AndRHS =
860 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
861 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
862 // Perform the xform if the AND RHS is a single bit.
863 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
864 return getNode(ISD::SRL, VT, N1,
865 getConstant(Log2_64(AndRHS->getValue()),
866 TLI.getShiftAmountTy()));
868 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
869 // (X & 8) == 8 --> (X & 8) >> 3
870 // Perform the xform if C2 is a single bit.
871 if ((C2 & (C2-1)) == 0) {
872 return getNode(ISD::SRL, VT, N1,
873 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
878 } else if (isa<ConstantSDNode>(N1.Val)) {
879 // Ensure that the constant occurs on the RHS.
880 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
883 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
884 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
885 double C1 = N1C->getValue(), C2 = N2C->getValue();
888 default: break; // FIXME: Implement the rest of these!
889 case ISD::SETEQ: return getConstant(C1 == C2, VT);
890 case ISD::SETNE: return getConstant(C1 != C2, VT);
891 case ISD::SETLT: return getConstant(C1 < C2, VT);
892 case ISD::SETGT: return getConstant(C1 > C2, VT);
893 case ISD::SETLE: return getConstant(C1 <= C2, VT);
894 case ISD::SETGE: return getConstant(C1 >= C2, VT);
897 // Ensure that the constant occurs on the RHS.
898 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
901 // Could not fold it.
905 /// getNode - Gets or creates the specified node.
907 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
908 SelectionDAGCSEMap::NodeID ID(Opcode, getVTList(VT));
910 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
911 return SDOperand(E, 0);
912 SDNode *N = new SDNode(Opcode, VT);
913 CSEMap.InsertNode(N, IP);
915 AllNodes.push_back(N);
916 return SDOperand(N, 0);
919 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
922 // Constant fold unary operations with an integer constant operand.
923 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
924 uint64_t Val = C->getValue();
927 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
928 case ISD::ANY_EXTEND:
929 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
930 case ISD::TRUNCATE: return getConstant(Val, VT);
931 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
932 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
933 case ISD::BIT_CONVERT:
934 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
935 return getConstantFP(BitsToFloat(Val), VT);
936 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
937 return getConstantFP(BitsToDouble(Val), VT);
941 default: assert(0 && "Invalid bswap!"); break;
942 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
943 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
944 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
949 default: assert(0 && "Invalid ctpop!"); break;
950 case MVT::i1: return getConstant(Val != 0, VT);
952 Tmp1 = (unsigned)Val & 0xFF;
953 return getConstant(CountPopulation_32(Tmp1), VT);
955 Tmp1 = (unsigned)Val & 0xFFFF;
956 return getConstant(CountPopulation_32(Tmp1), VT);
958 return getConstant(CountPopulation_32((unsigned)Val), VT);
960 return getConstant(CountPopulation_64(Val), VT);
964 default: assert(0 && "Invalid ctlz!"); break;
965 case MVT::i1: return getConstant(Val == 0, VT);
967 Tmp1 = (unsigned)Val & 0xFF;
968 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
970 Tmp1 = (unsigned)Val & 0xFFFF;
971 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
973 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
975 return getConstant(CountLeadingZeros_64(Val), VT);
979 default: assert(0 && "Invalid cttz!"); break;
980 case MVT::i1: return getConstant(Val == 0, VT);
982 Tmp1 = (unsigned)Val | 0x100;
983 return getConstant(CountTrailingZeros_32(Tmp1), VT);
985 Tmp1 = (unsigned)Val | 0x10000;
986 return getConstant(CountTrailingZeros_32(Tmp1), VT);
988 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
990 return getConstant(CountTrailingZeros_64(Val), VT);
995 // Constant fold unary operations with an floating point constant operand.
996 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
999 return getConstantFP(-C->getValue(), VT);
1001 return getConstantFP(fabs(C->getValue()), VT);
1003 case ISD::FP_EXTEND:
1004 return getConstantFP(C->getValue(), VT);
1005 case ISD::FP_TO_SINT:
1006 return getConstant((int64_t)C->getValue(), VT);
1007 case ISD::FP_TO_UINT:
1008 return getConstant((uint64_t)C->getValue(), VT);
1009 case ISD::BIT_CONVERT:
1010 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1011 return getConstant(FloatToBits(C->getValue()), VT);
1012 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1013 return getConstant(DoubleToBits(C->getValue()), VT);
1017 unsigned OpOpcode = Operand.Val->getOpcode();
1019 case ISD::TokenFactor:
1020 return Operand; // Factor of one node? No factor.
1021 case ISD::SIGN_EXTEND:
1022 if (Operand.getValueType() == VT) return Operand; // noop extension
1023 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
1024 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1025 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1027 case ISD::ZERO_EXTEND:
1028 if (Operand.getValueType() == VT) return Operand; // noop extension
1029 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
1030 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1031 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1033 case ISD::ANY_EXTEND:
1034 if (Operand.getValueType() == VT) return Operand; // noop extension
1035 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
1036 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1037 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1038 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1041 if (Operand.getValueType() == VT) return Operand; // noop truncate
1042 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
1043 if (OpOpcode == ISD::TRUNCATE)
1044 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1045 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1046 OpOpcode == ISD::ANY_EXTEND) {
1047 // If the source is smaller than the dest, we still need an extend.
1048 if (Operand.Val->getOperand(0).getValueType() < VT)
1049 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1050 else if (Operand.Val->getOperand(0).getValueType() > VT)
1051 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1053 return Operand.Val->getOperand(0);
1056 case ISD::BIT_CONVERT:
1057 // Basic sanity checking.
1058 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
1059 && "Cannot BIT_CONVERT between two different types!");
1060 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1061 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1062 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1063 if (OpOpcode == ISD::UNDEF)
1064 return getNode(ISD::UNDEF, VT);
1066 case ISD::SCALAR_TO_VECTOR:
1067 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1068 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1069 "Illegal SCALAR_TO_VECTOR node!");
1072 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1073 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1074 Operand.Val->getOperand(0));
1075 if (OpOpcode == ISD::FNEG) // --X -> X
1076 return Operand.Val->getOperand(0);
1079 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1080 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1085 SDVTList VTs = getVTList(VT);
1086 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1087 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Operand);
1089 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1090 return SDOperand(E, 0);
1091 N = new SDNode(Opcode, Operand);
1092 N->setValueTypes(VTs);
1093 CSEMap.InsertNode(N, IP);
1095 N = new SDNode(Opcode, Operand);
1096 N->setValueTypes(VTs);
1098 AllNodes.push_back(N);
1099 return SDOperand(N, 0);
1104 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1105 SDOperand N1, SDOperand N2) {
1108 case ISD::TokenFactor:
1109 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1110 N2.getValueType() == MVT::Other && "Invalid token factor!");
1119 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1126 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1133 assert(N1.getValueType() == N2.getValueType() &&
1134 N1.getValueType() == VT && "Binary operator types must match!");
1136 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1137 assert(N1.getValueType() == VT &&
1138 MVT::isFloatingPoint(N1.getValueType()) &&
1139 MVT::isFloatingPoint(N2.getValueType()) &&
1140 "Invalid FCOPYSIGN!");
1147 assert(VT == N1.getValueType() &&
1148 "Shift operators return type must be the same as their first arg");
1149 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1150 VT != MVT::i1 && "Shifts only work on integers");
1152 case ISD::FP_ROUND_INREG: {
1153 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1154 assert(VT == N1.getValueType() && "Not an inreg round!");
1155 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1156 "Cannot FP_ROUND_INREG integer types");
1157 assert(EVT <= VT && "Not rounding down!");
1160 case ISD::AssertSext:
1161 case ISD::AssertZext:
1162 case ISD::SIGN_EXTEND_INREG: {
1163 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1164 assert(VT == N1.getValueType() && "Not an inreg extend!");
1165 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1166 "Cannot *_EXTEND_INREG FP types");
1167 assert(EVT <= VT && "Not extending!");
1174 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1175 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1177 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1178 int64_t Val = N1C->getValue();
1179 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1180 Val <<= 64-FromBits;
1181 Val >>= 64-FromBits;
1182 return getConstant(Val, VT);
1186 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1188 case ISD::ADD: return getConstant(C1 + C2, VT);
1189 case ISD::SUB: return getConstant(C1 - C2, VT);
1190 case ISD::MUL: return getConstant(C1 * C2, VT);
1192 if (C2) return getConstant(C1 / C2, VT);
1195 if (C2) return getConstant(C1 % C2, VT);
1198 if (C2) return getConstant(N1C->getSignExtended() /
1199 N2C->getSignExtended(), VT);
1202 if (C2) return getConstant(N1C->getSignExtended() %
1203 N2C->getSignExtended(), VT);
1205 case ISD::AND : return getConstant(C1 & C2, VT);
1206 case ISD::OR : return getConstant(C1 | C2, VT);
1207 case ISD::XOR : return getConstant(C1 ^ C2, VT);
1208 case ISD::SHL : return getConstant(C1 << C2, VT);
1209 case ISD::SRL : return getConstant(C1 >> C2, VT);
1210 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
1212 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1215 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1219 } else { // Cannonicalize constant to RHS if commutative
1220 if (isCommutativeBinOp(Opcode)) {
1221 std::swap(N1C, N2C);
1227 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1228 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1231 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1233 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1234 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1235 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1237 if (C2) return getConstantFP(C1 / C2, VT);
1240 if (C2) return getConstantFP(fmod(C1, C2), VT);
1242 case ISD::FCOPYSIGN: {
1253 if (u2.I < 0) // Sign bit of RHS set?
1254 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1256 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1257 return getConstantFP(u1.F, VT);
1261 } else { // Cannonicalize constant to RHS if commutative
1262 if (isCommutativeBinOp(Opcode)) {
1263 std::swap(N1CFP, N2CFP);
1269 // Canonicalize an UNDEF to the RHS, even over a constant.
1270 if (N1.getOpcode() == ISD::UNDEF) {
1271 if (isCommutativeBinOp(Opcode)) {
1275 case ISD::FP_ROUND_INREG:
1276 case ISD::SIGN_EXTEND_INREG:
1282 return N1; // fold op(undef, arg2) -> undef
1289 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1294 // Fold a bunch of operators when the RHS is undef.
1295 if (N2.getOpcode() == ISD::UNDEF) {
1309 return N2; // fold op(arg1, undef) -> undef
1314 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1316 return getConstant(MVT::getIntVTBitMask(VT), VT);
1322 // Finally, fold operations that do not require constants.
1324 case ISD::FP_ROUND_INREG:
1325 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1327 case ISD::SIGN_EXTEND_INREG: {
1328 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1329 if (EVT == VT) return N1; // Not actually extending
1332 case ISD::EXTRACT_ELEMENT:
1333 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
1335 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
1336 // 64-bit integers into 32-bit parts. Instead of building the extract of
1337 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
1338 if (N1.getOpcode() == ISD::BUILD_PAIR)
1339 return N1.getOperand(N2C->getValue());
1341 // EXTRACT_ELEMENT of a constant int is also very common.
1342 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
1343 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
1344 return getConstant(C->getValue() >> Shift, VT);
1348 // FIXME: figure out how to safely handle things like
1349 // int foo(int x) { return 1 << (x & 255); }
1350 // int bar() { return foo(256); }
1355 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1356 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
1357 return getNode(Opcode, VT, N1, N2.getOperand(0));
1358 else if (N2.getOpcode() == ISD::AND)
1359 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1360 // If the and is only masking out bits that cannot effect the shift,
1361 // eliminate the and.
1362 unsigned NumBits = MVT::getSizeInBits(VT);
1363 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1364 return getNode(Opcode, VT, N1, N2.getOperand(0));
1370 // Memoize this node if possible.
1372 SDVTList VTs = getVTList(VT);
1373 if (VT != MVT::Flag) {
1374 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2);
1376 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1377 return SDOperand(E, 0);
1378 N = new SDNode(Opcode, N1, N2);
1379 N->setValueTypes(VTs);
1380 CSEMap.InsertNode(N, IP);
1382 N = new SDNode(Opcode, N1, N2);
1383 N->setValueTypes(VTs);
1386 AllNodes.push_back(N);
1387 return SDOperand(N, 0);
1390 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1391 SDOperand N1, SDOperand N2, SDOperand N3) {
1392 // Perform various simplifications.
1393 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1394 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1395 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1398 // Use SimplifySetCC to simplify SETCC's.
1399 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
1400 if (Simp.Val) return Simp;
1405 if (N1C->getValue())
1406 return N2; // select true, X, Y -> X
1408 return N3; // select false, X, Y -> Y
1410 if (N2 == N3) return N2; // select C, X, X -> X
1414 if (N2C->getValue()) // Unconditional branch
1415 return getNode(ISD::BR, MVT::Other, N1, N3);
1417 return N1; // Never-taken branch
1419 case ISD::VECTOR_SHUFFLE:
1420 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1421 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1422 N3.getOpcode() == ISD::BUILD_VECTOR &&
1423 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1424 "Illegal VECTOR_SHUFFLE node!");
1428 // Memoize node if it doesn't produce a flag.
1430 SDVTList VTs = getVTList(VT);
1431 if (VT != MVT::Flag) {
1432 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2, N3);
1434 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1435 return SDOperand(E, 0);
1436 N = new SDNode(Opcode, N1, N2, N3);
1437 N->setValueTypes(VTs);
1438 CSEMap.InsertNode(N, IP);
1440 N = new SDNode(Opcode, N1, N2, N3);
1441 N->setValueTypes(VTs);
1443 AllNodes.push_back(N);
1444 return SDOperand(N, 0);
1447 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1448 SDOperand N1, SDOperand N2, SDOperand N3,
1450 SDOperand Ops[] = { N1, N2, N3, N4 };
1451 return getNode(Opcode, VT, Ops, 4);
1454 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1455 SDOperand N1, SDOperand N2, SDOperand N3,
1456 SDOperand N4, SDOperand N5) {
1457 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1458 return getNode(Opcode, VT, Ops, 5);
1461 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1462 SDOperand Chain, SDOperand Ptr,
1464 SDVTList VTs = getVTList(VT, MVT::Other);
1466 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, SV);
1468 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1469 return SDOperand(E, 0);
1470 SDNode *N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1471 N->setValueTypes(VTs);
1472 CSEMap.InsertNode(N, IP);
1473 AllNodes.push_back(N);
1474 return SDOperand(N, 0);
1477 SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1478 SDOperand Chain, SDOperand Ptr,
1480 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1481 getValueType(EVT) };
1482 return getNode(ISD::VLOAD, getVTList(MVT::Vector, MVT::Other), Ops, 5);
1485 SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType LType, MVT::ValueType VT,
1486 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1487 MVT::ValueType EVT) {
1488 SDOperand Ops[] = { Chain, Ptr, SV, getValueType(EVT),
1489 getConstant(LType, MVT::i32) };
1490 return getNode(ISD::LOADX, getVTList(VT, MVT::Other), Ops, 5);
1493 SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Value,
1494 SDOperand Ptr, SDOperand SV) {
1495 SDVTList VTs = getVTList(MVT::Other);
1496 SDOperand Ops[] = { Chain, Value, Ptr, SV };
1497 SelectionDAGCSEMap::NodeID ID(ISD::STORE, VTs, Ops, 4);
1499 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1500 return SDOperand(E, 0);
1501 SDNode *N = new SDNode(ISD::STORE, Chain, Value, Ptr, SV);
1502 N->setValueTypes(VTs);
1503 CSEMap.InsertNode(N, IP);
1504 AllNodes.push_back(N);
1505 return SDOperand(N, 0);
1508 SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1509 SDOperand Chain, SDOperand Ptr,
1511 SDOperand Ops[] = { Chain, Ptr, SV };
1512 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
1515 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1516 const SDOperand *Ops, unsigned NumOps) {
1518 case 0: return getNode(Opcode, VT);
1519 case 1: return getNode(Opcode, VT, Ops[0]);
1520 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1521 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
1527 case ISD::TRUNCSTORE: {
1528 assert(NumOps == 5 && "TRUNCSTORE takes 5 operands!");
1529 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1530 #if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1531 // If this is a truncating store of a constant, convert to the desired type
1532 // and store it instead.
1533 if (isa<Constant>(Ops[0])) {
1534 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1535 if (isa<Constant>(Op))
1538 // Also for ConstantFP?
1540 if (Ops[0].getValueType() == EVT) // Normal store?
1541 return getStore(Ops[0], Ops[1], Ops[2], Ops[3]);
1542 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1543 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1544 "Can't do FP-INT conversion!");
1547 case ISD::SELECT_CC: {
1548 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
1549 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1550 "LHS and RHS of condition must have same type!");
1551 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1552 "True and False arms of SelectCC must have same type!");
1553 assert(Ops[2].getValueType() == VT &&
1554 "select_cc node must be of same type as true and false value!");
1558 assert(NumOps == 5 && "BR_CC takes 5 operands!");
1559 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1560 "LHS/RHS of comparison should match types!");
1567 SDVTList VTs = getVTList(VT);
1568 if (VT != MVT::Flag) {
1569 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Ops, NumOps);
1571 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1572 return SDOperand(E, 0);
1573 N = new SDNode(Opcode, Ops, NumOps);
1574 N->setValueTypes(VTs);
1575 CSEMap.InsertNode(N, IP);
1577 N = new SDNode(Opcode, Ops, NumOps);
1578 N->setValueTypes(VTs);
1580 AllNodes.push_back(N);
1581 return SDOperand(N, 0);
1584 SDOperand SelectionDAG::getNode(unsigned Opcode,
1585 std::vector<MVT::ValueType> &ResultTys,
1586 const SDOperand *Ops, unsigned NumOps) {
1587 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
1591 SDOperand SelectionDAG::getNode(unsigned Opcode,
1592 const MVT::ValueType *VTs, unsigned NumVTs,
1593 const SDOperand *Ops, unsigned NumOps) {
1595 return getNode(Opcode, VTs[0], Ops, NumOps);
1596 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
1599 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
1600 const SDOperand *Ops, unsigned NumOps) {
1601 if (VTList.NumVTs == 1)
1602 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
1606 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1607 unsigned LType = cast<ConstantSDNode>(Ops[4])->getValue();
1608 assert(NumOps == 5 && VTList.NumVTs == 2 && "Bad *EXTLOAD!");
1609 // If they are asking for an extending load from/to the same thing, return a
1611 if (VTList.VTs[0] == EVT)
1612 return getLoad(VTList.VTs[0], Ops[0], Ops[1], Ops[2]);
1613 if (MVT::isVector(VTList.VTs[0])) {
1614 assert(EVT == MVT::getVectorBaseType(VTList.VTs[0]) &&
1615 "Invalid vector extload!");
1617 assert(EVT < VTList.VTs[0] &&
1618 "Should only be an extending load, not truncating!");
1620 assert((LType == ISD::EXTLOAD || MVT::isInteger(VTList.VTs[0])) &&
1621 "Cannot sign/zero extend a FP/Vector load!");
1622 assert(MVT::isInteger(VTList.VTs[0]) == MVT::isInteger(EVT) &&
1623 "Cannot convert from FP to Int or Int -> FP!");
1627 // FIXME: figure out how to safely handle things like
1628 // int foo(int x) { return 1 << (x & 255); }
1629 // int bar() { return foo(256); }
1631 case ISD::SRA_PARTS:
1632 case ISD::SRL_PARTS:
1633 case ISD::SHL_PARTS:
1634 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1635 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
1636 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1637 else if (N3.getOpcode() == ISD::AND)
1638 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1639 // If the and is only masking out bits that cannot effect the shift,
1640 // eliminate the and.
1641 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1642 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1643 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1649 // Memoize the node unless it returns a flag.
1651 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
1652 SelectionDAGCSEMap::NodeID ID;
1653 ID.SetOpcode(Opcode);
1654 ID.SetValueTypes(VTList);
1655 ID.SetOperands(&Ops[0], NumOps);
1657 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1658 return SDOperand(E, 0);
1659 N = new SDNode(Opcode, Ops, NumOps);
1660 N->setValueTypes(VTList);
1661 CSEMap.InsertNode(N, IP);
1663 N = new SDNode(Opcode, Ops, NumOps);
1664 N->setValueTypes(VTList);
1666 AllNodes.push_back(N);
1667 return SDOperand(N, 0);
1670 SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
1671 return makeVTList(SDNode::getValueTypeList(VT), 1);
1674 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
1675 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1676 E = VTList.end(); I != E; ++I) {
1677 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
1678 return makeVTList(&(*I)[0], 2);
1680 std::vector<MVT::ValueType> V;
1683 VTList.push_front(V);
1684 return makeVTList(&(*VTList.begin())[0], 2);
1686 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
1687 MVT::ValueType VT3) {
1688 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1689 E = VTList.end(); I != E; ++I) {
1690 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
1692 return makeVTList(&(*I)[0], 3);
1694 std::vector<MVT::ValueType> V;
1698 VTList.push_front(V);
1699 return makeVTList(&(*VTList.begin())[0], 3);
1702 SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
1704 case 0: assert(0 && "Cannot have nodes without results!");
1705 case 1: return makeVTList(SDNode::getValueTypeList(VTs[0]), 1);
1706 case 2: return getVTList(VTs[0], VTs[1]);
1707 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
1711 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1712 E = VTList.end(); I != E; ++I) {
1713 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
1715 bool NoMatch = false;
1716 for (unsigned i = 2; i != NumVTs; ++i)
1717 if (VTs[i] != (*I)[i]) {
1722 return makeVTList(&*I->begin(), NumVTs);
1725 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
1726 return makeVTList(&*VTList.begin()->begin(), NumVTs);
1730 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1731 /// specified operands. If the resultant node already exists in the DAG,
1732 /// this does not modify the specified node, instead it returns the node that
1733 /// already exists. If the resultant node does not exist in the DAG, the
1734 /// input node is returned. As a degenerate case, if you specify the same
1735 /// input operands as the node already has, the input node is returned.
1736 SDOperand SelectionDAG::
1737 UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1738 SDNode *N = InN.Val;
1739 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1741 // Check to see if there is no change.
1742 if (Op == N->getOperand(0)) return InN;
1744 // See if the modified node already exists.
1745 void *InsertPos = 0;
1746 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1747 return SDOperand(Existing, InN.ResNo);
1749 // Nope it doesn't. Remove the node from it's current place in the maps.
1751 RemoveNodeFromCSEMaps(N);
1753 // Now we update the operands.
1754 N->OperandList[0].Val->removeUser(N);
1756 N->OperandList[0] = Op;
1758 // If this gets put into a CSE map, add it.
1759 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1763 SDOperand SelectionDAG::
1764 UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1765 SDNode *N = InN.Val;
1766 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1768 // Check to see if there is no change.
1769 bool AnyChange = false;
1770 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1771 return InN; // No operands changed, just return the input node.
1773 // See if the modified node already exists.
1774 void *InsertPos = 0;
1775 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1776 return SDOperand(Existing, InN.ResNo);
1778 // Nope it doesn't. Remove the node from it's current place in the maps.
1780 RemoveNodeFromCSEMaps(N);
1782 // Now we update the operands.
1783 if (N->OperandList[0] != Op1) {
1784 N->OperandList[0].Val->removeUser(N);
1785 Op1.Val->addUser(N);
1786 N->OperandList[0] = Op1;
1788 if (N->OperandList[1] != Op2) {
1789 N->OperandList[1].Val->removeUser(N);
1790 Op2.Val->addUser(N);
1791 N->OperandList[1] = Op2;
1794 // If this gets put into a CSE map, add it.
1795 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1799 SDOperand SelectionDAG::
1800 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1801 SDOperand Ops[] = { Op1, Op2, Op3 };
1802 return UpdateNodeOperands(N, Ops, 3);
1805 SDOperand SelectionDAG::
1806 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1807 SDOperand Op3, SDOperand Op4) {
1808 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
1809 return UpdateNodeOperands(N, Ops, 4);
1812 SDOperand SelectionDAG::
1813 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1814 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1815 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
1816 return UpdateNodeOperands(N, Ops, 5);
1820 SDOperand SelectionDAG::
1821 UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
1822 SDNode *N = InN.Val;
1823 assert(N->getNumOperands() == NumOps &&
1824 "Update with wrong number of operands");
1826 // Check to see if there is no change.
1827 bool AnyChange = false;
1828 for (unsigned i = 0; i != NumOps; ++i) {
1829 if (Ops[i] != N->getOperand(i)) {
1835 // No operands changed, just return the input node.
1836 if (!AnyChange) return InN;
1838 // See if the modified node already exists.
1839 void *InsertPos = 0;
1840 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
1841 return SDOperand(Existing, InN.ResNo);
1843 // Nope it doesn't. Remove the node from it's current place in the maps.
1845 RemoveNodeFromCSEMaps(N);
1847 // Now we update the operands.
1848 for (unsigned i = 0; i != NumOps; ++i) {
1849 if (N->OperandList[i] != Ops[i]) {
1850 N->OperandList[i].Val->removeUser(N);
1851 Ops[i].Val->addUser(N);
1852 N->OperandList[i] = Ops[i];
1856 // If this gets put into a CSE map, add it.
1857 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1864 /// SelectNodeTo - These are used for target selectors to *mutate* the
1865 /// specified node to have the specified return type, Target opcode, and
1866 /// operands. Note that target opcodes are stored as
1867 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1869 /// Note that SelectNodeTo returns the resultant node. If there is already a
1870 /// node of the specified opcode and operands, it returns that node instead of
1871 /// the current one.
1872 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1873 MVT::ValueType VT) {
1874 SDVTList VTs = getVTList(VT);
1875 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1877 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1880 RemoveNodeFromCSEMaps(N);
1882 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1883 N->setValueTypes(VTs);
1885 CSEMap.InsertNode(N, IP);
1889 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1890 MVT::ValueType VT, SDOperand Op1) {
1891 // If an identical node already exists, use it.
1892 SDVTList VTs = getVTList(VT);
1893 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
1895 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1898 RemoveNodeFromCSEMaps(N);
1899 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1900 N->setValueTypes(VTs);
1901 N->setOperands(Op1);
1902 CSEMap.InsertNode(N, IP);
1906 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1907 MVT::ValueType VT, SDOperand Op1,
1909 // If an identical node already exists, use it.
1910 SDVTList VTs = getVTList(VT);
1911 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1913 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1916 RemoveNodeFromCSEMaps(N);
1917 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1918 N->setValueTypes(VTs);
1919 N->setOperands(Op1, Op2);
1921 CSEMap.InsertNode(N, IP); // Memoize the new node.
1925 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1926 MVT::ValueType VT, SDOperand Op1,
1927 SDOperand Op2, SDOperand Op3) {
1928 // If an identical node already exists, use it.
1929 SDVTList VTs = getVTList(VT);
1930 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
1933 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1936 RemoveNodeFromCSEMaps(N);
1937 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1938 N->setValueTypes(VTs);
1939 N->setOperands(Op1, Op2, Op3);
1941 CSEMap.InsertNode(N, IP); // Memoize the new node.
1945 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1946 MVT::ValueType VT, const SDOperand *Ops,
1948 // If an identical node already exists, use it.
1949 SDVTList VTs = getVTList(VT);
1950 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1951 for (unsigned i = 0; i != NumOps; ++i)
1952 ID.AddOperand(Ops[i]);
1954 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1957 RemoveNodeFromCSEMaps(N);
1958 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1959 N->setValueTypes(VTs);
1960 N->setOperands(Ops, NumOps);
1962 CSEMap.InsertNode(N, IP); // Memoize the new node.
1966 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1967 MVT::ValueType VT1, MVT::ValueType VT2,
1968 SDOperand Op1, SDOperand Op2) {
1969 SDVTList VTs = getVTList(VT1, VT2);
1970 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1972 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1975 RemoveNodeFromCSEMaps(N);
1976 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1977 N->setValueTypes(VTs);
1978 N->setOperands(Op1, Op2);
1980 CSEMap.InsertNode(N, IP); // Memoize the new node.
1984 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1985 MVT::ValueType VT1, MVT::ValueType VT2,
1986 SDOperand Op1, SDOperand Op2,
1988 // If an identical node already exists, use it.
1989 SDVTList VTs = getVTList(VT1, VT2);
1990 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
1993 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1996 RemoveNodeFromCSEMaps(N);
1997 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1998 N->setValueTypes(VTs);
1999 N->setOperands(Op1, Op2, Op3);
2001 CSEMap.InsertNode(N, IP); // Memoize the new node.
2006 /// getTargetNode - These are used for target selectors to create a new node
2007 /// with specified return type(s), target opcode, and operands.
2009 /// Note that getTargetNode returns the resultant node. If there is already a
2010 /// node of the specified opcode and operands, it returns that node instead of
2011 /// the current one.
2012 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2013 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2015 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2017 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2019 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2020 SDOperand Op1, SDOperand Op2) {
2021 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2023 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2024 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2025 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2027 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2028 const SDOperand *Ops, unsigned NumOps) {
2029 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
2031 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2032 MVT::ValueType VT2, SDOperand Op1) {
2033 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2034 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
2036 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2037 MVT::ValueType VT2, SDOperand Op1,
2039 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2040 SDOperand Ops[] = { Op1, Op2 };
2041 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
2043 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2044 MVT::ValueType VT2, SDOperand Op1,
2045 SDOperand Op2, SDOperand Op3) {
2046 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2047 SDOperand Ops[] = { Op1, Op2, Op3 };
2048 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
2050 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2052 const SDOperand *Ops, unsigned NumOps) {
2053 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2054 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
2056 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2057 MVT::ValueType VT2, MVT::ValueType VT3,
2058 SDOperand Op1, SDOperand Op2) {
2059 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
2060 SDOperand Ops[] = { Op1, Op2 };
2061 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
2063 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2064 MVT::ValueType VT2, MVT::ValueType VT3,
2065 const SDOperand *Ops, unsigned NumOps) {
2066 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
2067 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
2070 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2071 /// This can cause recursive merging of nodes in the DAG.
2073 /// This version assumes From/To have a single result value.
2075 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2076 std::vector<SDNode*> *Deleted) {
2077 SDNode *From = FromN.Val, *To = ToN.Val;
2078 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2079 "Cannot replace with this method!");
2080 assert(From != To && "Cannot replace uses of with self");
2082 while (!From->use_empty()) {
2083 // Process users until they are all gone.
2084 SDNode *U = *From->use_begin();
2086 // This node is about to morph, remove its old self from the CSE maps.
2087 RemoveNodeFromCSEMaps(U);
2089 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2091 if (I->Val == From) {
2092 From->removeUser(U);
2097 // Now that we have modified U, add it back to the CSE maps. If it already
2098 // exists there, recursively merge the results together.
2099 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2100 ReplaceAllUsesWith(U, Existing, Deleted);
2102 if (Deleted) Deleted->push_back(U);
2103 DeleteNodeNotInCSEMaps(U);
2108 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2109 /// This can cause recursive merging of nodes in the DAG.
2111 /// This version assumes From/To have matching types and numbers of result
2114 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2115 std::vector<SDNode*> *Deleted) {
2116 assert(From != To && "Cannot replace uses of with self");
2117 assert(From->getNumValues() == To->getNumValues() &&
2118 "Cannot use this version of ReplaceAllUsesWith!");
2119 if (From->getNumValues() == 1) { // If possible, use the faster version.
2120 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
2124 while (!From->use_empty()) {
2125 // Process users until they are all gone.
2126 SDNode *U = *From->use_begin();
2128 // This node is about to morph, remove its old self from the CSE maps.
2129 RemoveNodeFromCSEMaps(U);
2131 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2133 if (I->Val == From) {
2134 From->removeUser(U);
2139 // Now that we have modified U, add it back to the CSE maps. If it already
2140 // exists there, recursively merge the results together.
2141 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2142 ReplaceAllUsesWith(U, Existing, Deleted);
2144 if (Deleted) Deleted->push_back(U);
2145 DeleteNodeNotInCSEMaps(U);
2150 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2151 /// This can cause recursive merging of nodes in the DAG.
2153 /// This version can replace From with any result values. To must match the
2154 /// number and types of values returned by From.
2155 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
2156 const SDOperand *To,
2157 std::vector<SDNode*> *Deleted) {
2158 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
2159 // Degenerate case handled above.
2160 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
2164 while (!From->use_empty()) {
2165 // Process users until they are all gone.
2166 SDNode *U = *From->use_begin();
2168 // This node is about to morph, remove its old self from the CSE maps.
2169 RemoveNodeFromCSEMaps(U);
2171 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2173 if (I->Val == From) {
2174 const SDOperand &ToOp = To[I->ResNo];
2175 From->removeUser(U);
2177 ToOp.Val->addUser(U);
2180 // Now that we have modified U, add it back to the CSE maps. If it already
2181 // exists there, recursively merge the results together.
2182 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2183 ReplaceAllUsesWith(U, Existing, Deleted);
2185 if (Deleted) Deleted->push_back(U);
2186 DeleteNodeNotInCSEMaps(U);
2191 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2192 /// uses of other values produced by From.Val alone. The Deleted vector is
2193 /// handled the same was as for ReplaceAllUsesWith.
2194 void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2195 std::vector<SDNode*> &Deleted) {
2196 assert(From != To && "Cannot replace a value with itself");
2197 // Handle the simple, trivial, case efficiently.
2198 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2199 ReplaceAllUsesWith(From, To, &Deleted);
2203 // Get all of the users in a nice, deterministically ordered, uniqued set.
2204 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2206 while (!Users.empty()) {
2207 // We know that this user uses some value of From. If it is the right
2208 // value, update it.
2209 SDNode *User = Users.back();
2212 for (SDOperand *Op = User->OperandList,
2213 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2215 // Okay, we know this user needs to be updated. Remove its old self
2216 // from the CSE maps.
2217 RemoveNodeFromCSEMaps(User);
2219 // Update all operands that match "From".
2220 for (; Op != E; ++Op) {
2222 From.Val->removeUser(User);
2224 To.Val->addUser(User);
2228 // Now that we have modified User, add it back to the CSE maps. If it
2229 // already exists there, recursively merge the results together.
2230 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2231 unsigned NumDeleted = Deleted.size();
2232 ReplaceAllUsesWith(User, Existing, &Deleted);
2234 // User is now dead.
2235 Deleted.push_back(User);
2236 DeleteNodeNotInCSEMaps(User);
2238 // We have to be careful here, because ReplaceAllUsesWith could have
2239 // deleted a user of From, which means there may be dangling pointers
2240 // in the "Users" setvector. Scan over the deleted node pointers and
2241 // remove them from the setvector.
2242 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2243 Users.remove(Deleted[i]);
2245 break; // Exit the operand scanning loop.
2252 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2253 /// their allnodes order. It returns the maximum id.
2254 unsigned SelectionDAG::AssignNodeIds() {
2256 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2263 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
2264 /// based on their topological order. It returns the maximum id and a vector
2265 /// of the SDNodes* in assigned order by reference.
2266 unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
2267 unsigned DAGSize = AllNodes.size();
2268 std::vector<unsigned> InDegree(DAGSize);
2269 std::vector<SDNode*> Sources;
2271 // Use a two pass approach to avoid using a std::map which is slow.
2273 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2276 unsigned Degree = N->use_size();
2277 InDegree[N->getNodeId()] = Degree;
2279 Sources.push_back(N);
2283 while (!Sources.empty()) {
2284 SDNode *N = Sources.back();
2286 TopOrder.push_back(N);
2287 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2289 unsigned Degree = --InDegree[P->getNodeId()];
2291 Sources.push_back(P);
2295 // Second pass, assign the actual topological order as node ids.
2297 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2299 (*TI)->setNodeId(Id++);
2306 //===----------------------------------------------------------------------===//
2308 //===----------------------------------------------------------------------===//
2310 // Out-of-line virtual method to give class a home.
2311 void SDNode::ANCHOR() {
2314 /// getValueTypeList - Return a pointer to the specified value type.
2316 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2317 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2322 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2323 /// indicated value. This method ignores uses of other values defined by this
2325 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
2326 assert(Value < getNumValues() && "Bad value!");
2328 // If there is only one value, this is easy.
2329 if (getNumValues() == 1)
2330 return use_size() == NUses;
2331 if (Uses.size() < NUses) return false;
2333 SDOperand TheValue(const_cast<SDNode *>(this), Value);
2335 std::set<SDNode*> UsersHandled;
2337 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
2339 if (User->getNumOperands() == 1 ||
2340 UsersHandled.insert(User).second) // First time we've seen this?
2341 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2342 if (User->getOperand(i) == TheValue) {
2344 return false; // too many uses
2349 // Found exactly the right number of uses?
2354 // isOnlyUse - Return true if this node is the only use of N.
2355 bool SDNode::isOnlyUse(SDNode *N) const {
2357 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2368 // isOperand - Return true if this node is an operand of N.
2369 bool SDOperand::isOperand(SDNode *N) const {
2370 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2371 if (*this == N->getOperand(i))
2376 bool SDNode::isOperand(SDNode *N) const {
2377 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2378 if (this == N->OperandList[i].Val)
2383 uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
2384 assert(Num < NumOperands && "Invalid child # of SDNode!");
2385 return cast<ConstantSDNode>(OperandList[Num])->getValue();
2388 const char *SDNode::getOperationName(const SelectionDAG *G) const {
2389 switch (getOpcode()) {
2391 if (getOpcode() < ISD::BUILTIN_OP_END)
2392 return "<<Unknown DAG Node>>";
2395 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
2396 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2397 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
2399 TargetLowering &TLI = G->getTargetLoweringInfo();
2401 TLI.getTargetNodeName(getOpcode());
2402 if (Name) return Name;
2405 return "<<Unknown Target Node>>";
2408 case ISD::PCMARKER: return "PCMarker";
2409 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
2410 case ISD::SRCVALUE: return "SrcValue";
2411 case ISD::EntryToken: return "EntryToken";
2412 case ISD::TokenFactor: return "TokenFactor";
2413 case ISD::AssertSext: return "AssertSext";
2414 case ISD::AssertZext: return "AssertZext";
2416 case ISD::STRING: return "String";
2417 case ISD::BasicBlock: return "BasicBlock";
2418 case ISD::VALUETYPE: return "ValueType";
2419 case ISD::Register: return "Register";
2421 case ISD::Constant: return "Constant";
2422 case ISD::ConstantFP: return "ConstantFP";
2423 case ISD::GlobalAddress: return "GlobalAddress";
2424 case ISD::FrameIndex: return "FrameIndex";
2425 case ISD::JumpTable: return "JumpTable";
2426 case ISD::JumpTableRelocBase: return "JumpTableRelocBase";
2427 case ISD::ConstantPool: return "ConstantPool";
2428 case ISD::ExternalSymbol: return "ExternalSymbol";
2429 case ISD::INTRINSIC_WO_CHAIN: {
2430 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2431 return Intrinsic::getName((Intrinsic::ID)IID);
2433 case ISD::INTRINSIC_VOID:
2434 case ISD::INTRINSIC_W_CHAIN: {
2435 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
2436 return Intrinsic::getName((Intrinsic::ID)IID);
2439 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
2440 case ISD::TargetConstant: return "TargetConstant";
2441 case ISD::TargetConstantFP:return "TargetConstantFP";
2442 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2443 case ISD::TargetFrameIndex: return "TargetFrameIndex";
2444 case ISD::TargetJumpTable: return "TargetJumpTable";
2445 case ISD::TargetConstantPool: return "TargetConstantPool";
2446 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
2448 case ISD::CopyToReg: return "CopyToReg";
2449 case ISD::CopyFromReg: return "CopyFromReg";
2450 case ISD::UNDEF: return "undef";
2451 case ISD::MERGE_VALUES: return "mergevalues";
2452 case ISD::INLINEASM: return "inlineasm";
2453 case ISD::HANDLENODE: return "handlenode";
2454 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
2455 case ISD::CALL: return "call";
2458 case ISD::FABS: return "fabs";
2459 case ISD::FNEG: return "fneg";
2460 case ISD::FSQRT: return "fsqrt";
2461 case ISD::FSIN: return "fsin";
2462 case ISD::FCOS: return "fcos";
2463 case ISD::FPOWI: return "fpowi";
2466 case ISD::ADD: return "add";
2467 case ISD::SUB: return "sub";
2468 case ISD::MUL: return "mul";
2469 case ISD::MULHU: return "mulhu";
2470 case ISD::MULHS: return "mulhs";
2471 case ISD::SDIV: return "sdiv";
2472 case ISD::UDIV: return "udiv";
2473 case ISD::SREM: return "srem";
2474 case ISD::UREM: return "urem";
2475 case ISD::AND: return "and";
2476 case ISD::OR: return "or";
2477 case ISD::XOR: return "xor";
2478 case ISD::SHL: return "shl";
2479 case ISD::SRA: return "sra";
2480 case ISD::SRL: return "srl";
2481 case ISD::ROTL: return "rotl";
2482 case ISD::ROTR: return "rotr";
2483 case ISD::FADD: return "fadd";
2484 case ISD::FSUB: return "fsub";
2485 case ISD::FMUL: return "fmul";
2486 case ISD::FDIV: return "fdiv";
2487 case ISD::FREM: return "frem";
2488 case ISD::FCOPYSIGN: return "fcopysign";
2489 case ISD::VADD: return "vadd";
2490 case ISD::VSUB: return "vsub";
2491 case ISD::VMUL: return "vmul";
2492 case ISD::VSDIV: return "vsdiv";
2493 case ISD::VUDIV: return "vudiv";
2494 case ISD::VAND: return "vand";
2495 case ISD::VOR: return "vor";
2496 case ISD::VXOR: return "vxor";
2498 case ISD::SETCC: return "setcc";
2499 case ISD::SELECT: return "select";
2500 case ISD::SELECT_CC: return "select_cc";
2501 case ISD::VSELECT: return "vselect";
2502 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2503 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2504 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
2505 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
2506 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2507 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2508 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2509 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2510 case ISD::VBIT_CONVERT: return "vbit_convert";
2511 case ISD::ADDC: return "addc";
2512 case ISD::ADDE: return "adde";
2513 case ISD::SUBC: return "subc";
2514 case ISD::SUBE: return "sube";
2515 case ISD::SHL_PARTS: return "shl_parts";
2516 case ISD::SRA_PARTS: return "sra_parts";
2517 case ISD::SRL_PARTS: return "srl_parts";
2519 // Conversion operators.
2520 case ISD::SIGN_EXTEND: return "sign_extend";
2521 case ISD::ZERO_EXTEND: return "zero_extend";
2522 case ISD::ANY_EXTEND: return "any_extend";
2523 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
2524 case ISD::TRUNCATE: return "truncate";
2525 case ISD::FP_ROUND: return "fp_round";
2526 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
2527 case ISD::FP_EXTEND: return "fp_extend";
2529 case ISD::SINT_TO_FP: return "sint_to_fp";
2530 case ISD::UINT_TO_FP: return "uint_to_fp";
2531 case ISD::FP_TO_SINT: return "fp_to_sint";
2532 case ISD::FP_TO_UINT: return "fp_to_uint";
2533 case ISD::BIT_CONVERT: return "bit_convert";
2535 // Control flow instructions
2536 case ISD::BR: return "br";
2537 case ISD::BRIND: return "brind";
2538 case ISD::BRCOND: return "brcond";
2539 case ISD::BR_CC: return "br_cc";
2540 case ISD::RET: return "ret";
2541 case ISD::CALLSEQ_START: return "callseq_start";
2542 case ISD::CALLSEQ_END: return "callseq_end";
2545 case ISD::LOAD: return "load";
2546 case ISD::STORE: return "store";
2547 case ISD::VLOAD: return "vload";
2548 case ISD::LOADX: return "loadx";
2549 case ISD::TRUNCSTORE: return "truncstore";
2550 case ISD::VAARG: return "vaarg";
2551 case ISD::VACOPY: return "vacopy";
2552 case ISD::VAEND: return "vaend";
2553 case ISD::VASTART: return "vastart";
2554 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2555 case ISD::EXTRACT_ELEMENT: return "extract_element";
2556 case ISD::BUILD_PAIR: return "build_pair";
2557 case ISD::STACKSAVE: return "stacksave";
2558 case ISD::STACKRESTORE: return "stackrestore";
2560 // Block memory operations.
2561 case ISD::MEMSET: return "memset";
2562 case ISD::MEMCPY: return "memcpy";
2563 case ISD::MEMMOVE: return "memmove";
2566 case ISD::BSWAP: return "bswap";
2567 case ISD::CTPOP: return "ctpop";
2568 case ISD::CTTZ: return "cttz";
2569 case ISD::CTLZ: return "ctlz";
2572 case ISD::LOCATION: return "location";
2573 case ISD::DEBUG_LOC: return "debug_loc";
2574 case ISD::DEBUG_LABEL: return "debug_label";
2577 switch (cast<CondCodeSDNode>(this)->get()) {
2578 default: assert(0 && "Unknown setcc condition!");
2579 case ISD::SETOEQ: return "setoeq";
2580 case ISD::SETOGT: return "setogt";
2581 case ISD::SETOGE: return "setoge";
2582 case ISD::SETOLT: return "setolt";
2583 case ISD::SETOLE: return "setole";
2584 case ISD::SETONE: return "setone";
2586 case ISD::SETO: return "seto";
2587 case ISD::SETUO: return "setuo";
2588 case ISD::SETUEQ: return "setue";
2589 case ISD::SETUGT: return "setugt";
2590 case ISD::SETUGE: return "setuge";
2591 case ISD::SETULT: return "setult";
2592 case ISD::SETULE: return "setule";
2593 case ISD::SETUNE: return "setune";
2595 case ISD::SETEQ: return "seteq";
2596 case ISD::SETGT: return "setgt";
2597 case ISD::SETGE: return "setge";
2598 case ISD::SETLT: return "setlt";
2599 case ISD::SETLE: return "setle";
2600 case ISD::SETNE: return "setne";
2605 void SDNode::dump() const { dump(0); }
2606 void SDNode::dump(const SelectionDAG *G) const {
2607 std::cerr << (void*)this << ": ";
2609 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2610 if (i) std::cerr << ",";
2611 if (getValueType(i) == MVT::Other)
2614 std::cerr << MVT::getValueTypeString(getValueType(i));
2616 std::cerr << " = " << getOperationName(G);
2619 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2620 if (i) std::cerr << ", ";
2621 std::cerr << (void*)getOperand(i).Val;
2622 if (unsigned RN = getOperand(i).ResNo)
2623 std::cerr << ":" << RN;
2626 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2627 std::cerr << "<" << CSDN->getValue() << ">";
2628 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2629 std::cerr << "<" << CSDN->getValue() << ">";
2630 } else if (const GlobalAddressSDNode *GADN =
2631 dyn_cast<GlobalAddressSDNode>(this)) {
2632 int offset = GADN->getOffset();
2634 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
2636 std::cerr << " + " << offset;
2638 std::cerr << " " << offset;
2639 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
2640 std::cerr << "<" << FIDN->getIndex() << ">";
2641 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
2642 int offset = CP->getOffset();
2643 if (CP->isMachineConstantPoolEntry())
2644 std::cerr << "<" << *CP->getMachineCPVal() << ">";
2646 std::cerr << "<" << *CP->getConstVal() << ">";
2648 std::cerr << " + " << offset;
2650 std::cerr << " " << offset;
2651 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
2653 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2655 std::cerr << LBB->getName() << " ";
2656 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
2657 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
2658 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2659 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2661 std::cerr << " #" << R->getReg();
2663 } else if (const ExternalSymbolSDNode *ES =
2664 dyn_cast<ExternalSymbolSDNode>(this)) {
2665 std::cerr << "'" << ES->getSymbol() << "'";
2666 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2668 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2670 std::cerr << "<null:" << M->getOffset() << ">";
2671 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2672 std::cerr << ":" << getValueTypeString(N->getVT());
2676 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
2677 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2678 if (N->getOperand(i).Val->hasOneUse())
2679 DumpNodes(N->getOperand(i).Val, indent+2, G);
2681 std::cerr << "\n" << std::string(indent+2, ' ')
2682 << (void*)N->getOperand(i).Val << ": <multiple use>";
2685 std::cerr << "\n" << std::string(indent, ' ');
2689 void SelectionDAG::dump() const {
2690 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
2691 std::vector<const SDNode*> Nodes;
2692 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2696 std::sort(Nodes.begin(), Nodes.end());
2698 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2699 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
2700 DumpNodes(Nodes[i], 2, this);
2703 DumpNodes(getRoot().Val, 2, this);
2705 std::cerr << "\n\n";
2708 const Type *ConstantPoolSDNode::getType() const {
2709 if (isMachineConstantPoolEntry())
2710 return Val.MachineCPVal->getType();
2711 return Val.ConstVal->getType();