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 /// makeVTList - Return an instance of the SDVTList struct initialized with the
35 /// specified members.
36 static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
37 SDVTList Res = {VTs, NumVTs};
41 // isInvertibleForFree - Return true if there is no cost to emitting the logical
42 // inverse of this node.
43 static bool isInvertibleForFree(SDOperand N) {
44 if (isa<ConstantSDNode>(N.Val)) return true;
45 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
50 //===----------------------------------------------------------------------===//
51 // ConstantFPSDNode Class
52 //===----------------------------------------------------------------------===//
54 /// isExactlyValue - We don't rely on operator== working on double values, as
55 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
56 /// As such, this method can be used to do an exact bit-for-bit comparison of
57 /// two floating point values.
58 bool ConstantFPSDNode::isExactlyValue(double V) const {
59 return DoubleToBits(V) == DoubleToBits(Value);
62 //===----------------------------------------------------------------------===//
64 //===----------------------------------------------------------------------===//
66 /// isBuildVectorAllOnes - Return true if the specified node is a
67 /// BUILD_VECTOR where all of the elements are ~0 or undef.
68 bool ISD::isBuildVectorAllOnes(const SDNode *N) {
69 // Look through a bit convert.
70 if (N->getOpcode() == ISD::BIT_CONVERT)
71 N = N->getOperand(0).Val;
73 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
75 unsigned i = 0, e = N->getNumOperands();
77 // Skip over all of the undef values.
78 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
81 // Do not accept an all-undef vector.
82 if (i == e) return false;
84 // Do not accept build_vectors that aren't all constants or which have non-~0
86 SDOperand NotZero = N->getOperand(i);
87 if (isa<ConstantSDNode>(NotZero)) {
88 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
90 } else if (isa<ConstantFPSDNode>(NotZero)) {
91 MVT::ValueType VT = NotZero.getValueType();
93 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
97 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
104 // Okay, we have at least one ~0 value, check to see if the rest match or are
106 for (++i; i != e; ++i)
107 if (N->getOperand(i) != NotZero &&
108 N->getOperand(i).getOpcode() != ISD::UNDEF)
114 /// isBuildVectorAllZeros - Return true if the specified node is a
115 /// BUILD_VECTOR where all of the elements are 0 or undef.
116 bool ISD::isBuildVectorAllZeros(const SDNode *N) {
117 // Look through a bit convert.
118 if (N->getOpcode() == ISD::BIT_CONVERT)
119 N = N->getOperand(0).Val;
121 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
123 unsigned i = 0, e = N->getNumOperands();
125 // Skip over all of the undef values.
126 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
129 // Do not accept an all-undef vector.
130 if (i == e) return false;
132 // Do not accept build_vectors that aren't all constants or which have non-~0
134 SDOperand Zero = N->getOperand(i);
135 if (isa<ConstantSDNode>(Zero)) {
136 if (!cast<ConstantSDNode>(Zero)->isNullValue())
138 } else if (isa<ConstantFPSDNode>(Zero)) {
139 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
144 // Okay, we have at least one ~0 value, check to see if the rest match or are
146 for (++i; i != e; ++i)
147 if (N->getOperand(i) != Zero &&
148 N->getOperand(i).getOpcode() != ISD::UNDEF)
153 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
154 /// when given the operation for (X op Y).
155 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
156 // To perform this operation, we just need to swap the L and G bits of the
158 unsigned OldL = (Operation >> 2) & 1;
159 unsigned OldG = (Operation >> 1) & 1;
160 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
161 (OldL << 1) | // New G bit
162 (OldG << 2)); // New L bit.
165 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
166 /// 'op' is a valid SetCC operation.
167 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
168 unsigned Operation = Op;
170 Operation ^= 7; // Flip L, G, E bits, but not U.
172 Operation ^= 15; // Flip all of the condition bits.
173 if (Operation > ISD::SETTRUE2)
174 Operation &= ~8; // Don't let N and U bits get set.
175 return ISD::CondCode(Operation);
179 /// isSignedOp - For an integer comparison, return 1 if the comparison is a
180 /// signed operation and 2 if the result is an unsigned comparison. Return zero
181 /// if the operation does not depend on the sign of the input (setne and seteq).
182 static int isSignedOp(ISD::CondCode Opcode) {
184 default: assert(0 && "Illegal integer setcc operation!");
186 case ISD::SETNE: return 0;
190 case ISD::SETGE: return 1;
194 case ISD::SETUGE: return 2;
198 /// getSetCCOrOperation - Return the result of a logical OR between different
199 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
200 /// returns SETCC_INVALID if it is not possible to represent the resultant
202 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
204 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
205 // Cannot fold a signed integer setcc with an unsigned integer setcc.
206 return ISD::SETCC_INVALID;
208 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
210 // If the N and U bits get set then the resultant comparison DOES suddenly
211 // care about orderedness, and is true when ordered.
212 if (Op > ISD::SETTRUE2)
213 Op &= ~16; // Clear the U bit if the N bit is set.
215 // Canonicalize illegal integer setcc's.
216 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
219 return ISD::CondCode(Op);
222 /// getSetCCAndOperation - Return the result of a logical AND between different
223 /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
224 /// function returns zero if it is not possible to represent the resultant
226 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
228 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
229 // Cannot fold a signed setcc with an unsigned setcc.
230 return ISD::SETCC_INVALID;
232 // Combine all of the condition bits.
233 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
235 // Canonicalize illegal integer setcc's.
239 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
240 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
241 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
242 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
249 const TargetMachine &SelectionDAG::getTarget() const {
250 return TLI.getTargetMachine();
253 //===----------------------------------------------------------------------===//
254 // SelectionDAG Class
255 //===----------------------------------------------------------------------===//
257 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
259 void SelectionDAG::RemoveDeadNodes() {
260 // Create a dummy node (which is not added to allnodes), that adds a reference
261 // to the root node, preventing it from being deleted.
262 HandleSDNode Dummy(getRoot());
264 SmallVector<SDNode*, 128> DeadNodes;
266 // Add all obviously-dead nodes to the DeadNodes worklist.
267 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
269 DeadNodes.push_back(I);
271 // Process the worklist, deleting the nodes and adding their uses to the
273 while (!DeadNodes.empty()) {
274 SDNode *N = DeadNodes.back();
275 DeadNodes.pop_back();
277 // Take the node out of the appropriate CSE map.
278 RemoveNodeFromCSEMaps(N);
280 // Next, brutally remove the operand list. This is safe to do, as there are
281 // no cycles in the graph.
282 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
283 SDNode *Operand = I->Val;
284 Operand->removeUser(N);
286 // Now that we removed this operand, see if there are no uses of it left.
287 if (Operand->use_empty())
288 DeadNodes.push_back(Operand);
290 delete[] N->OperandList;
294 // Finally, remove N itself.
298 // If the root changed (e.g. it was a dead load, update the root).
299 setRoot(Dummy.getValue());
302 void SelectionDAG::DeleteNode(SDNode *N) {
303 assert(N->use_empty() && "Cannot delete a node that is not dead!");
305 // First take this out of the appropriate CSE map.
306 RemoveNodeFromCSEMaps(N);
308 // Finally, remove uses due to operands of this node, remove from the
309 // AllNodes list, and delete the node.
310 DeleteNodeNotInCSEMaps(N);
313 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
315 // Remove it from the AllNodes list.
318 // Drop all of the operands and decrement used nodes use counts.
319 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
320 I->Val->removeUser(N);
321 delete[] N->OperandList;
328 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
329 /// correspond to it. This is useful when we're about to delete or repurpose
330 /// the node. We don't want future request for structurally identical nodes
331 /// to return N anymore.
332 void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
334 switch (N->getOpcode()) {
335 case ISD::HANDLENODE: return; // noop.
337 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
340 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
341 "Cond code doesn't exist!");
342 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
343 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
345 case ISD::ExternalSymbol:
346 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
348 case ISD::TargetExternalSymbol:
350 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
353 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
354 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
357 // Remove it from the CSE Map.
358 Erased = CSEMap.RemoveNode(N);
362 // Verify that the node was actually in one of the CSE maps, unless it has a
363 // flag result (which cannot be CSE'd) or is one of the special cases that are
364 // not subject to CSE.
365 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
366 !N->isTargetOpcode()) {
369 assert(0 && "Node is not in map!");
374 /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
375 /// has been taken out and modified in some way. If the specified node already
376 /// exists in the CSE maps, do not modify the maps, but return the existing node
377 /// instead. If it doesn't exist, add it and return null.
379 SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
380 assert(N->getNumOperands() && "This is a leaf node!");
381 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
382 return 0; // Never add these nodes.
384 // Check that remaining values produced are not flags.
385 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
386 if (N->getValueType(i) == MVT::Flag)
387 return 0; // Never CSE anything that produces a flag.
389 SDNode *New = CSEMap.GetOrInsertNode(N);
390 if (New != N) return New; // Node already existed.
394 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
395 /// were replaced with those specified. If this node is never memoized,
396 /// return null, otherwise return a pointer to the slot it would take. If a
397 /// node already exists with these operands, the slot will be non-null.
398 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
400 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
401 return 0; // Never add these nodes.
403 // Check that remaining values produced are not flags.
404 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
405 if (N->getValueType(i) == MVT::Flag)
406 return 0; // Never CSE anything that produces a flag.
408 SelectionDAGCSEMap::NodeID ID;
409 ID.SetOpcode(N->getOpcode());
410 ID.SetValueTypes(N->getVTList());
412 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
415 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
416 /// were replaced with those specified. If this node is never memoized,
417 /// return null, otherwise return a pointer to the slot it would take. If a
418 /// node already exists with these operands, the slot will be non-null.
419 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
420 SDOperand Op1, SDOperand Op2,
422 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
423 return 0; // Never add these nodes.
425 // Check that remaining values produced are not flags.
426 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
427 if (N->getValueType(i) == MVT::Flag)
428 return 0; // Never CSE anything that produces a flag.
430 SelectionDAGCSEMap::NodeID ID;
431 ID.SetOpcode(N->getOpcode());
432 ID.SetValueTypes(N->getVTList());
433 ID.SetOperands(Op1, Op2);
434 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
438 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
439 /// were replaced with those specified. If this node is never memoized,
440 /// return null, otherwise return a pointer to the slot it would take. If a
441 /// node already exists with these operands, the slot will be non-null.
442 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
443 const SDOperand *Ops,unsigned NumOps,
445 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
446 return 0; // Never add these nodes.
448 // Check that remaining values produced are not flags.
449 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
450 if (N->getValueType(i) == MVT::Flag)
451 return 0; // Never CSE anything that produces a flag.
453 SelectionDAGCSEMap::NodeID ID;
454 ID.SetOpcode(N->getOpcode());
455 ID.SetValueTypes(N->getVTList());
456 ID.SetOperands(Ops, NumOps);
457 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
461 SelectionDAG::~SelectionDAG() {
462 while (!AllNodes.empty()) {
463 SDNode *N = AllNodes.begin();
464 N->SetNextInBucket(0);
465 delete [] N->OperandList;
468 AllNodes.pop_front();
472 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
473 if (Op.getValueType() == VT) return Op;
474 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
475 return getNode(ISD::AND, Op.getValueType(), Op,
476 getConstant(Imm, Op.getValueType()));
479 SDOperand SelectionDAG::getString(const std::string &Val) {
480 StringSDNode *&N = StringNodes[Val];
482 N = new StringSDNode(Val);
483 AllNodes.push_back(N);
485 return SDOperand(N, 0);
488 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
489 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
490 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
492 // Mask out any bits that are not valid for this constant.
493 Val &= MVT::getIntVTBitMask(VT);
495 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
496 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
499 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
500 return SDOperand(E, 0);
501 SDNode *N = new ConstantSDNode(isT, Val, VT);
502 CSEMap.InsertNode(N, IP);
503 AllNodes.push_back(N);
504 return SDOperand(N, 0);
508 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
510 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
512 Val = (float)Val; // Mask out extra precision.
514 // Do the map lookup using the actual bit pattern for the floating point
515 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
516 // we don't have issues with SNANs.
517 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
518 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
519 ID.AddInteger(DoubleToBits(Val));
521 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
522 return SDOperand(E, 0);
523 SDNode *N = new ConstantFPSDNode(isTarget, Val, VT);
524 CSEMap.InsertNode(N, IP);
525 AllNodes.push_back(N);
526 return SDOperand(N, 0);
529 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
530 MVT::ValueType VT, int Offset,
532 unsigned Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
533 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
535 ID.AddInteger(Offset);
537 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
538 return SDOperand(E, 0);
539 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
540 CSEMap.InsertNode(N, IP);
541 AllNodes.push_back(N);
542 return SDOperand(N, 0);
545 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
547 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
548 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
551 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
552 return SDOperand(E, 0);
553 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
554 CSEMap.InsertNode(N, IP);
555 AllNodes.push_back(N);
556 return SDOperand(N, 0);
559 SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
560 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
561 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
564 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
565 return SDOperand(E, 0);
566 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
567 CSEMap.InsertNode(N, IP);
568 AllNodes.push_back(N);
569 return SDOperand(N, 0);
572 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
573 unsigned Alignment, int Offset,
575 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
576 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
577 ID.AddInteger(Alignment);
578 ID.AddInteger(Offset);
581 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
582 return SDOperand(E, 0);
583 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
584 CSEMap.InsertNode(N, IP);
585 AllNodes.push_back(N);
586 return SDOperand(N, 0);
590 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
591 SelectionDAGCSEMap::NodeID ID(ISD::BasicBlock, getVTList(MVT::Other));
594 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
595 return SDOperand(E, 0);
596 SDNode *N = new BasicBlockSDNode(MBB);
597 CSEMap.InsertNode(N, IP);
598 AllNodes.push_back(N);
599 return SDOperand(N, 0);
602 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
603 if ((unsigned)VT >= ValueTypeNodes.size())
604 ValueTypeNodes.resize(VT+1);
605 if (ValueTypeNodes[VT] == 0) {
606 ValueTypeNodes[VT] = new VTSDNode(VT);
607 AllNodes.push_back(ValueTypeNodes[VT]);
610 return SDOperand(ValueTypeNodes[VT], 0);
613 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
614 SDNode *&N = ExternalSymbols[Sym];
615 if (N) return SDOperand(N, 0);
616 N = new ExternalSymbolSDNode(false, Sym, VT);
617 AllNodes.push_back(N);
618 return SDOperand(N, 0);
621 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
623 SDNode *&N = TargetExternalSymbols[Sym];
624 if (N) return SDOperand(N, 0);
625 N = new ExternalSymbolSDNode(true, Sym, VT);
626 AllNodes.push_back(N);
627 return SDOperand(N, 0);
630 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
631 if ((unsigned)Cond >= CondCodeNodes.size())
632 CondCodeNodes.resize(Cond+1);
634 if (CondCodeNodes[Cond] == 0) {
635 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
636 AllNodes.push_back(CondCodeNodes[Cond]);
638 return SDOperand(CondCodeNodes[Cond], 0);
641 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
642 SelectionDAGCSEMap::NodeID ID(ISD::Register, getVTList(VT));
643 ID.AddInteger(RegNo);
645 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
646 return SDOperand(E, 0);
647 SDNode *N = new RegisterSDNode(RegNo, VT);
648 CSEMap.InsertNode(N, IP);
649 AllNodes.push_back(N);
650 return SDOperand(N, 0);
653 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
654 assert((!V || isa<PointerType>(V->getType())) &&
655 "SrcValue is not a pointer?");
657 SelectionDAGCSEMap::NodeID ID(ISD::SRCVALUE, getVTList(MVT::Other));
659 ID.AddInteger(Offset);
661 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
662 return SDOperand(E, 0);
663 SDNode *N = new SrcValueSDNode(V, Offset);
664 CSEMap.InsertNode(N, IP);
665 AllNodes.push_back(N);
666 return SDOperand(N, 0);
669 SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
670 SDOperand N2, ISD::CondCode Cond) {
671 // These setcc operations always fold.
675 case ISD::SETFALSE2: return getConstant(0, VT);
677 case ISD::SETTRUE2: return getConstant(1, VT);
689 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
693 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
694 uint64_t C2 = N2C->getValue();
695 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
696 uint64_t C1 = N1C->getValue();
698 // Sign extend the operands if required
699 if (ISD::isSignedIntSetCC(Cond)) {
700 C1 = N1C->getSignExtended();
701 C2 = N2C->getSignExtended();
705 default: assert(0 && "Unknown integer setcc!");
706 case ISD::SETEQ: return getConstant(C1 == C2, VT);
707 case ISD::SETNE: return getConstant(C1 != C2, VT);
708 case ISD::SETULT: return getConstant(C1 < C2, VT);
709 case ISD::SETUGT: return getConstant(C1 > C2, VT);
710 case ISD::SETULE: return getConstant(C1 <= C2, VT);
711 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
712 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
713 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
714 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
715 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
718 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
719 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
720 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
722 // If the comparison constant has bits in the upper part, the
723 // zero-extended value could never match.
724 if (C2 & (~0ULL << InSize)) {
725 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
729 case ISD::SETEQ: return getConstant(0, VT);
732 case ISD::SETNE: return getConstant(1, VT);
735 // True if the sign bit of C2 is set.
736 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
739 // True if the sign bit of C2 isn't set.
740 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
746 // Otherwise, we can perform the comparison with the low bits.
754 return getSetCC(VT, N1.getOperand(0),
755 getConstant(C2, N1.getOperand(0).getValueType()),
758 break; // todo, be more careful with signed comparisons
760 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
761 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
762 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
763 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
764 MVT::ValueType ExtDstTy = N1.getValueType();
765 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
767 // If the extended part has any inconsistent bits, it cannot ever
768 // compare equal. In other words, they have to be all ones or all
771 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
772 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
773 return getConstant(Cond == ISD::SETNE, VT);
775 // Otherwise, make this a use of a zext.
776 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
777 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
781 uint64_t MinVal, MaxVal;
782 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
783 if (ISD::isSignedIntSetCC(Cond)) {
784 MinVal = 1ULL << (OperandBitSize-1);
785 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
786 MaxVal = ~0ULL >> (65-OperandBitSize);
791 MaxVal = ~0ULL >> (64-OperandBitSize);
794 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
795 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
796 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
797 --C2; // X >= C1 --> X > (C1-1)
798 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
799 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
802 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
803 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
804 ++C2; // X <= C1 --> X < (C1+1)
805 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
806 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
809 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
810 return getConstant(0, VT); // X < MIN --> false
812 // Canonicalize setgt X, Min --> setne X, Min
813 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
814 return getSetCC(VT, N1, N2, ISD::SETNE);
816 // If we have setult X, 1, turn it into seteq X, 0
817 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
818 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
820 // If we have setugt X, Max-1, turn it into seteq X, Max
821 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
822 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
825 // If we have "setcc X, C1", check to see if we can shrink the immediate
828 // SETUGT X, SINTMAX -> SETLT X, 0
829 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
830 C2 == (~0ULL >> (65-OperandBitSize)))
831 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
833 // FIXME: Implement the rest of these.
836 // Fold bit comparisons when we can.
837 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
838 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
839 if (ConstantSDNode *AndRHS =
840 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
841 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
842 // Perform the xform if the AND RHS is a single bit.
843 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
844 return getNode(ISD::SRL, VT, N1,
845 getConstant(Log2_64(AndRHS->getValue()),
846 TLI.getShiftAmountTy()));
848 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
849 // (X & 8) == 8 --> (X & 8) >> 3
850 // Perform the xform if C2 is a single bit.
851 if ((C2 & (C2-1)) == 0) {
852 return getNode(ISD::SRL, VT, N1,
853 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
858 } else if (isa<ConstantSDNode>(N1.Val)) {
859 // Ensure that the constant occurs on the RHS.
860 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
863 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
864 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
865 double C1 = N1C->getValue(), C2 = N2C->getValue();
868 default: break; // FIXME: Implement the rest of these!
869 case ISD::SETEQ: return getConstant(C1 == C2, VT);
870 case ISD::SETNE: return getConstant(C1 != C2, VT);
871 case ISD::SETLT: return getConstant(C1 < C2, VT);
872 case ISD::SETGT: return getConstant(C1 > C2, VT);
873 case ISD::SETLE: return getConstant(C1 <= C2, VT);
874 case ISD::SETGE: return getConstant(C1 >= C2, VT);
877 // Ensure that the constant occurs on the RHS.
878 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
881 // Could not fold it.
885 /// getNode - Gets or creates the specified node.
887 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
888 SelectionDAGCSEMap::NodeID ID(Opcode, getVTList(VT));
890 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
891 return SDOperand(E, 0);
892 SDNode *N = new SDNode(Opcode, VT);
893 CSEMap.InsertNode(N, IP);
895 AllNodes.push_back(N);
896 return SDOperand(N, 0);
899 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
902 // Constant fold unary operations with an integer constant operand.
903 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
904 uint64_t Val = C->getValue();
907 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
908 case ISD::ANY_EXTEND:
909 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
910 case ISD::TRUNCATE: return getConstant(Val, VT);
911 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
912 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
913 case ISD::BIT_CONVERT:
914 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
915 return getConstantFP(BitsToFloat(Val), VT);
916 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
917 return getConstantFP(BitsToDouble(Val), VT);
921 default: assert(0 && "Invalid bswap!"); break;
922 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
923 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
924 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
929 default: assert(0 && "Invalid ctpop!"); break;
930 case MVT::i1: return getConstant(Val != 0, VT);
932 Tmp1 = (unsigned)Val & 0xFF;
933 return getConstant(CountPopulation_32(Tmp1), VT);
935 Tmp1 = (unsigned)Val & 0xFFFF;
936 return getConstant(CountPopulation_32(Tmp1), VT);
938 return getConstant(CountPopulation_32((unsigned)Val), VT);
940 return getConstant(CountPopulation_64(Val), VT);
944 default: assert(0 && "Invalid ctlz!"); break;
945 case MVT::i1: return getConstant(Val == 0, VT);
947 Tmp1 = (unsigned)Val & 0xFF;
948 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
950 Tmp1 = (unsigned)Val & 0xFFFF;
951 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
953 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
955 return getConstant(CountLeadingZeros_64(Val), VT);
959 default: assert(0 && "Invalid cttz!"); break;
960 case MVT::i1: return getConstant(Val == 0, VT);
962 Tmp1 = (unsigned)Val | 0x100;
963 return getConstant(CountTrailingZeros_32(Tmp1), VT);
965 Tmp1 = (unsigned)Val | 0x10000;
966 return getConstant(CountTrailingZeros_32(Tmp1), VT);
968 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
970 return getConstant(CountTrailingZeros_64(Val), VT);
975 // Constant fold unary operations with an floating point constant operand.
976 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
979 return getConstantFP(-C->getValue(), VT);
981 return getConstantFP(fabs(C->getValue()), VT);
984 return getConstantFP(C->getValue(), VT);
985 case ISD::FP_TO_SINT:
986 return getConstant((int64_t)C->getValue(), VT);
987 case ISD::FP_TO_UINT:
988 return getConstant((uint64_t)C->getValue(), VT);
989 case ISD::BIT_CONVERT:
990 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
991 return getConstant(FloatToBits(C->getValue()), VT);
992 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
993 return getConstant(DoubleToBits(C->getValue()), VT);
997 unsigned OpOpcode = Operand.Val->getOpcode();
999 case ISD::TokenFactor:
1000 return Operand; // Factor of one node? No factor.
1001 case ISD::SIGN_EXTEND:
1002 if (Operand.getValueType() == VT) return Operand; // noop extension
1003 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
1004 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1005 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1007 case ISD::ZERO_EXTEND:
1008 if (Operand.getValueType() == VT) return Operand; // noop extension
1009 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
1010 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1011 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1013 case ISD::ANY_EXTEND:
1014 if (Operand.getValueType() == VT) return Operand; // noop extension
1015 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
1016 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1017 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1018 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1021 if (Operand.getValueType() == VT) return Operand; // noop truncate
1022 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
1023 if (OpOpcode == ISD::TRUNCATE)
1024 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1025 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1026 OpOpcode == ISD::ANY_EXTEND) {
1027 // If the source is smaller than the dest, we still need an extend.
1028 if (Operand.Val->getOperand(0).getValueType() < VT)
1029 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1030 else if (Operand.Val->getOperand(0).getValueType() > VT)
1031 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1033 return Operand.Val->getOperand(0);
1036 case ISD::BIT_CONVERT:
1037 // Basic sanity checking.
1038 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
1039 && "Cannot BIT_CONVERT between two different types!");
1040 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1041 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1042 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1043 if (OpOpcode == ISD::UNDEF)
1044 return getNode(ISD::UNDEF, VT);
1046 case ISD::SCALAR_TO_VECTOR:
1047 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1048 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1049 "Illegal SCALAR_TO_VECTOR node!");
1052 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1053 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1054 Operand.Val->getOperand(0));
1055 if (OpOpcode == ISD::FNEG) // --X -> X
1056 return Operand.Val->getOperand(0);
1059 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1060 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1065 SDVTList VTs = getVTList(VT);
1066 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1067 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Operand);
1069 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1070 return SDOperand(E, 0);
1071 N = new SDNode(Opcode, Operand);
1072 N->setValueTypes(VTs);
1073 CSEMap.InsertNode(N, IP);
1075 N = new SDNode(Opcode, Operand);
1076 N->setValueTypes(VTs);
1078 AllNodes.push_back(N);
1079 return SDOperand(N, 0);
1084 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1085 SDOperand N1, SDOperand N2) {
1088 case ISD::TokenFactor:
1089 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1090 N2.getValueType() == MVT::Other && "Invalid token factor!");
1099 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1106 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1113 assert(N1.getValueType() == N2.getValueType() &&
1114 N1.getValueType() == VT && "Binary operator types must match!");
1116 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1117 assert(N1.getValueType() == VT &&
1118 MVT::isFloatingPoint(N1.getValueType()) &&
1119 MVT::isFloatingPoint(N2.getValueType()) &&
1120 "Invalid FCOPYSIGN!");
1127 assert(VT == N1.getValueType() &&
1128 "Shift operators return type must be the same as their first arg");
1129 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1130 VT != MVT::i1 && "Shifts only work on integers");
1132 case ISD::FP_ROUND_INREG: {
1133 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1134 assert(VT == N1.getValueType() && "Not an inreg round!");
1135 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1136 "Cannot FP_ROUND_INREG integer types");
1137 assert(EVT <= VT && "Not rounding down!");
1140 case ISD::AssertSext:
1141 case ISD::AssertZext:
1142 case ISD::SIGN_EXTEND_INREG: {
1143 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1144 assert(VT == N1.getValueType() && "Not an inreg extend!");
1145 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1146 "Cannot *_EXTEND_INREG FP types");
1147 assert(EVT <= VT && "Not extending!");
1154 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1155 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1157 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1158 int64_t Val = N1C->getValue();
1159 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1160 Val <<= 64-FromBits;
1161 Val >>= 64-FromBits;
1162 return getConstant(Val, VT);
1166 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1168 case ISD::ADD: return getConstant(C1 + C2, VT);
1169 case ISD::SUB: return getConstant(C1 - C2, VT);
1170 case ISD::MUL: return getConstant(C1 * C2, VT);
1172 if (C2) return getConstant(C1 / C2, VT);
1175 if (C2) return getConstant(C1 % C2, VT);
1178 if (C2) return getConstant(N1C->getSignExtended() /
1179 N2C->getSignExtended(), VT);
1182 if (C2) return getConstant(N1C->getSignExtended() %
1183 N2C->getSignExtended(), VT);
1185 case ISD::AND : return getConstant(C1 & C2, VT);
1186 case ISD::OR : return getConstant(C1 | C2, VT);
1187 case ISD::XOR : return getConstant(C1 ^ C2, VT);
1188 case ISD::SHL : return getConstant(C1 << C2, VT);
1189 case ISD::SRL : return getConstant(C1 >> C2, VT);
1190 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
1192 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1195 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1199 } else { // Cannonicalize constant to RHS if commutative
1200 if (isCommutativeBinOp(Opcode)) {
1201 std::swap(N1C, N2C);
1207 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1208 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1211 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1213 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1214 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1215 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1217 if (C2) return getConstantFP(C1 / C2, VT);
1220 if (C2) return getConstantFP(fmod(C1, C2), VT);
1222 case ISD::FCOPYSIGN: {
1233 if (u2.I < 0) // Sign bit of RHS set?
1234 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1236 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1237 return getConstantFP(u1.F, VT);
1241 } else { // Cannonicalize constant to RHS if commutative
1242 if (isCommutativeBinOp(Opcode)) {
1243 std::swap(N1CFP, N2CFP);
1249 // Canonicalize an UNDEF to the RHS, even over a constant.
1250 if (N1.getOpcode() == ISD::UNDEF) {
1251 if (isCommutativeBinOp(Opcode)) {
1255 case ISD::FP_ROUND_INREG:
1256 case ISD::SIGN_EXTEND_INREG:
1262 return N1; // fold op(undef, arg2) -> undef
1269 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1274 // Fold a bunch of operators when the RHS is undef.
1275 if (N2.getOpcode() == ISD::UNDEF) {
1289 return N2; // fold op(arg1, undef) -> undef
1294 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1296 return getConstant(MVT::getIntVTBitMask(VT), VT);
1302 // Finally, fold operations that do not require constants.
1304 case ISD::FP_ROUND_INREG:
1305 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1307 case ISD::SIGN_EXTEND_INREG: {
1308 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1309 if (EVT == VT) return N1; // Not actually extending
1313 // FIXME: figure out how to safely handle things like
1314 // int foo(int x) { return 1 << (x & 255); }
1315 // int bar() { return foo(256); }
1320 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1321 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
1322 return getNode(Opcode, VT, N1, N2.getOperand(0));
1323 else if (N2.getOpcode() == ISD::AND)
1324 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1325 // If the and is only masking out bits that cannot effect the shift,
1326 // eliminate the and.
1327 unsigned NumBits = MVT::getSizeInBits(VT);
1328 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1329 return getNode(Opcode, VT, N1, N2.getOperand(0));
1335 // Memoize this node if possible.
1337 SDVTList VTs = getVTList(VT);
1338 if (VT != MVT::Flag) {
1339 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2);
1341 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1342 return SDOperand(E, 0);
1343 N = new SDNode(Opcode, N1, N2);
1344 N->setValueTypes(VTs);
1345 CSEMap.InsertNode(N, IP);
1347 N = new SDNode(Opcode, N1, N2);
1348 N->setValueTypes(VTs);
1351 AllNodes.push_back(N);
1352 return SDOperand(N, 0);
1355 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1356 SDOperand N1, SDOperand N2, SDOperand N3) {
1357 // Perform various simplifications.
1358 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1359 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1360 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1363 // Use SimplifySetCC to simplify SETCC's.
1364 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
1365 if (Simp.Val) return Simp;
1370 if (N1C->getValue())
1371 return N2; // select true, X, Y -> X
1373 return N3; // select false, X, Y -> Y
1375 if (N2 == N3) return N2; // select C, X, X -> X
1379 if (N2C->getValue()) // Unconditional branch
1380 return getNode(ISD::BR, MVT::Other, N1, N3);
1382 return N1; // Never-taken branch
1384 case ISD::VECTOR_SHUFFLE:
1385 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1386 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1387 N3.getOpcode() == ISD::BUILD_VECTOR &&
1388 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1389 "Illegal VECTOR_SHUFFLE node!");
1393 // Memoize node if it doesn't produce a flag.
1395 SDVTList VTs = getVTList(VT);
1396 if (VT != MVT::Flag) {
1397 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2, N3);
1399 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1400 return SDOperand(E, 0);
1401 N = new SDNode(Opcode, N1, N2, N3);
1402 N->setValueTypes(VTs);
1403 CSEMap.InsertNode(N, IP);
1405 N = new SDNode(Opcode, N1, N2, N3);
1406 N->setValueTypes(VTs);
1408 AllNodes.push_back(N);
1409 return SDOperand(N, 0);
1412 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1413 SDOperand N1, SDOperand N2, SDOperand N3,
1415 SDOperand Ops[] = { N1, N2, N3, N4 };
1416 return getNode(Opcode, VT, Ops, 4);
1419 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1420 SDOperand N1, SDOperand N2, SDOperand N3,
1421 SDOperand N4, SDOperand N5) {
1422 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1423 return getNode(Opcode, VT, Ops, 5);
1426 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1427 SDOperand Chain, SDOperand Ptr,
1429 SDVTList VTs = getVTList(VT, MVT::Other);
1431 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, SV);
1433 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1434 return SDOperand(E, 0);
1435 SDNode *N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1436 N->setValueTypes(VTs);
1437 CSEMap.InsertNode(N, IP);
1438 AllNodes.push_back(N);
1439 return SDOperand(N, 0);
1442 SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1443 SDOperand Chain, SDOperand Ptr,
1445 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1446 getValueType(EVT) };
1447 return getNode(ISD::VLOAD, getVTList(MVT::Vector, MVT::Other), Ops, 5);
1450 SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1451 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1452 MVT::ValueType EVT) {
1453 SDOperand Ops[] = { Chain, Ptr, SV, getValueType(EVT) };
1454 return getNode(Opcode, getVTList(VT, MVT::Other), Ops, 4);
1457 SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1458 SDOperand Chain, SDOperand Ptr,
1460 SDOperand Ops[] = { Chain, Ptr, SV };
1461 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
1464 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1465 const SDOperand *Ops, unsigned NumOps) {
1467 case 0: return getNode(Opcode, VT);
1468 case 1: return getNode(Opcode, VT, Ops[0]);
1469 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1470 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
1476 case ISD::TRUNCSTORE: {
1477 assert(NumOps == 5 && "TRUNCSTORE takes 5 operands!");
1478 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1479 #if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1480 // If this is a truncating store of a constant, convert to the desired type
1481 // and store it instead.
1482 if (isa<Constant>(Ops[0])) {
1483 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1484 if (isa<Constant>(Op))
1487 // Also for ConstantFP?
1489 if (Ops[0].getValueType() == EVT) // Normal store?
1490 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1491 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1492 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1493 "Can't do FP-INT conversion!");
1496 case ISD::SELECT_CC: {
1497 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
1498 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1499 "LHS and RHS of condition must have same type!");
1500 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1501 "True and False arms of SelectCC must have same type!");
1502 assert(Ops[2].getValueType() == VT &&
1503 "select_cc node must be of same type as true and false value!");
1507 assert(NumOps == 5 && "BR_CC takes 5 operands!");
1508 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1509 "LHS/RHS of comparison should match types!");
1516 SDVTList VTs = getVTList(VT);
1517 if (VT != MVT::Flag) {
1518 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Ops, NumOps);
1520 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1521 return SDOperand(E, 0);
1522 N = new SDNode(Opcode, Ops, NumOps);
1523 N->setValueTypes(VTs);
1524 CSEMap.InsertNode(N, IP);
1526 N = new SDNode(Opcode, Ops, NumOps);
1527 N->setValueTypes(VTs);
1529 AllNodes.push_back(N);
1530 return SDOperand(N, 0);
1533 SDOperand SelectionDAG::getNode(unsigned Opcode,
1534 std::vector<MVT::ValueType> &ResultTys,
1535 const SDOperand *Ops, unsigned NumOps) {
1536 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
1540 SDOperand SelectionDAG::getNode(unsigned Opcode,
1541 const MVT::ValueType *VTs, unsigned NumVTs,
1542 const SDOperand *Ops, unsigned NumOps) {
1544 return getNode(Opcode, VTs[0], Ops, NumOps);
1545 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
1548 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
1549 const SDOperand *Ops, unsigned NumOps) {
1550 if (VTList.NumVTs == 1)
1551 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
1556 case ISD::ZEXTLOAD: {
1557 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1558 assert(NumOps == 4 && VTList.NumVTs == 2 && "Bad *EXTLOAD!");
1559 // If they are asking for an extending load from/to the same thing, return a
1561 if (VTList.VTs[0] == EVT)
1562 return getLoad(VTList.VTs[0], Ops[0], Ops[1], Ops[2]);
1563 if (MVT::isVector(VTList.VTs[0])) {
1564 assert(EVT == MVT::getVectorBaseType(VTList.VTs[0]) &&
1565 "Invalid vector extload!");
1567 assert(EVT < VTList.VTs[0] &&
1568 "Should only be an extending load, not truncating!");
1570 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(VTList.VTs[0])) &&
1571 "Cannot sign/zero extend a FP/Vector load!");
1572 assert(MVT::isInteger(VTList.VTs[0]) == MVT::isInteger(EVT) &&
1573 "Cannot convert from FP to Int or Int -> FP!");
1577 // FIXME: figure out how to safely handle things like
1578 // int foo(int x) { return 1 << (x & 255); }
1579 // int bar() { return foo(256); }
1581 case ISD::SRA_PARTS:
1582 case ISD::SRL_PARTS:
1583 case ISD::SHL_PARTS:
1584 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1585 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
1586 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1587 else if (N3.getOpcode() == ISD::AND)
1588 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1589 // If the and is only masking out bits that cannot effect the shift,
1590 // eliminate the and.
1591 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1592 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1593 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1599 // Memoize the node unless it returns a flag.
1601 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
1602 SelectionDAGCSEMap::NodeID ID;
1603 ID.SetOpcode(Opcode);
1604 ID.SetValueTypes(VTList);
1605 ID.SetOperands(&Ops[0], NumOps);
1607 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1608 return SDOperand(E, 0);
1609 N = new SDNode(Opcode, Ops, NumOps);
1610 N->setValueTypes(VTList);
1611 CSEMap.InsertNode(N, IP);
1613 N = new SDNode(Opcode, Ops, NumOps);
1614 N->setValueTypes(VTList);
1616 AllNodes.push_back(N);
1617 return SDOperand(N, 0);
1620 SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
1621 return makeVTList(SDNode::getValueTypeList(VT), 1);
1624 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
1625 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1626 E = VTList.end(); I != E; ++I) {
1627 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
1628 return makeVTList(&(*I)[0], 2);
1630 std::vector<MVT::ValueType> V;
1633 VTList.push_front(V);
1634 return makeVTList(&(*VTList.begin())[0], 2);
1636 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
1637 MVT::ValueType VT3) {
1638 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1639 E = VTList.end(); I != E; ++I) {
1640 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
1642 return makeVTList(&(*I)[0], 3);
1644 std::vector<MVT::ValueType> V;
1648 VTList.push_front(V);
1649 return makeVTList(&(*VTList.begin())[0], 3);
1652 SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
1654 case 0: assert(0 && "Cannot have nodes without results!");
1655 case 1: return makeVTList(SDNode::getValueTypeList(VTs[0]), 1);
1656 case 2: return getVTList(VTs[0], VTs[1]);
1657 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
1661 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1662 E = VTList.end(); I != E; ++I) {
1663 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
1665 bool NoMatch = false;
1666 for (unsigned i = 2; i != NumVTs; ++i)
1667 if (VTs[i] != (*I)[i]) {
1672 return makeVTList(&*I->begin(), NumVTs);
1675 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
1676 return makeVTList(&*VTList.begin()->begin(), NumVTs);
1680 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1681 /// specified operands. If the resultant node already exists in the DAG,
1682 /// this does not modify the specified node, instead it returns the node that
1683 /// already exists. If the resultant node does not exist in the DAG, the
1684 /// input node is returned. As a degenerate case, if you specify the same
1685 /// input operands as the node already has, the input node is returned.
1686 SDOperand SelectionDAG::
1687 UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1688 SDNode *N = InN.Val;
1689 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1691 // Check to see if there is no change.
1692 if (Op == N->getOperand(0)) return InN;
1694 // See if the modified node already exists.
1695 void *InsertPos = 0;
1696 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1697 return SDOperand(Existing, InN.ResNo);
1699 // Nope it doesn't. Remove the node from it's current place in the maps.
1701 RemoveNodeFromCSEMaps(N);
1703 // Now we update the operands.
1704 N->OperandList[0].Val->removeUser(N);
1706 N->OperandList[0] = Op;
1708 // If this gets put into a CSE map, add it.
1709 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1713 SDOperand SelectionDAG::
1714 UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1715 SDNode *N = InN.Val;
1716 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1718 // Check to see if there is no change.
1719 bool AnyChange = false;
1720 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1721 return InN; // No operands changed, just return the input node.
1723 // See if the modified node already exists.
1724 void *InsertPos = 0;
1725 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1726 return SDOperand(Existing, InN.ResNo);
1728 // Nope it doesn't. Remove the node from it's current place in the maps.
1730 RemoveNodeFromCSEMaps(N);
1732 // Now we update the operands.
1733 if (N->OperandList[0] != Op1) {
1734 N->OperandList[0].Val->removeUser(N);
1735 Op1.Val->addUser(N);
1736 N->OperandList[0] = Op1;
1738 if (N->OperandList[1] != Op2) {
1739 N->OperandList[1].Val->removeUser(N);
1740 Op2.Val->addUser(N);
1741 N->OperandList[1] = Op2;
1744 // If this gets put into a CSE map, add it.
1745 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1749 SDOperand SelectionDAG::
1750 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1751 SDOperand Ops[] = { Op1, Op2, Op3 };
1752 return UpdateNodeOperands(N, Ops, 3);
1755 SDOperand SelectionDAG::
1756 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1757 SDOperand Op3, SDOperand Op4) {
1758 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
1759 return UpdateNodeOperands(N, Ops, 4);
1762 SDOperand SelectionDAG::
1763 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1764 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1765 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
1766 return UpdateNodeOperands(N, Ops, 5);
1770 SDOperand SelectionDAG::
1771 UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
1772 SDNode *N = InN.Val;
1773 assert(N->getNumOperands() == NumOps &&
1774 "Update with wrong number of operands");
1776 // Check to see if there is no change.
1777 bool AnyChange = false;
1778 for (unsigned i = 0; i != NumOps; ++i) {
1779 if (Ops[i] != N->getOperand(i)) {
1785 // No operands changed, just return the input node.
1786 if (!AnyChange) return InN;
1788 // See if the modified node already exists.
1789 void *InsertPos = 0;
1790 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
1791 return SDOperand(Existing, InN.ResNo);
1793 // Nope it doesn't. Remove the node from it's current place in the maps.
1795 RemoveNodeFromCSEMaps(N);
1797 // Now we update the operands.
1798 for (unsigned i = 0; i != NumOps; ++i) {
1799 if (N->OperandList[i] != Ops[i]) {
1800 N->OperandList[i].Val->removeUser(N);
1801 Ops[i].Val->addUser(N);
1802 N->OperandList[i] = Ops[i];
1806 // If this gets put into a CSE map, add it.
1807 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1814 /// SelectNodeTo - These are used for target selectors to *mutate* the
1815 /// specified node to have the specified return type, Target opcode, and
1816 /// operands. Note that target opcodes are stored as
1817 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1819 /// Note that SelectNodeTo returns the resultant node. If there is already a
1820 /// node of the specified opcode and operands, it returns that node instead of
1821 /// the current one.
1822 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1823 MVT::ValueType VT) {
1824 SDVTList VTs = getVTList(VT);
1825 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1827 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1830 RemoveNodeFromCSEMaps(N);
1832 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1833 N->setValueTypes(VTs);
1835 CSEMap.InsertNode(N, IP);
1839 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1840 MVT::ValueType VT, SDOperand Op1) {
1841 // If an identical node already exists, use it.
1842 SDVTList VTs = getVTList(VT);
1843 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
1845 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1848 RemoveNodeFromCSEMaps(N);
1849 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1850 N->setValueTypes(VTs);
1851 N->setOperands(Op1);
1852 CSEMap.InsertNode(N, IP);
1856 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1857 MVT::ValueType VT, SDOperand Op1,
1859 // If an identical node already exists, use it.
1860 SDVTList VTs = getVTList(VT);
1861 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1863 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1866 RemoveNodeFromCSEMaps(N);
1867 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1868 N->setValueTypes(VTs);
1869 N->setOperands(Op1, Op2);
1871 CSEMap.InsertNode(N, IP); // Memoize the new node.
1875 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1876 MVT::ValueType VT, SDOperand Op1,
1877 SDOperand Op2, SDOperand Op3) {
1878 // If an identical node already exists, use it.
1879 SDVTList VTs = getVTList(VT);
1880 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
1883 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1886 RemoveNodeFromCSEMaps(N);
1887 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1888 N->setValueTypes(VTs);
1889 N->setOperands(Op1, Op2, Op3);
1891 CSEMap.InsertNode(N, IP); // Memoize the new node.
1895 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1896 MVT::ValueType VT, const SDOperand *Ops,
1898 // If an identical node already exists, use it.
1899 SDVTList VTs = getVTList(VT);
1900 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1901 for (unsigned i = 0; i != NumOps; ++i)
1902 ID.AddOperand(Ops[i]);
1904 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1907 RemoveNodeFromCSEMaps(N);
1908 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1909 N->setValueTypes(VTs);
1910 N->setOperands(Ops, NumOps);
1912 CSEMap.InsertNode(N, IP); // Memoize the new node.
1916 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1917 MVT::ValueType VT1, MVT::ValueType VT2,
1918 SDOperand Op1, SDOperand Op2) {
1919 SDVTList VTs = getVTList(VT1, VT2);
1920 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1922 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1925 RemoveNodeFromCSEMaps(N);
1926 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1927 N->setValueTypes(VTs);
1928 N->setOperands(Op1, Op2);
1930 CSEMap.InsertNode(N, IP); // Memoize the new node.
1934 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1935 MVT::ValueType VT1, MVT::ValueType VT2,
1936 SDOperand Op1, SDOperand Op2,
1938 // If an identical node already exists, use it.
1939 SDVTList VTs = getVTList(VT1, VT2);
1940 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
1943 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1946 RemoveNodeFromCSEMaps(N);
1947 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1948 N->setValueTypes(VTs);
1949 N->setOperands(Op1, Op2, Op3);
1951 CSEMap.InsertNode(N, IP); // Memoize the new node.
1956 /// getTargetNode - These are used for target selectors to create a new node
1957 /// with specified return type(s), target opcode, and operands.
1959 /// Note that getTargetNode returns the resultant node. If there is already a
1960 /// node of the specified opcode and operands, it returns that node instead of
1961 /// the current one.
1962 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
1963 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
1965 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
1967 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
1969 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
1970 SDOperand Op1, SDOperand Op2) {
1971 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
1973 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
1974 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1975 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
1977 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
1978 const SDOperand *Ops, unsigned NumOps) {
1979 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
1981 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
1982 MVT::ValueType VT2, SDOperand Op1) {
1983 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
1984 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
1986 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
1987 MVT::ValueType VT2, SDOperand Op1,
1989 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
1990 SDOperand Ops[] = { Op1, Op2 };
1991 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
1993 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
1994 MVT::ValueType VT2, SDOperand Op1,
1995 SDOperand Op2, SDOperand Op3) {
1996 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
1997 SDOperand Ops[] = { Op1, Op2, Op3 };
1998 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
2000 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2002 const SDOperand *Ops, unsigned NumOps) {
2003 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2004 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
2006 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2007 MVT::ValueType VT2, MVT::ValueType VT3,
2008 SDOperand Op1, SDOperand Op2) {
2009 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
2010 SDOperand Ops[] = { Op1, Op2 };
2011 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
2013 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2014 MVT::ValueType VT2, MVT::ValueType VT3,
2015 const SDOperand *Ops, unsigned NumOps) {
2016 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
2017 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
2020 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2021 /// This can cause recursive merging of nodes in the DAG.
2023 /// This version assumes From/To have a single result value.
2025 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2026 std::vector<SDNode*> *Deleted) {
2027 SDNode *From = FromN.Val, *To = ToN.Val;
2028 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2029 "Cannot replace with this method!");
2030 assert(From != To && "Cannot replace uses of with self");
2032 while (!From->use_empty()) {
2033 // Process users until they are all gone.
2034 SDNode *U = *From->use_begin();
2036 // This node is about to morph, remove its old self from the CSE maps.
2037 RemoveNodeFromCSEMaps(U);
2039 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2041 if (I->Val == From) {
2042 From->removeUser(U);
2047 // Now that we have modified U, add it back to the CSE maps. If it already
2048 // exists there, recursively merge the results together.
2049 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2050 ReplaceAllUsesWith(U, Existing, Deleted);
2052 if (Deleted) Deleted->push_back(U);
2053 DeleteNodeNotInCSEMaps(U);
2058 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2059 /// This can cause recursive merging of nodes in the DAG.
2061 /// This version assumes From/To have matching types and numbers of result
2064 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2065 std::vector<SDNode*> *Deleted) {
2066 assert(From != To && "Cannot replace uses of with self");
2067 assert(From->getNumValues() == To->getNumValues() &&
2068 "Cannot use this version of ReplaceAllUsesWith!");
2069 if (From->getNumValues() == 1) { // If possible, use the faster version.
2070 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
2074 while (!From->use_empty()) {
2075 // Process users until they are all gone.
2076 SDNode *U = *From->use_begin();
2078 // This node is about to morph, remove its old self from the CSE maps.
2079 RemoveNodeFromCSEMaps(U);
2081 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2083 if (I->Val == From) {
2084 From->removeUser(U);
2089 // Now that we have modified U, add it back to the CSE maps. If it already
2090 // exists there, recursively merge the results together.
2091 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2092 ReplaceAllUsesWith(U, Existing, Deleted);
2094 if (Deleted) Deleted->push_back(U);
2095 DeleteNodeNotInCSEMaps(U);
2100 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2101 /// This can cause recursive merging of nodes in the DAG.
2103 /// This version can replace From with any result values. To must match the
2104 /// number and types of values returned by From.
2105 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
2106 const SDOperand *To,
2107 std::vector<SDNode*> *Deleted) {
2108 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
2109 // Degenerate case handled above.
2110 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
2114 while (!From->use_empty()) {
2115 // Process users until they are all gone.
2116 SDNode *U = *From->use_begin();
2118 // This node is about to morph, remove its old self from the CSE maps.
2119 RemoveNodeFromCSEMaps(U);
2121 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2123 if (I->Val == From) {
2124 const SDOperand &ToOp = To[I->ResNo];
2125 From->removeUser(U);
2127 ToOp.Val->addUser(U);
2130 // Now that we have modified U, add it back to the CSE maps. If it already
2131 // exists there, recursively merge the results together.
2132 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2133 ReplaceAllUsesWith(U, Existing, Deleted);
2135 if (Deleted) Deleted->push_back(U);
2136 DeleteNodeNotInCSEMaps(U);
2141 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2142 /// uses of other values produced by From.Val alone. The Deleted vector is
2143 /// handled the same was as for ReplaceAllUsesWith.
2144 void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2145 std::vector<SDNode*> &Deleted) {
2146 assert(From != To && "Cannot replace a value with itself");
2147 // Handle the simple, trivial, case efficiently.
2148 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2149 ReplaceAllUsesWith(From, To, &Deleted);
2153 // Get all of the users in a nice, deterministically ordered, uniqued set.
2154 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2156 while (!Users.empty()) {
2157 // We know that this user uses some value of From. If it is the right
2158 // value, update it.
2159 SDNode *User = Users.back();
2162 for (SDOperand *Op = User->OperandList,
2163 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2165 // Okay, we know this user needs to be updated. Remove its old self
2166 // from the CSE maps.
2167 RemoveNodeFromCSEMaps(User);
2169 // Update all operands that match "From".
2170 for (; Op != E; ++Op) {
2172 From.Val->removeUser(User);
2174 To.Val->addUser(User);
2178 // Now that we have modified User, add it back to the CSE maps. If it
2179 // already exists there, recursively merge the results together.
2180 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2181 unsigned NumDeleted = Deleted.size();
2182 ReplaceAllUsesWith(User, Existing, &Deleted);
2184 // User is now dead.
2185 Deleted.push_back(User);
2186 DeleteNodeNotInCSEMaps(User);
2188 // We have to be careful here, because ReplaceAllUsesWith could have
2189 // deleted a user of From, which means there may be dangling pointers
2190 // in the "Users" setvector. Scan over the deleted node pointers and
2191 // remove them from the setvector.
2192 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2193 Users.remove(Deleted[i]);
2195 break; // Exit the operand scanning loop.
2202 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2203 /// their allnodes order. It returns the maximum id.
2204 unsigned SelectionDAG::AssignNodeIds() {
2206 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2213 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
2214 /// based on their topological order. It returns the maximum id and a vector
2215 /// of the SDNodes* in assigned order by reference.
2216 unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
2217 unsigned DAGSize = AllNodes.size();
2218 std::vector<unsigned> InDegree(DAGSize);
2219 std::vector<SDNode*> Sources;
2221 // Use a two pass approach to avoid using a std::map which is slow.
2223 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2226 unsigned Degree = N->use_size();
2227 InDegree[N->getNodeId()] = Degree;
2229 Sources.push_back(N);
2233 while (!Sources.empty()) {
2234 SDNode *N = Sources.back();
2236 TopOrder.push_back(N);
2237 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2239 unsigned Degree = --InDegree[P->getNodeId()];
2241 Sources.push_back(P);
2245 // Second pass, assign the actual topological order as node ids.
2247 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2249 (*TI)->setNodeId(Id++);
2256 //===----------------------------------------------------------------------===//
2258 //===----------------------------------------------------------------------===//
2260 // Out-of-line virtual method to give class a home.
2261 void SDNode::ANCHOR() {
2264 /// getValueTypeList - Return a pointer to the specified value type.
2266 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2267 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2272 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2273 /// indicated value. This method ignores uses of other values defined by this
2275 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
2276 assert(Value < getNumValues() && "Bad value!");
2278 // If there is only one value, this is easy.
2279 if (getNumValues() == 1)
2280 return use_size() == NUses;
2281 if (Uses.size() < NUses) return false;
2283 SDOperand TheValue(const_cast<SDNode *>(this), Value);
2285 std::set<SDNode*> UsersHandled;
2287 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
2289 if (User->getNumOperands() == 1 ||
2290 UsersHandled.insert(User).second) // First time we've seen this?
2291 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2292 if (User->getOperand(i) == TheValue) {
2294 return false; // too many uses
2299 // Found exactly the right number of uses?
2304 // isOnlyUse - Return true if this node is the only use of N.
2305 bool SDNode::isOnlyUse(SDNode *N) const {
2307 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2318 // isOperand - Return true if this node is an operand of N.
2319 bool SDOperand::isOperand(SDNode *N) const {
2320 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2321 if (*this == N->getOperand(i))
2326 bool SDNode::isOperand(SDNode *N) const {
2327 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2328 if (this == N->OperandList[i].Val)
2333 const char *SDNode::getOperationName(const SelectionDAG *G) const {
2334 switch (getOpcode()) {
2336 if (getOpcode() < ISD::BUILTIN_OP_END)
2337 return "<<Unknown DAG Node>>";
2340 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
2341 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2342 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
2344 TargetLowering &TLI = G->getTargetLoweringInfo();
2346 TLI.getTargetNodeName(getOpcode());
2347 if (Name) return Name;
2350 return "<<Unknown Target Node>>";
2353 case ISD::PCMARKER: return "PCMarker";
2354 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
2355 case ISD::SRCVALUE: return "SrcValue";
2356 case ISD::EntryToken: return "EntryToken";
2357 case ISD::TokenFactor: return "TokenFactor";
2358 case ISD::AssertSext: return "AssertSext";
2359 case ISD::AssertZext: return "AssertZext";
2361 case ISD::STRING: return "String";
2362 case ISD::BasicBlock: return "BasicBlock";
2363 case ISD::VALUETYPE: return "ValueType";
2364 case ISD::Register: return "Register";
2366 case ISD::Constant: return "Constant";
2367 case ISD::ConstantFP: return "ConstantFP";
2368 case ISD::GlobalAddress: return "GlobalAddress";
2369 case ISD::FrameIndex: return "FrameIndex";
2370 case ISD::JumpTable: return "JumpTable";
2371 case ISD::ConstantPool: return "ConstantPool";
2372 case ISD::ExternalSymbol: return "ExternalSymbol";
2373 case ISD::INTRINSIC_WO_CHAIN: {
2374 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2375 return Intrinsic::getName((Intrinsic::ID)IID);
2377 case ISD::INTRINSIC_VOID:
2378 case ISD::INTRINSIC_W_CHAIN: {
2379 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
2380 return Intrinsic::getName((Intrinsic::ID)IID);
2383 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
2384 case ISD::TargetConstant: return "TargetConstant";
2385 case ISD::TargetConstantFP:return "TargetConstantFP";
2386 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2387 case ISD::TargetFrameIndex: return "TargetFrameIndex";
2388 case ISD::TargetJumpTable: return "TargetJumpTable";
2389 case ISD::TargetConstantPool: return "TargetConstantPool";
2390 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
2392 case ISD::CopyToReg: return "CopyToReg";
2393 case ISD::CopyFromReg: return "CopyFromReg";
2394 case ISD::UNDEF: return "undef";
2395 case ISD::MERGE_VALUES: return "mergevalues";
2396 case ISD::INLINEASM: return "inlineasm";
2397 case ISD::HANDLENODE: return "handlenode";
2398 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
2399 case ISD::CALL: return "call";
2402 case ISD::FABS: return "fabs";
2403 case ISD::FNEG: return "fneg";
2404 case ISD::FSQRT: return "fsqrt";
2405 case ISD::FSIN: return "fsin";
2406 case ISD::FCOS: return "fcos";
2409 case ISD::ADD: return "add";
2410 case ISD::SUB: return "sub";
2411 case ISD::MUL: return "mul";
2412 case ISD::MULHU: return "mulhu";
2413 case ISD::MULHS: return "mulhs";
2414 case ISD::SDIV: return "sdiv";
2415 case ISD::UDIV: return "udiv";
2416 case ISD::SREM: return "srem";
2417 case ISD::UREM: return "urem";
2418 case ISD::AND: return "and";
2419 case ISD::OR: return "or";
2420 case ISD::XOR: return "xor";
2421 case ISD::SHL: return "shl";
2422 case ISD::SRA: return "sra";
2423 case ISD::SRL: return "srl";
2424 case ISD::ROTL: return "rotl";
2425 case ISD::ROTR: return "rotr";
2426 case ISD::FADD: return "fadd";
2427 case ISD::FSUB: return "fsub";
2428 case ISD::FMUL: return "fmul";
2429 case ISD::FDIV: return "fdiv";
2430 case ISD::FREM: return "frem";
2431 case ISD::FCOPYSIGN: return "fcopysign";
2432 case ISD::VADD: return "vadd";
2433 case ISD::VSUB: return "vsub";
2434 case ISD::VMUL: return "vmul";
2435 case ISD::VSDIV: return "vsdiv";
2436 case ISD::VUDIV: return "vudiv";
2437 case ISD::VAND: return "vand";
2438 case ISD::VOR: return "vor";
2439 case ISD::VXOR: return "vxor";
2441 case ISD::SETCC: return "setcc";
2442 case ISD::SELECT: return "select";
2443 case ISD::SELECT_CC: return "select_cc";
2444 case ISD::VSELECT: return "vselect";
2445 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2446 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2447 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
2448 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
2449 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2450 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2451 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2452 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2453 case ISD::VBIT_CONVERT: return "vbit_convert";
2454 case ISD::ADDC: return "addc";
2455 case ISD::ADDE: return "adde";
2456 case ISD::SUBC: return "subc";
2457 case ISD::SUBE: return "sube";
2458 case ISD::SHL_PARTS: return "shl_parts";
2459 case ISD::SRA_PARTS: return "sra_parts";
2460 case ISD::SRL_PARTS: return "srl_parts";
2462 // Conversion operators.
2463 case ISD::SIGN_EXTEND: return "sign_extend";
2464 case ISD::ZERO_EXTEND: return "zero_extend";
2465 case ISD::ANY_EXTEND: return "any_extend";
2466 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
2467 case ISD::TRUNCATE: return "truncate";
2468 case ISD::FP_ROUND: return "fp_round";
2469 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
2470 case ISD::FP_EXTEND: return "fp_extend";
2472 case ISD::SINT_TO_FP: return "sint_to_fp";
2473 case ISD::UINT_TO_FP: return "uint_to_fp";
2474 case ISD::FP_TO_SINT: return "fp_to_sint";
2475 case ISD::FP_TO_UINT: return "fp_to_uint";
2476 case ISD::BIT_CONVERT: return "bit_convert";
2478 // Control flow instructions
2479 case ISD::BR: return "br";
2480 case ISD::BRIND: return "brind";
2481 case ISD::BRCOND: return "brcond";
2482 case ISD::BR_CC: return "br_cc";
2483 case ISD::RET: return "ret";
2484 case ISD::CALLSEQ_START: return "callseq_start";
2485 case ISD::CALLSEQ_END: return "callseq_end";
2488 case ISD::LOAD: return "load";
2489 case ISD::STORE: return "store";
2490 case ISD::VLOAD: return "vload";
2491 case ISD::EXTLOAD: return "extload";
2492 case ISD::SEXTLOAD: return "sextload";
2493 case ISD::ZEXTLOAD: return "zextload";
2494 case ISD::TRUNCSTORE: return "truncstore";
2495 case ISD::VAARG: return "vaarg";
2496 case ISD::VACOPY: return "vacopy";
2497 case ISD::VAEND: return "vaend";
2498 case ISD::VASTART: return "vastart";
2499 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2500 case ISD::EXTRACT_ELEMENT: return "extract_element";
2501 case ISD::BUILD_PAIR: return "build_pair";
2502 case ISD::STACKSAVE: return "stacksave";
2503 case ISD::STACKRESTORE: return "stackrestore";
2505 // Block memory operations.
2506 case ISD::MEMSET: return "memset";
2507 case ISD::MEMCPY: return "memcpy";
2508 case ISD::MEMMOVE: return "memmove";
2511 case ISD::BSWAP: return "bswap";
2512 case ISD::CTPOP: return "ctpop";
2513 case ISD::CTTZ: return "cttz";
2514 case ISD::CTLZ: return "ctlz";
2517 case ISD::LOCATION: return "location";
2518 case ISD::DEBUG_LOC: return "debug_loc";
2519 case ISD::DEBUG_LABEL: return "debug_label";
2522 switch (cast<CondCodeSDNode>(this)->get()) {
2523 default: assert(0 && "Unknown setcc condition!");
2524 case ISD::SETOEQ: return "setoeq";
2525 case ISD::SETOGT: return "setogt";
2526 case ISD::SETOGE: return "setoge";
2527 case ISD::SETOLT: return "setolt";
2528 case ISD::SETOLE: return "setole";
2529 case ISD::SETONE: return "setone";
2531 case ISD::SETO: return "seto";
2532 case ISD::SETUO: return "setuo";
2533 case ISD::SETUEQ: return "setue";
2534 case ISD::SETUGT: return "setugt";
2535 case ISD::SETUGE: return "setuge";
2536 case ISD::SETULT: return "setult";
2537 case ISD::SETULE: return "setule";
2538 case ISD::SETUNE: return "setune";
2540 case ISD::SETEQ: return "seteq";
2541 case ISD::SETGT: return "setgt";
2542 case ISD::SETGE: return "setge";
2543 case ISD::SETLT: return "setlt";
2544 case ISD::SETLE: return "setle";
2545 case ISD::SETNE: return "setne";
2550 void SDNode::dump() const { dump(0); }
2551 void SDNode::dump(const SelectionDAG *G) const {
2552 std::cerr << (void*)this << ": ";
2554 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2555 if (i) std::cerr << ",";
2556 if (getValueType(i) == MVT::Other)
2559 std::cerr << MVT::getValueTypeString(getValueType(i));
2561 std::cerr << " = " << getOperationName(G);
2564 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2565 if (i) std::cerr << ", ";
2566 std::cerr << (void*)getOperand(i).Val;
2567 if (unsigned RN = getOperand(i).ResNo)
2568 std::cerr << ":" << RN;
2571 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2572 std::cerr << "<" << CSDN->getValue() << ">";
2573 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2574 std::cerr << "<" << CSDN->getValue() << ">";
2575 } else if (const GlobalAddressSDNode *GADN =
2576 dyn_cast<GlobalAddressSDNode>(this)) {
2577 int offset = GADN->getOffset();
2579 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
2581 std::cerr << " + " << offset;
2583 std::cerr << " " << offset;
2584 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
2585 std::cerr << "<" << FIDN->getIndex() << ">";
2586 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
2587 int offset = CP->getOffset();
2588 std::cerr << "<" << *CP->get() << ">";
2590 std::cerr << " + " << offset;
2592 std::cerr << " " << offset;
2593 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
2595 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2597 std::cerr << LBB->getName() << " ";
2598 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
2599 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
2600 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2601 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2603 std::cerr << " #" << R->getReg();
2605 } else if (const ExternalSymbolSDNode *ES =
2606 dyn_cast<ExternalSymbolSDNode>(this)) {
2607 std::cerr << "'" << ES->getSymbol() << "'";
2608 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2610 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2612 std::cerr << "<null:" << M->getOffset() << ">";
2613 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2614 std::cerr << ":" << getValueTypeString(N->getVT());
2618 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
2619 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2620 if (N->getOperand(i).Val->hasOneUse())
2621 DumpNodes(N->getOperand(i).Val, indent+2, G);
2623 std::cerr << "\n" << std::string(indent+2, ' ')
2624 << (void*)N->getOperand(i).Val << ": <multiple use>";
2627 std::cerr << "\n" << std::string(indent, ' ');
2631 void SelectionDAG::dump() const {
2632 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
2633 std::vector<const SDNode*> Nodes;
2634 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2638 std::sort(Nodes.begin(), Nodes.end());
2640 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2641 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
2642 DumpNodes(Nodes[i], 2, this);
2645 DumpNodes(getRoot().Val, 2, this);
2647 std::cerr << "\n\n";