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 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
458 ID.AddInteger(LD->getAddressingMode());
459 ID.AddInteger(LD->getExtensionType());
460 ID.AddInteger(LD->getLoadedVT());
461 ID.AddPointer(LD->getSrcValue());
462 ID.AddInteger(LD->getSrcValueOffset());
463 ID.AddInteger(LD->getAlignment());
464 ID.AddInteger(LD->isVolatile());
466 ID.SetOperands(Ops, NumOps);
467 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
471 SelectionDAG::~SelectionDAG() {
472 while (!AllNodes.empty()) {
473 SDNode *N = AllNodes.begin();
474 N->SetNextInBucket(0);
475 delete [] N->OperandList;
478 AllNodes.pop_front();
482 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
483 if (Op.getValueType() == VT) return Op;
484 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
485 return getNode(ISD::AND, Op.getValueType(), Op,
486 getConstant(Imm, Op.getValueType()));
489 SDOperand SelectionDAG::getString(const std::string &Val) {
490 StringSDNode *&N = StringNodes[Val];
492 N = new StringSDNode(Val);
493 AllNodes.push_back(N);
495 return SDOperand(N, 0);
498 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
499 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
500 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
502 // Mask out any bits that are not valid for this constant.
503 Val &= MVT::getIntVTBitMask(VT);
505 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
506 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
509 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
510 return SDOperand(E, 0);
511 SDNode *N = new ConstantSDNode(isT, Val, VT);
512 CSEMap.InsertNode(N, IP);
513 AllNodes.push_back(N);
514 return SDOperand(N, 0);
518 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
520 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
522 Val = (float)Val; // Mask out extra precision.
524 // Do the map lookup using the actual bit pattern for the floating point
525 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
526 // we don't have issues with SNANs.
527 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
528 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
529 ID.AddInteger(DoubleToBits(Val));
531 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
532 return SDOperand(E, 0);
533 SDNode *N = new ConstantFPSDNode(isTarget, Val, VT);
534 CSEMap.InsertNode(N, IP);
535 AllNodes.push_back(N);
536 return SDOperand(N, 0);
539 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
540 MVT::ValueType VT, int Offset,
542 unsigned Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
543 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
545 ID.AddInteger(Offset);
547 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
548 return SDOperand(E, 0);
549 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
550 CSEMap.InsertNode(N, IP);
551 AllNodes.push_back(N);
552 return SDOperand(N, 0);
555 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
557 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
558 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
561 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
562 return SDOperand(E, 0);
563 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
564 CSEMap.InsertNode(N, IP);
565 AllNodes.push_back(N);
566 return SDOperand(N, 0);
569 SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
570 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
571 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
574 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
575 return SDOperand(E, 0);
576 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
577 CSEMap.InsertNode(N, IP);
578 AllNodes.push_back(N);
579 return SDOperand(N, 0);
582 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
583 unsigned Alignment, int Offset,
585 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
586 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
587 ID.AddInteger(Alignment);
588 ID.AddInteger(Offset);
591 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
592 return SDOperand(E, 0);
593 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
594 CSEMap.InsertNode(N, IP);
595 AllNodes.push_back(N);
596 return SDOperand(N, 0);
600 SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
602 unsigned Alignment, int Offset,
604 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
605 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
606 ID.AddInteger(Alignment);
607 ID.AddInteger(Offset);
608 C->AddSelectionDAGCSEId(&ID);
610 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
611 return SDOperand(E, 0);
612 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
613 CSEMap.InsertNode(N, IP);
614 AllNodes.push_back(N);
615 return SDOperand(N, 0);
619 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
620 SelectionDAGCSEMap::NodeID ID(ISD::BasicBlock, getVTList(MVT::Other));
623 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
624 return SDOperand(E, 0);
625 SDNode *N = new BasicBlockSDNode(MBB);
626 CSEMap.InsertNode(N, IP);
627 AllNodes.push_back(N);
628 return SDOperand(N, 0);
631 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
632 if ((unsigned)VT >= ValueTypeNodes.size())
633 ValueTypeNodes.resize(VT+1);
634 if (ValueTypeNodes[VT] == 0) {
635 ValueTypeNodes[VT] = new VTSDNode(VT);
636 AllNodes.push_back(ValueTypeNodes[VT]);
639 return SDOperand(ValueTypeNodes[VT], 0);
642 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
643 SDNode *&N = ExternalSymbols[Sym];
644 if (N) return SDOperand(N, 0);
645 N = new ExternalSymbolSDNode(false, Sym, VT);
646 AllNodes.push_back(N);
647 return SDOperand(N, 0);
650 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
652 SDNode *&N = TargetExternalSymbols[Sym];
653 if (N) return SDOperand(N, 0);
654 N = new ExternalSymbolSDNode(true, Sym, VT);
655 AllNodes.push_back(N);
656 return SDOperand(N, 0);
659 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
660 if ((unsigned)Cond >= CondCodeNodes.size())
661 CondCodeNodes.resize(Cond+1);
663 if (CondCodeNodes[Cond] == 0) {
664 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
665 AllNodes.push_back(CondCodeNodes[Cond]);
667 return SDOperand(CondCodeNodes[Cond], 0);
670 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
671 SelectionDAGCSEMap::NodeID ID(ISD::Register, getVTList(VT));
672 ID.AddInteger(RegNo);
674 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
675 return SDOperand(E, 0);
676 SDNode *N = new RegisterSDNode(RegNo, VT);
677 CSEMap.InsertNode(N, IP);
678 AllNodes.push_back(N);
679 return SDOperand(N, 0);
682 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
683 assert((!V || isa<PointerType>(V->getType())) &&
684 "SrcValue is not a pointer?");
686 SelectionDAGCSEMap::NodeID ID(ISD::SRCVALUE, getVTList(MVT::Other));
688 ID.AddInteger(Offset);
690 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
691 return SDOperand(E, 0);
692 SDNode *N = new SrcValueSDNode(V, Offset);
693 CSEMap.InsertNode(N, IP);
694 AllNodes.push_back(N);
695 return SDOperand(N, 0);
698 SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
699 SDOperand N2, ISD::CondCode Cond) {
700 // These setcc operations always fold.
704 case ISD::SETFALSE2: return getConstant(0, VT);
706 case ISD::SETTRUE2: return getConstant(1, VT);
718 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
722 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
723 uint64_t C2 = N2C->getValue();
724 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
725 uint64_t C1 = N1C->getValue();
727 // Sign extend the operands if required
728 if (ISD::isSignedIntSetCC(Cond)) {
729 C1 = N1C->getSignExtended();
730 C2 = N2C->getSignExtended();
734 default: assert(0 && "Unknown integer setcc!");
735 case ISD::SETEQ: return getConstant(C1 == C2, VT);
736 case ISD::SETNE: return getConstant(C1 != C2, VT);
737 case ISD::SETULT: return getConstant(C1 < C2, VT);
738 case ISD::SETUGT: return getConstant(C1 > C2, VT);
739 case ISD::SETULE: return getConstant(C1 <= C2, VT);
740 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
741 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
742 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
743 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
744 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
747 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
748 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
749 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
751 // If the comparison constant has bits in the upper part, the
752 // zero-extended value could never match.
753 if (C2 & (~0ULL << InSize)) {
754 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
758 case ISD::SETEQ: return getConstant(0, VT);
761 case ISD::SETNE: return getConstant(1, VT);
764 // True if the sign bit of C2 is set.
765 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
768 // True if the sign bit of C2 isn't set.
769 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
775 // Otherwise, we can perform the comparison with the low bits.
783 return getSetCC(VT, N1.getOperand(0),
784 getConstant(C2, N1.getOperand(0).getValueType()),
787 break; // todo, be more careful with signed comparisons
789 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
790 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
791 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
792 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
793 MVT::ValueType ExtDstTy = N1.getValueType();
794 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
796 // If the extended part has any inconsistent bits, it cannot ever
797 // compare equal. In other words, they have to be all ones or all
800 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
801 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
802 return getConstant(Cond == ISD::SETNE, VT);
804 // Otherwise, make this a use of a zext.
805 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
806 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
810 uint64_t MinVal, MaxVal;
811 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
812 if (ISD::isSignedIntSetCC(Cond)) {
813 MinVal = 1ULL << (OperandBitSize-1);
814 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
815 MaxVal = ~0ULL >> (65-OperandBitSize);
820 MaxVal = ~0ULL >> (64-OperandBitSize);
823 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
824 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
825 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
826 --C2; // X >= C1 --> X > (C1-1)
827 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
828 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
831 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
832 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
833 ++C2; // X <= C1 --> X < (C1+1)
834 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
835 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
838 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
839 return getConstant(0, VT); // X < MIN --> false
841 // Canonicalize setgt X, Min --> setne X, Min
842 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
843 return getSetCC(VT, N1, N2, ISD::SETNE);
845 // If we have setult X, 1, turn it into seteq X, 0
846 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
847 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
849 // If we have setugt X, Max-1, turn it into seteq X, Max
850 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
851 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
854 // If we have "setcc X, C1", check to see if we can shrink the immediate
857 // SETUGT X, SINTMAX -> SETLT X, 0
858 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
859 C2 == (~0ULL >> (65-OperandBitSize)))
860 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
862 // FIXME: Implement the rest of these.
865 // Fold bit comparisons when we can.
866 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
867 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
868 if (ConstantSDNode *AndRHS =
869 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
870 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
871 // Perform the xform if the AND RHS is a single bit.
872 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
873 return getNode(ISD::SRL, VT, N1,
874 getConstant(Log2_64(AndRHS->getValue()),
875 TLI.getShiftAmountTy()));
877 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
878 // (X & 8) == 8 --> (X & 8) >> 3
879 // Perform the xform if C2 is a single bit.
880 if ((C2 & (C2-1)) == 0) {
881 return getNode(ISD::SRL, VT, N1,
882 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
887 } else if (isa<ConstantSDNode>(N1.Val)) {
888 // Ensure that the constant occurs on the RHS.
889 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
892 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
893 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
894 double C1 = N1C->getValue(), C2 = N2C->getValue();
897 default: break; // FIXME: Implement the rest of these!
898 case ISD::SETEQ: return getConstant(C1 == C2, VT);
899 case ISD::SETNE: return getConstant(C1 != C2, VT);
900 case ISD::SETLT: return getConstant(C1 < C2, VT);
901 case ISD::SETGT: return getConstant(C1 > C2, VT);
902 case ISD::SETLE: return getConstant(C1 <= C2, VT);
903 case ISD::SETGE: return getConstant(C1 >= C2, VT);
906 // Ensure that the constant occurs on the RHS.
907 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
910 // Could not fold it.
914 /// getNode - Gets or creates the specified node.
916 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
917 SelectionDAGCSEMap::NodeID ID(Opcode, getVTList(VT));
919 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
920 return SDOperand(E, 0);
921 SDNode *N = new SDNode(Opcode, VT);
922 CSEMap.InsertNode(N, IP);
924 AllNodes.push_back(N);
925 return SDOperand(N, 0);
928 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
931 // Constant fold unary operations with an integer constant operand.
932 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
933 uint64_t Val = C->getValue();
936 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
937 case ISD::ANY_EXTEND:
938 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
939 case ISD::TRUNCATE: return getConstant(Val, VT);
940 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
941 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
942 case ISD::BIT_CONVERT:
943 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
944 return getConstantFP(BitsToFloat(Val), VT);
945 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
946 return getConstantFP(BitsToDouble(Val), VT);
950 default: assert(0 && "Invalid bswap!"); break;
951 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
952 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
953 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
958 default: assert(0 && "Invalid ctpop!"); break;
959 case MVT::i1: return getConstant(Val != 0, VT);
961 Tmp1 = (unsigned)Val & 0xFF;
962 return getConstant(CountPopulation_32(Tmp1), VT);
964 Tmp1 = (unsigned)Val & 0xFFFF;
965 return getConstant(CountPopulation_32(Tmp1), VT);
967 return getConstant(CountPopulation_32((unsigned)Val), VT);
969 return getConstant(CountPopulation_64(Val), VT);
973 default: assert(0 && "Invalid ctlz!"); break;
974 case MVT::i1: return getConstant(Val == 0, VT);
976 Tmp1 = (unsigned)Val & 0xFF;
977 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
979 Tmp1 = (unsigned)Val & 0xFFFF;
980 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
982 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
984 return getConstant(CountLeadingZeros_64(Val), VT);
988 default: assert(0 && "Invalid cttz!"); break;
989 case MVT::i1: return getConstant(Val == 0, VT);
991 Tmp1 = (unsigned)Val | 0x100;
992 return getConstant(CountTrailingZeros_32(Tmp1), VT);
994 Tmp1 = (unsigned)Val | 0x10000;
995 return getConstant(CountTrailingZeros_32(Tmp1), VT);
997 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
999 return getConstant(CountTrailingZeros_64(Val), VT);
1004 // Constant fold unary operations with an floating point constant operand.
1005 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1008 return getConstantFP(-C->getValue(), VT);
1010 return getConstantFP(fabs(C->getValue()), VT);
1012 case ISD::FP_EXTEND:
1013 return getConstantFP(C->getValue(), VT);
1014 case ISD::FP_TO_SINT:
1015 return getConstant((int64_t)C->getValue(), VT);
1016 case ISD::FP_TO_UINT:
1017 return getConstant((uint64_t)C->getValue(), VT);
1018 case ISD::BIT_CONVERT:
1019 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1020 return getConstant(FloatToBits(C->getValue()), VT);
1021 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1022 return getConstant(DoubleToBits(C->getValue()), VT);
1026 unsigned OpOpcode = Operand.Val->getOpcode();
1028 case ISD::TokenFactor:
1029 return Operand; // Factor of one node? No factor.
1030 case ISD::SIGN_EXTEND:
1031 if (Operand.getValueType() == VT) return Operand; // noop extension
1032 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
1033 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1034 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1036 case ISD::ZERO_EXTEND:
1037 if (Operand.getValueType() == VT) return Operand; // noop extension
1038 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
1039 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1040 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1042 case ISD::ANY_EXTEND:
1043 if (Operand.getValueType() == VT) return Operand; // noop extension
1044 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
1045 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1046 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1047 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1050 if (Operand.getValueType() == VT) return Operand; // noop truncate
1051 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
1052 if (OpOpcode == ISD::TRUNCATE)
1053 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1054 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1055 OpOpcode == ISD::ANY_EXTEND) {
1056 // If the source is smaller than the dest, we still need an extend.
1057 if (Operand.Val->getOperand(0).getValueType() < VT)
1058 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1059 else if (Operand.Val->getOperand(0).getValueType() > VT)
1060 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1062 return Operand.Val->getOperand(0);
1065 case ISD::BIT_CONVERT:
1066 // Basic sanity checking.
1067 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
1068 && "Cannot BIT_CONVERT between two different types!");
1069 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1070 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1071 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1072 if (OpOpcode == ISD::UNDEF)
1073 return getNode(ISD::UNDEF, VT);
1075 case ISD::SCALAR_TO_VECTOR:
1076 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1077 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1078 "Illegal SCALAR_TO_VECTOR node!");
1081 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1082 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1083 Operand.Val->getOperand(0));
1084 if (OpOpcode == ISD::FNEG) // --X -> X
1085 return Operand.Val->getOperand(0);
1088 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1089 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1094 SDVTList VTs = getVTList(VT);
1095 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1096 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Operand);
1098 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1099 return SDOperand(E, 0);
1100 N = new SDNode(Opcode, Operand);
1101 N->setValueTypes(VTs);
1102 CSEMap.InsertNode(N, IP);
1104 N = new SDNode(Opcode, Operand);
1105 N->setValueTypes(VTs);
1107 AllNodes.push_back(N);
1108 return SDOperand(N, 0);
1113 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1114 SDOperand N1, SDOperand N2) {
1117 case ISD::TokenFactor:
1118 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1119 N2.getValueType() == MVT::Other && "Invalid token factor!");
1128 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1135 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1142 assert(N1.getValueType() == N2.getValueType() &&
1143 N1.getValueType() == VT && "Binary operator types must match!");
1145 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1146 assert(N1.getValueType() == VT &&
1147 MVT::isFloatingPoint(N1.getValueType()) &&
1148 MVT::isFloatingPoint(N2.getValueType()) &&
1149 "Invalid FCOPYSIGN!");
1156 assert(VT == N1.getValueType() &&
1157 "Shift operators return type must be the same as their first arg");
1158 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1159 VT != MVT::i1 && "Shifts only work on integers");
1161 case ISD::FP_ROUND_INREG: {
1162 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1163 assert(VT == N1.getValueType() && "Not an inreg round!");
1164 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1165 "Cannot FP_ROUND_INREG integer types");
1166 assert(EVT <= VT && "Not rounding down!");
1169 case ISD::AssertSext:
1170 case ISD::AssertZext:
1171 case ISD::SIGN_EXTEND_INREG: {
1172 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1173 assert(VT == N1.getValueType() && "Not an inreg extend!");
1174 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1175 "Cannot *_EXTEND_INREG FP types");
1176 assert(EVT <= VT && "Not extending!");
1183 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1184 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1186 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1187 int64_t Val = N1C->getValue();
1188 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1189 Val <<= 64-FromBits;
1190 Val >>= 64-FromBits;
1191 return getConstant(Val, VT);
1195 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1197 case ISD::ADD: return getConstant(C1 + C2, VT);
1198 case ISD::SUB: return getConstant(C1 - C2, VT);
1199 case ISD::MUL: return getConstant(C1 * C2, VT);
1201 if (C2) return getConstant(C1 / C2, VT);
1204 if (C2) return getConstant(C1 % C2, VT);
1207 if (C2) return getConstant(N1C->getSignExtended() /
1208 N2C->getSignExtended(), VT);
1211 if (C2) return getConstant(N1C->getSignExtended() %
1212 N2C->getSignExtended(), VT);
1214 case ISD::AND : return getConstant(C1 & C2, VT);
1215 case ISD::OR : return getConstant(C1 | C2, VT);
1216 case ISD::XOR : return getConstant(C1 ^ C2, VT);
1217 case ISD::SHL : return getConstant(C1 << C2, VT);
1218 case ISD::SRL : return getConstant(C1 >> C2, VT);
1219 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
1221 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1224 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1228 } else { // Cannonicalize constant to RHS if commutative
1229 if (isCommutativeBinOp(Opcode)) {
1230 std::swap(N1C, N2C);
1236 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1237 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1240 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1242 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1243 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1244 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1246 if (C2) return getConstantFP(C1 / C2, VT);
1249 if (C2) return getConstantFP(fmod(C1, C2), VT);
1251 case ISD::FCOPYSIGN: {
1262 if (u2.I < 0) // Sign bit of RHS set?
1263 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1265 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1266 return getConstantFP(u1.F, VT);
1270 } else { // Cannonicalize constant to RHS if commutative
1271 if (isCommutativeBinOp(Opcode)) {
1272 std::swap(N1CFP, N2CFP);
1278 // Canonicalize an UNDEF to the RHS, even over a constant.
1279 if (N1.getOpcode() == ISD::UNDEF) {
1280 if (isCommutativeBinOp(Opcode)) {
1284 case ISD::FP_ROUND_INREG:
1285 case ISD::SIGN_EXTEND_INREG:
1291 return N1; // fold op(undef, arg2) -> undef
1298 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1303 // Fold a bunch of operators when the RHS is undef.
1304 if (N2.getOpcode() == ISD::UNDEF) {
1318 return N2; // fold op(arg1, undef) -> undef
1323 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1325 return getConstant(MVT::getIntVTBitMask(VT), VT);
1331 // Finally, fold operations that do not require constants.
1333 case ISD::FP_ROUND_INREG:
1334 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1336 case ISD::SIGN_EXTEND_INREG: {
1337 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1338 if (EVT == VT) return N1; // Not actually extending
1341 case ISD::EXTRACT_ELEMENT:
1342 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
1344 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
1345 // 64-bit integers into 32-bit parts. Instead of building the extract of
1346 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
1347 if (N1.getOpcode() == ISD::BUILD_PAIR)
1348 return N1.getOperand(N2C->getValue());
1350 // EXTRACT_ELEMENT of a constant int is also very common.
1351 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
1352 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
1353 return getConstant(C->getValue() >> Shift, VT);
1357 // FIXME: figure out how to safely handle things like
1358 // int foo(int x) { return 1 << (x & 255); }
1359 // int bar() { return foo(256); }
1364 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1365 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
1366 return getNode(Opcode, VT, N1, N2.getOperand(0));
1367 else if (N2.getOpcode() == ISD::AND)
1368 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1369 // If the and is only masking out bits that cannot effect the shift,
1370 // eliminate the and.
1371 unsigned NumBits = MVT::getSizeInBits(VT);
1372 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1373 return getNode(Opcode, VT, N1, N2.getOperand(0));
1379 // Memoize this node if possible.
1381 SDVTList VTs = getVTList(VT);
1382 if (VT != MVT::Flag) {
1383 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2);
1385 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1386 return SDOperand(E, 0);
1387 N = new SDNode(Opcode, N1, N2);
1388 N->setValueTypes(VTs);
1389 CSEMap.InsertNode(N, IP);
1391 N = new SDNode(Opcode, N1, N2);
1392 N->setValueTypes(VTs);
1395 AllNodes.push_back(N);
1396 return SDOperand(N, 0);
1399 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1400 SDOperand N1, SDOperand N2, SDOperand N3) {
1401 // Perform various simplifications.
1402 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1403 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1404 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1407 // Use SimplifySetCC to simplify SETCC's.
1408 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
1409 if (Simp.Val) return Simp;
1414 if (N1C->getValue())
1415 return N2; // select true, X, Y -> X
1417 return N3; // select false, X, Y -> Y
1419 if (N2 == N3) return N2; // select C, X, X -> X
1423 if (N2C->getValue()) // Unconditional branch
1424 return getNode(ISD::BR, MVT::Other, N1, N3);
1426 return N1; // Never-taken branch
1428 case ISD::VECTOR_SHUFFLE:
1429 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1430 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1431 N3.getOpcode() == ISD::BUILD_VECTOR &&
1432 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1433 "Illegal VECTOR_SHUFFLE node!");
1437 // Memoize node if it doesn't produce a flag.
1439 SDVTList VTs = getVTList(VT);
1440 if (VT != MVT::Flag) {
1441 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2, N3);
1443 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1444 return SDOperand(E, 0);
1445 N = new SDNode(Opcode, N1, N2, N3);
1446 N->setValueTypes(VTs);
1447 CSEMap.InsertNode(N, IP);
1449 N = new SDNode(Opcode, N1, N2, N3);
1450 N->setValueTypes(VTs);
1452 AllNodes.push_back(N);
1453 return SDOperand(N, 0);
1456 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1457 SDOperand N1, SDOperand N2, SDOperand N3,
1459 SDOperand Ops[] = { N1, N2, N3, N4 };
1460 return getNode(Opcode, VT, Ops, 4);
1463 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1464 SDOperand N1, SDOperand N2, SDOperand N3,
1465 SDOperand N4, SDOperand N5) {
1466 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1467 return getNode(Opcode, VT, Ops, 5);
1470 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1471 SDOperand Chain, SDOperand Ptr,
1472 const Value *SV, int SVOffset,
1474 // FIXME: Alignment == 1 for now.
1475 unsigned Alignment = 1;
1476 SDVTList VTs = getVTList(VT, MVT::Other);
1477 SDOperand Undef = getNode(ISD::UNDEF, VT);
1478 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, Undef);
1479 ID.AddInteger(ISD::UNINDEXED);
1480 ID.AddInteger(ISD::NON_EXTLOAD);
1483 ID.AddInteger(SVOffset);
1484 ID.AddInteger(Alignment);
1485 ID.AddInteger(isVolatile);
1487 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1488 return SDOperand(E, 0);
1489 SDNode *N = new LoadSDNode(Chain, Ptr, Undef, ISD::NON_EXTLOAD, VT,
1490 SV, SVOffset, Alignment, isVolatile);
1491 N->setValueTypes(VTs);
1492 CSEMap.InsertNode(N, IP);
1493 AllNodes.push_back(N);
1494 return SDOperand(N, 0);
1497 SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
1498 SDOperand Chain, SDOperand Ptr, const Value *SV,
1499 int SVOffset, MVT::ValueType EVT,
1501 // If they are asking for an extending load from/to the same thing, return a
1504 ExtType = ISD::NON_EXTLOAD;
1506 if (MVT::isVector(VT))
1507 assert(EVT == MVT::getVectorBaseType(VT) && "Invalid vector extload!");
1509 assert(EVT < VT && "Should only be an extending load, not truncating!");
1510 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
1511 "Cannot sign/zero extend a FP/Vector load!");
1512 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
1513 "Cannot convert from FP to Int or Int -> FP!");
1515 // FIXME: Alignment == 1 for now.
1516 unsigned Alignment = 1;
1517 SDVTList VTs = getVTList(VT, MVT::Other);
1518 SDOperand Undef = getNode(ISD::UNDEF, VT);
1519 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, Undef);
1520 ID.AddInteger(ISD::UNINDEXED);
1521 ID.AddInteger(ExtType);
1524 ID.AddInteger(SVOffset);
1525 ID.AddInteger(Alignment);
1526 ID.AddInteger(isVolatile);
1528 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1529 return SDOperand(E, 0);
1530 SDNode *N = new LoadSDNode(Chain, Ptr, Undef, ExtType, EVT, SV, SVOffset,
1531 Alignment, isVolatile);
1532 N->setValueTypes(VTs);
1533 CSEMap.InsertNode(N, IP);
1534 AllNodes.push_back(N);
1535 return SDOperand(N, 0);
1538 SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1539 SDOperand Chain, SDOperand Ptr,
1541 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1542 getValueType(EVT) };
1543 return getNode(ISD::VLOAD, getVTList(MVT::Vector, MVT::Other), Ops, 5);
1546 SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Value,
1547 SDOperand Ptr, SDOperand SV) {
1548 SDVTList VTs = getVTList(MVT::Other);
1549 SDOperand Ops[] = { Chain, Value, Ptr, SV };
1550 SelectionDAGCSEMap::NodeID ID(ISD::STORE, VTs, Ops, 4);
1552 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1553 return SDOperand(E, 0);
1554 SDNode *N = new SDNode(ISD::STORE, Chain, Value, Ptr, SV);
1555 N->setValueTypes(VTs);
1556 CSEMap.InsertNode(N, IP);
1557 AllNodes.push_back(N);
1558 return SDOperand(N, 0);
1561 SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1562 SDOperand Chain, SDOperand Ptr,
1564 SDOperand Ops[] = { Chain, Ptr, SV };
1565 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
1568 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1569 const SDOperand *Ops, unsigned NumOps) {
1571 case 0: return getNode(Opcode, VT);
1572 case 1: return getNode(Opcode, VT, Ops[0]);
1573 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1574 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
1580 case ISD::TRUNCSTORE: {
1581 assert(NumOps == 5 && "TRUNCSTORE takes 5 operands!");
1582 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1583 #if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1584 // If this is a truncating store of a constant, convert to the desired type
1585 // and store it instead.
1586 if (isa<Constant>(Ops[0])) {
1587 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1588 if (isa<Constant>(Op))
1591 // Also for ConstantFP?
1593 if (Ops[0].getValueType() == EVT) // Normal store?
1594 return getStore(Ops[0], Ops[1], Ops[2], Ops[3]);
1595 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1596 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1597 "Can't do FP-INT conversion!");
1600 case ISD::SELECT_CC: {
1601 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
1602 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1603 "LHS and RHS of condition must have same type!");
1604 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1605 "True and False arms of SelectCC must have same type!");
1606 assert(Ops[2].getValueType() == VT &&
1607 "select_cc node must be of same type as true and false value!");
1611 assert(NumOps == 5 && "BR_CC takes 5 operands!");
1612 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1613 "LHS/RHS of comparison should match types!");
1620 SDVTList VTs = getVTList(VT);
1621 if (VT != MVT::Flag) {
1622 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Ops, NumOps);
1624 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1625 return SDOperand(E, 0);
1626 N = new SDNode(Opcode, Ops, NumOps);
1627 N->setValueTypes(VTs);
1628 CSEMap.InsertNode(N, IP);
1630 N = new SDNode(Opcode, Ops, NumOps);
1631 N->setValueTypes(VTs);
1633 AllNodes.push_back(N);
1634 return SDOperand(N, 0);
1637 SDOperand SelectionDAG::getNode(unsigned Opcode,
1638 std::vector<MVT::ValueType> &ResultTys,
1639 const SDOperand *Ops, unsigned NumOps) {
1640 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
1644 SDOperand SelectionDAG::getNode(unsigned Opcode,
1645 const MVT::ValueType *VTs, unsigned NumVTs,
1646 const SDOperand *Ops, unsigned NumOps) {
1648 return getNode(Opcode, VTs[0], Ops, NumOps);
1649 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
1652 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
1653 const SDOperand *Ops, unsigned NumOps) {
1654 if (VTList.NumVTs == 1)
1655 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
1658 // FIXME: figure out how to safely handle things like
1659 // int foo(int x) { return 1 << (x & 255); }
1660 // int bar() { return foo(256); }
1662 case ISD::SRA_PARTS:
1663 case ISD::SRL_PARTS:
1664 case ISD::SHL_PARTS:
1665 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1666 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
1667 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1668 else if (N3.getOpcode() == ISD::AND)
1669 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1670 // If the and is only masking out bits that cannot effect the shift,
1671 // eliminate the and.
1672 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1673 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1674 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1680 // Memoize the node unless it returns a flag.
1682 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
1683 SelectionDAGCSEMap::NodeID ID;
1684 ID.SetOpcode(Opcode);
1685 ID.SetValueTypes(VTList);
1686 ID.SetOperands(&Ops[0], NumOps);
1688 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1689 return SDOperand(E, 0);
1690 N = new SDNode(Opcode, Ops, NumOps);
1691 N->setValueTypes(VTList);
1692 CSEMap.InsertNode(N, IP);
1694 N = new SDNode(Opcode, Ops, NumOps);
1695 N->setValueTypes(VTList);
1697 AllNodes.push_back(N);
1698 return SDOperand(N, 0);
1701 SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
1702 return makeVTList(SDNode::getValueTypeList(VT), 1);
1705 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
1706 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1707 E = VTList.end(); I != E; ++I) {
1708 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
1709 return makeVTList(&(*I)[0], 2);
1711 std::vector<MVT::ValueType> V;
1714 VTList.push_front(V);
1715 return makeVTList(&(*VTList.begin())[0], 2);
1717 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
1718 MVT::ValueType VT3) {
1719 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1720 E = VTList.end(); I != E; ++I) {
1721 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
1723 return makeVTList(&(*I)[0], 3);
1725 std::vector<MVT::ValueType> V;
1729 VTList.push_front(V);
1730 return makeVTList(&(*VTList.begin())[0], 3);
1733 SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
1735 case 0: assert(0 && "Cannot have nodes without results!");
1736 case 1: return makeVTList(SDNode::getValueTypeList(VTs[0]), 1);
1737 case 2: return getVTList(VTs[0], VTs[1]);
1738 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
1742 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1743 E = VTList.end(); I != E; ++I) {
1744 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
1746 bool NoMatch = false;
1747 for (unsigned i = 2; i != NumVTs; ++i)
1748 if (VTs[i] != (*I)[i]) {
1753 return makeVTList(&*I->begin(), NumVTs);
1756 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
1757 return makeVTList(&*VTList.begin()->begin(), NumVTs);
1761 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1762 /// specified operands. If the resultant node already exists in the DAG,
1763 /// this does not modify the specified node, instead it returns the node that
1764 /// already exists. If the resultant node does not exist in the DAG, the
1765 /// input node is returned. As a degenerate case, if you specify the same
1766 /// input operands as the node already has, the input node is returned.
1767 SDOperand SelectionDAG::
1768 UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1769 SDNode *N = InN.Val;
1770 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1772 // Check to see if there is no change.
1773 if (Op == N->getOperand(0)) return InN;
1775 // See if the modified node already exists.
1776 void *InsertPos = 0;
1777 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1778 return SDOperand(Existing, InN.ResNo);
1780 // Nope it doesn't. Remove the node from it's current place in the maps.
1782 RemoveNodeFromCSEMaps(N);
1784 // Now we update the operands.
1785 N->OperandList[0].Val->removeUser(N);
1787 N->OperandList[0] = Op;
1789 // If this gets put into a CSE map, add it.
1790 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1794 SDOperand SelectionDAG::
1795 UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1796 SDNode *N = InN.Val;
1797 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1799 // Check to see if there is no change.
1800 bool AnyChange = false;
1801 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1802 return InN; // No operands changed, just return the input node.
1804 // See if the modified node already exists.
1805 void *InsertPos = 0;
1806 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1807 return SDOperand(Existing, InN.ResNo);
1809 // Nope it doesn't. Remove the node from it's current place in the maps.
1811 RemoveNodeFromCSEMaps(N);
1813 // Now we update the operands.
1814 if (N->OperandList[0] != Op1) {
1815 N->OperandList[0].Val->removeUser(N);
1816 Op1.Val->addUser(N);
1817 N->OperandList[0] = Op1;
1819 if (N->OperandList[1] != Op2) {
1820 N->OperandList[1].Val->removeUser(N);
1821 Op2.Val->addUser(N);
1822 N->OperandList[1] = Op2;
1825 // If this gets put into a CSE map, add it.
1826 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1830 SDOperand SelectionDAG::
1831 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1832 SDOperand Ops[] = { Op1, Op2, Op3 };
1833 return UpdateNodeOperands(N, Ops, 3);
1836 SDOperand SelectionDAG::
1837 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1838 SDOperand Op3, SDOperand Op4) {
1839 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
1840 return UpdateNodeOperands(N, Ops, 4);
1843 SDOperand SelectionDAG::
1844 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1845 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1846 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
1847 return UpdateNodeOperands(N, Ops, 5);
1851 SDOperand SelectionDAG::
1852 UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
1853 SDNode *N = InN.Val;
1854 assert(N->getNumOperands() == NumOps &&
1855 "Update with wrong number of operands");
1857 // Check to see if there is no change.
1858 bool AnyChange = false;
1859 for (unsigned i = 0; i != NumOps; ++i) {
1860 if (Ops[i] != N->getOperand(i)) {
1866 // No operands changed, just return the input node.
1867 if (!AnyChange) return InN;
1869 // See if the modified node already exists.
1870 void *InsertPos = 0;
1871 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
1872 return SDOperand(Existing, InN.ResNo);
1874 // Nope it doesn't. Remove the node from it's current place in the maps.
1876 RemoveNodeFromCSEMaps(N);
1878 // Now we update the operands.
1879 for (unsigned i = 0; i != NumOps; ++i) {
1880 if (N->OperandList[i] != Ops[i]) {
1881 N->OperandList[i].Val->removeUser(N);
1882 Ops[i].Val->addUser(N);
1883 N->OperandList[i] = Ops[i];
1887 // If this gets put into a CSE map, add it.
1888 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1895 /// SelectNodeTo - These are used for target selectors to *mutate* the
1896 /// specified node to have the specified return type, Target opcode, and
1897 /// operands. Note that target opcodes are stored as
1898 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1900 /// Note that SelectNodeTo returns the resultant node. If there is already a
1901 /// node of the specified opcode and operands, it returns that node instead of
1902 /// the current one.
1903 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1904 MVT::ValueType VT) {
1905 SDVTList VTs = getVTList(VT);
1906 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1908 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1911 RemoveNodeFromCSEMaps(N);
1913 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1914 N->setValueTypes(VTs);
1916 CSEMap.InsertNode(N, IP);
1920 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1921 MVT::ValueType VT, SDOperand Op1) {
1922 // If an identical node already exists, use it.
1923 SDVTList VTs = getVTList(VT);
1924 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
1926 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1929 RemoveNodeFromCSEMaps(N);
1930 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1931 N->setValueTypes(VTs);
1932 N->setOperands(Op1);
1933 CSEMap.InsertNode(N, IP);
1937 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1938 MVT::ValueType VT, SDOperand Op1,
1940 // If an identical node already exists, use it.
1941 SDVTList VTs = getVTList(VT);
1942 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1944 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1947 RemoveNodeFromCSEMaps(N);
1948 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1949 N->setValueTypes(VTs);
1950 N->setOperands(Op1, Op2);
1952 CSEMap.InsertNode(N, IP); // Memoize the new node.
1956 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1957 MVT::ValueType VT, SDOperand Op1,
1958 SDOperand Op2, SDOperand Op3) {
1959 // If an identical node already exists, use it.
1960 SDVTList VTs = getVTList(VT);
1961 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
1964 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1967 RemoveNodeFromCSEMaps(N);
1968 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1969 N->setValueTypes(VTs);
1970 N->setOperands(Op1, Op2, Op3);
1972 CSEMap.InsertNode(N, IP); // Memoize the new node.
1976 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1977 MVT::ValueType VT, const SDOperand *Ops,
1979 // If an identical node already exists, use it.
1980 SDVTList VTs = getVTList(VT);
1981 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1982 for (unsigned i = 0; i != NumOps; ++i)
1983 ID.AddOperand(Ops[i]);
1985 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1988 RemoveNodeFromCSEMaps(N);
1989 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1990 N->setValueTypes(VTs);
1991 N->setOperands(Ops, NumOps);
1993 CSEMap.InsertNode(N, IP); // Memoize the new node.
1997 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1998 MVT::ValueType VT1, MVT::ValueType VT2,
1999 SDOperand Op1, SDOperand Op2) {
2000 SDVTList VTs = getVTList(VT1, VT2);
2001 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
2003 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2006 RemoveNodeFromCSEMaps(N);
2007 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2008 N->setValueTypes(VTs);
2009 N->setOperands(Op1, Op2);
2011 CSEMap.InsertNode(N, IP); // Memoize the new node.
2015 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2016 MVT::ValueType VT1, MVT::ValueType VT2,
2017 SDOperand Op1, SDOperand Op2,
2019 // If an identical node already exists, use it.
2020 SDVTList VTs = getVTList(VT1, VT2);
2021 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
2024 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2027 RemoveNodeFromCSEMaps(N);
2028 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2029 N->setValueTypes(VTs);
2030 N->setOperands(Op1, Op2, Op3);
2032 CSEMap.InsertNode(N, IP); // Memoize the new node.
2037 /// getTargetNode - These are used for target selectors to create a new node
2038 /// with specified return type(s), target opcode, and operands.
2040 /// Note that getTargetNode returns the resultant node. If there is already a
2041 /// node of the specified opcode and operands, it returns that node instead of
2042 /// the current one.
2043 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2044 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2046 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2048 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2050 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2051 SDOperand Op1, SDOperand Op2) {
2052 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2054 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2055 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2056 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2058 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2059 const SDOperand *Ops, unsigned NumOps) {
2060 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
2062 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2063 MVT::ValueType VT2, SDOperand Op1) {
2064 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2065 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
2067 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2068 MVT::ValueType VT2, SDOperand Op1,
2070 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2071 SDOperand Ops[] = { Op1, Op2 };
2072 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
2074 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2075 MVT::ValueType VT2, SDOperand Op1,
2076 SDOperand Op2, SDOperand Op3) {
2077 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2078 SDOperand Ops[] = { Op1, Op2, Op3 };
2079 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
2081 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2083 const SDOperand *Ops, unsigned NumOps) {
2084 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2085 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
2087 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2088 MVT::ValueType VT2, MVT::ValueType VT3,
2089 SDOperand Op1, SDOperand Op2) {
2090 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
2091 SDOperand Ops[] = { Op1, Op2 };
2092 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
2094 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2095 MVT::ValueType VT2, MVT::ValueType VT3,
2096 const SDOperand *Ops, unsigned NumOps) {
2097 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
2098 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
2101 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2102 /// This can cause recursive merging of nodes in the DAG.
2104 /// This version assumes From/To have a single result value.
2106 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2107 std::vector<SDNode*> *Deleted) {
2108 SDNode *From = FromN.Val, *To = ToN.Val;
2109 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2110 "Cannot replace with this method!");
2111 assert(From != To && "Cannot replace uses of with self");
2113 while (!From->use_empty()) {
2114 // Process users until they are all gone.
2115 SDNode *U = *From->use_begin();
2117 // This node is about to morph, remove its old self from the CSE maps.
2118 RemoveNodeFromCSEMaps(U);
2120 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2122 if (I->Val == From) {
2123 From->removeUser(U);
2128 // Now that we have modified U, add it back to the CSE maps. If it already
2129 // exists there, recursively merge the results together.
2130 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2131 ReplaceAllUsesWith(U, Existing, Deleted);
2133 if (Deleted) Deleted->push_back(U);
2134 DeleteNodeNotInCSEMaps(U);
2139 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2140 /// This can cause recursive merging of nodes in the DAG.
2142 /// This version assumes From/To have matching types and numbers of result
2145 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2146 std::vector<SDNode*> *Deleted) {
2147 assert(From != To && "Cannot replace uses of with self");
2148 assert(From->getNumValues() == To->getNumValues() &&
2149 "Cannot use this version of ReplaceAllUsesWith!");
2150 if (From->getNumValues() == 1) { // If possible, use the faster version.
2151 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
2155 while (!From->use_empty()) {
2156 // Process users until they are all gone.
2157 SDNode *U = *From->use_begin();
2159 // This node is about to morph, remove its old self from the CSE maps.
2160 RemoveNodeFromCSEMaps(U);
2162 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2164 if (I->Val == From) {
2165 From->removeUser(U);
2170 // Now that we have modified U, add it back to the CSE maps. If it already
2171 // exists there, recursively merge the results together.
2172 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2173 ReplaceAllUsesWith(U, Existing, Deleted);
2175 if (Deleted) Deleted->push_back(U);
2176 DeleteNodeNotInCSEMaps(U);
2181 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2182 /// This can cause recursive merging of nodes in the DAG.
2184 /// This version can replace From with any result values. To must match the
2185 /// number and types of values returned by From.
2186 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
2187 const SDOperand *To,
2188 std::vector<SDNode*> *Deleted) {
2189 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
2190 // Degenerate case handled above.
2191 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
2195 while (!From->use_empty()) {
2196 // Process users until they are all gone.
2197 SDNode *U = *From->use_begin();
2199 // This node is about to morph, remove its old self from the CSE maps.
2200 RemoveNodeFromCSEMaps(U);
2202 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2204 if (I->Val == From) {
2205 const SDOperand &ToOp = To[I->ResNo];
2206 From->removeUser(U);
2208 ToOp.Val->addUser(U);
2211 // Now that we have modified U, add it back to the CSE maps. If it already
2212 // exists there, recursively merge the results together.
2213 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2214 ReplaceAllUsesWith(U, Existing, Deleted);
2216 if (Deleted) Deleted->push_back(U);
2217 DeleteNodeNotInCSEMaps(U);
2222 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2223 /// uses of other values produced by From.Val alone. The Deleted vector is
2224 /// handled the same was as for ReplaceAllUsesWith.
2225 void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2226 std::vector<SDNode*> &Deleted) {
2227 assert(From != To && "Cannot replace a value with itself");
2228 // Handle the simple, trivial, case efficiently.
2229 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2230 ReplaceAllUsesWith(From, To, &Deleted);
2234 // Get all of the users in a nice, deterministically ordered, uniqued set.
2235 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2237 while (!Users.empty()) {
2238 // We know that this user uses some value of From. If it is the right
2239 // value, update it.
2240 SDNode *User = Users.back();
2243 for (SDOperand *Op = User->OperandList,
2244 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2246 // Okay, we know this user needs to be updated. Remove its old self
2247 // from the CSE maps.
2248 RemoveNodeFromCSEMaps(User);
2250 // Update all operands that match "From".
2251 for (; Op != E; ++Op) {
2253 From.Val->removeUser(User);
2255 To.Val->addUser(User);
2259 // Now that we have modified User, add it back to the CSE maps. If it
2260 // already exists there, recursively merge the results together.
2261 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2262 unsigned NumDeleted = Deleted.size();
2263 ReplaceAllUsesWith(User, Existing, &Deleted);
2265 // User is now dead.
2266 Deleted.push_back(User);
2267 DeleteNodeNotInCSEMaps(User);
2269 // We have to be careful here, because ReplaceAllUsesWith could have
2270 // deleted a user of From, which means there may be dangling pointers
2271 // in the "Users" setvector. Scan over the deleted node pointers and
2272 // remove them from the setvector.
2273 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2274 Users.remove(Deleted[i]);
2276 break; // Exit the operand scanning loop.
2283 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2284 /// their allnodes order. It returns the maximum id.
2285 unsigned SelectionDAG::AssignNodeIds() {
2287 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2294 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
2295 /// based on their topological order. It returns the maximum id and a vector
2296 /// of the SDNodes* in assigned order by reference.
2297 unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
2298 unsigned DAGSize = AllNodes.size();
2299 std::vector<unsigned> InDegree(DAGSize);
2300 std::vector<SDNode*> Sources;
2302 // Use a two pass approach to avoid using a std::map which is slow.
2304 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2307 unsigned Degree = N->use_size();
2308 InDegree[N->getNodeId()] = Degree;
2310 Sources.push_back(N);
2314 while (!Sources.empty()) {
2315 SDNode *N = Sources.back();
2317 TopOrder.push_back(N);
2318 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2320 unsigned Degree = --InDegree[P->getNodeId()];
2322 Sources.push_back(P);
2326 // Second pass, assign the actual topological order as node ids.
2328 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2330 (*TI)->setNodeId(Id++);
2337 //===----------------------------------------------------------------------===//
2339 //===----------------------------------------------------------------------===//
2341 // Out-of-line virtual method to give class a home.
2342 void SDNode::ANCHOR() {
2345 /// getValueTypeList - Return a pointer to the specified value type.
2347 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2348 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2353 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2354 /// indicated value. This method ignores uses of other values defined by this
2356 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
2357 assert(Value < getNumValues() && "Bad value!");
2359 // If there is only one value, this is easy.
2360 if (getNumValues() == 1)
2361 return use_size() == NUses;
2362 if (Uses.size() < NUses) return false;
2364 SDOperand TheValue(const_cast<SDNode *>(this), Value);
2366 std::set<SDNode*> UsersHandled;
2368 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
2370 if (User->getNumOperands() == 1 ||
2371 UsersHandled.insert(User).second) // First time we've seen this?
2372 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2373 if (User->getOperand(i) == TheValue) {
2375 return false; // too many uses
2380 // Found exactly the right number of uses?
2385 // isOnlyUse - Return true if this node is the only use of N.
2386 bool SDNode::isOnlyUse(SDNode *N) const {
2388 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2399 // isOperand - Return true if this node is an operand of N.
2400 bool SDOperand::isOperand(SDNode *N) const {
2401 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2402 if (*this == N->getOperand(i))
2407 bool SDNode::isOperand(SDNode *N) const {
2408 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2409 if (this == N->OperandList[i].Val)
2414 uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
2415 assert(Num < NumOperands && "Invalid child # of SDNode!");
2416 return cast<ConstantSDNode>(OperandList[Num])->getValue();
2419 const char *SDNode::getOperationName(const SelectionDAG *G) const {
2420 switch (getOpcode()) {
2422 if (getOpcode() < ISD::BUILTIN_OP_END)
2423 return "<<Unknown DAG Node>>";
2426 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
2427 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2428 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
2430 TargetLowering &TLI = G->getTargetLoweringInfo();
2432 TLI.getTargetNodeName(getOpcode());
2433 if (Name) return Name;
2436 return "<<Unknown Target Node>>";
2439 case ISD::PCMARKER: return "PCMarker";
2440 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
2441 case ISD::SRCVALUE: return "SrcValue";
2442 case ISD::EntryToken: return "EntryToken";
2443 case ISD::TokenFactor: return "TokenFactor";
2444 case ISD::AssertSext: return "AssertSext";
2445 case ISD::AssertZext: return "AssertZext";
2447 case ISD::STRING: return "String";
2448 case ISD::BasicBlock: return "BasicBlock";
2449 case ISD::VALUETYPE: return "ValueType";
2450 case ISD::Register: return "Register";
2452 case ISD::Constant: return "Constant";
2453 case ISD::ConstantFP: return "ConstantFP";
2454 case ISD::GlobalAddress: return "GlobalAddress";
2455 case ISD::FrameIndex: return "FrameIndex";
2456 case ISD::JumpTable: return "JumpTable";
2457 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
2458 case ISD::ConstantPool: return "ConstantPool";
2459 case ISD::ExternalSymbol: return "ExternalSymbol";
2460 case ISD::INTRINSIC_WO_CHAIN: {
2461 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2462 return Intrinsic::getName((Intrinsic::ID)IID);
2464 case ISD::INTRINSIC_VOID:
2465 case ISD::INTRINSIC_W_CHAIN: {
2466 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
2467 return Intrinsic::getName((Intrinsic::ID)IID);
2470 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
2471 case ISD::TargetConstant: return "TargetConstant";
2472 case ISD::TargetConstantFP:return "TargetConstantFP";
2473 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2474 case ISD::TargetFrameIndex: return "TargetFrameIndex";
2475 case ISD::TargetJumpTable: return "TargetJumpTable";
2476 case ISD::TargetConstantPool: return "TargetConstantPool";
2477 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
2479 case ISD::CopyToReg: return "CopyToReg";
2480 case ISD::CopyFromReg: return "CopyFromReg";
2481 case ISD::UNDEF: return "undef";
2482 case ISD::MERGE_VALUES: return "mergevalues";
2483 case ISD::INLINEASM: return "inlineasm";
2484 case ISD::HANDLENODE: return "handlenode";
2485 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
2486 case ISD::CALL: return "call";
2489 case ISD::FABS: return "fabs";
2490 case ISD::FNEG: return "fneg";
2491 case ISD::FSQRT: return "fsqrt";
2492 case ISD::FSIN: return "fsin";
2493 case ISD::FCOS: return "fcos";
2494 case ISD::FPOWI: return "fpowi";
2497 case ISD::ADD: return "add";
2498 case ISD::SUB: return "sub";
2499 case ISD::MUL: return "mul";
2500 case ISD::MULHU: return "mulhu";
2501 case ISD::MULHS: return "mulhs";
2502 case ISD::SDIV: return "sdiv";
2503 case ISD::UDIV: return "udiv";
2504 case ISD::SREM: return "srem";
2505 case ISD::UREM: return "urem";
2506 case ISD::AND: return "and";
2507 case ISD::OR: return "or";
2508 case ISD::XOR: return "xor";
2509 case ISD::SHL: return "shl";
2510 case ISD::SRA: return "sra";
2511 case ISD::SRL: return "srl";
2512 case ISD::ROTL: return "rotl";
2513 case ISD::ROTR: return "rotr";
2514 case ISD::FADD: return "fadd";
2515 case ISD::FSUB: return "fsub";
2516 case ISD::FMUL: return "fmul";
2517 case ISD::FDIV: return "fdiv";
2518 case ISD::FREM: return "frem";
2519 case ISD::FCOPYSIGN: return "fcopysign";
2520 case ISD::VADD: return "vadd";
2521 case ISD::VSUB: return "vsub";
2522 case ISD::VMUL: return "vmul";
2523 case ISD::VSDIV: return "vsdiv";
2524 case ISD::VUDIV: return "vudiv";
2525 case ISD::VAND: return "vand";
2526 case ISD::VOR: return "vor";
2527 case ISD::VXOR: return "vxor";
2529 case ISD::SETCC: return "setcc";
2530 case ISD::SELECT: return "select";
2531 case ISD::SELECT_CC: return "select_cc";
2532 case ISD::VSELECT: return "vselect";
2533 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2534 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2535 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
2536 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
2537 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2538 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2539 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2540 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2541 case ISD::VBIT_CONVERT: return "vbit_convert";
2542 case ISD::ADDC: return "addc";
2543 case ISD::ADDE: return "adde";
2544 case ISD::SUBC: return "subc";
2545 case ISD::SUBE: return "sube";
2546 case ISD::SHL_PARTS: return "shl_parts";
2547 case ISD::SRA_PARTS: return "sra_parts";
2548 case ISD::SRL_PARTS: return "srl_parts";
2550 // Conversion operators.
2551 case ISD::SIGN_EXTEND: return "sign_extend";
2552 case ISD::ZERO_EXTEND: return "zero_extend";
2553 case ISD::ANY_EXTEND: return "any_extend";
2554 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
2555 case ISD::TRUNCATE: return "truncate";
2556 case ISD::FP_ROUND: return "fp_round";
2557 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
2558 case ISD::FP_EXTEND: return "fp_extend";
2560 case ISD::SINT_TO_FP: return "sint_to_fp";
2561 case ISD::UINT_TO_FP: return "uint_to_fp";
2562 case ISD::FP_TO_SINT: return "fp_to_sint";
2563 case ISD::FP_TO_UINT: return "fp_to_uint";
2564 case ISD::BIT_CONVERT: return "bit_convert";
2566 // Control flow instructions
2567 case ISD::BR: return "br";
2568 case ISD::BRIND: return "brind";
2569 case ISD::BRCOND: return "brcond";
2570 case ISD::BR_CC: return "br_cc";
2571 case ISD::RET: return "ret";
2572 case ISD::CALLSEQ_START: return "callseq_start";
2573 case ISD::CALLSEQ_END: return "callseq_end";
2576 case ISD::LOAD: return "load";
2577 case ISD::STORE: return "store";
2578 case ISD::VLOAD: return "vload";
2579 case ISD::TRUNCSTORE: return "truncstore";
2580 case ISD::VAARG: return "vaarg";
2581 case ISD::VACOPY: return "vacopy";
2582 case ISD::VAEND: return "vaend";
2583 case ISD::VASTART: return "vastart";
2584 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2585 case ISD::EXTRACT_ELEMENT: return "extract_element";
2586 case ISD::BUILD_PAIR: return "build_pair";
2587 case ISD::STACKSAVE: return "stacksave";
2588 case ISD::STACKRESTORE: return "stackrestore";
2590 // Block memory operations.
2591 case ISD::MEMSET: return "memset";
2592 case ISD::MEMCPY: return "memcpy";
2593 case ISD::MEMMOVE: return "memmove";
2596 case ISD::BSWAP: return "bswap";
2597 case ISD::CTPOP: return "ctpop";
2598 case ISD::CTTZ: return "cttz";
2599 case ISD::CTLZ: return "ctlz";
2602 case ISD::LOCATION: return "location";
2603 case ISD::DEBUG_LOC: return "debug_loc";
2604 case ISD::DEBUG_LABEL: return "debug_label";
2607 switch (cast<CondCodeSDNode>(this)->get()) {
2608 default: assert(0 && "Unknown setcc condition!");
2609 case ISD::SETOEQ: return "setoeq";
2610 case ISD::SETOGT: return "setogt";
2611 case ISD::SETOGE: return "setoge";
2612 case ISD::SETOLT: return "setolt";
2613 case ISD::SETOLE: return "setole";
2614 case ISD::SETONE: return "setone";
2616 case ISD::SETO: return "seto";
2617 case ISD::SETUO: return "setuo";
2618 case ISD::SETUEQ: return "setue";
2619 case ISD::SETUGT: return "setugt";
2620 case ISD::SETUGE: return "setuge";
2621 case ISD::SETULT: return "setult";
2622 case ISD::SETULE: return "setule";
2623 case ISD::SETUNE: return "setune";
2625 case ISD::SETEQ: return "seteq";
2626 case ISD::SETGT: return "setgt";
2627 case ISD::SETGE: return "setge";
2628 case ISD::SETLT: return "setlt";
2629 case ISD::SETLE: return "setle";
2630 case ISD::SETNE: return "setne";
2635 void SDNode::dump() const { dump(0); }
2636 void SDNode::dump(const SelectionDAG *G) const {
2637 std::cerr << (void*)this << ": ";
2639 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2640 if (i) std::cerr << ",";
2641 if (getValueType(i) == MVT::Other)
2644 std::cerr << MVT::getValueTypeString(getValueType(i));
2646 std::cerr << " = " << getOperationName(G);
2649 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2650 if (i) std::cerr << ", ";
2651 std::cerr << (void*)getOperand(i).Val;
2652 if (unsigned RN = getOperand(i).ResNo)
2653 std::cerr << ":" << RN;
2656 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2657 std::cerr << "<" << CSDN->getValue() << ">";
2658 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2659 std::cerr << "<" << CSDN->getValue() << ">";
2660 } else if (const GlobalAddressSDNode *GADN =
2661 dyn_cast<GlobalAddressSDNode>(this)) {
2662 int offset = GADN->getOffset();
2664 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
2666 std::cerr << " + " << offset;
2668 std::cerr << " " << offset;
2669 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
2670 std::cerr << "<" << FIDN->getIndex() << ">";
2671 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
2672 int offset = CP->getOffset();
2673 if (CP->isMachineConstantPoolEntry())
2674 std::cerr << "<" << *CP->getMachineCPVal() << ">";
2676 std::cerr << "<" << *CP->getConstVal() << ">";
2678 std::cerr << " + " << offset;
2680 std::cerr << " " << offset;
2681 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
2683 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2685 std::cerr << LBB->getName() << " ";
2686 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
2687 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
2688 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2689 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2691 std::cerr << " #" << R->getReg();
2693 } else if (const ExternalSymbolSDNode *ES =
2694 dyn_cast<ExternalSymbolSDNode>(this)) {
2695 std::cerr << "'" << ES->getSymbol() << "'";
2696 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2698 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2700 std::cerr << "<null:" << M->getOffset() << ">";
2701 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2702 std::cerr << ":" << getValueTypeString(N->getVT());
2703 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
2705 switch (LD->getExtensionType()) {
2706 default: doExt = false; break;
2708 std::cerr << " <anyext ";
2711 std::cerr << " <sext ";
2714 std::cerr << " <zext ";
2718 std::cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
2720 if (LD->getAddressingMode() == ISD::PRE_INDEXED)
2721 std::cerr << " <pre>";
2722 else if (LD->getAddressingMode() == ISD::POST_INDEXED)
2723 std::cerr << " <post>";
2727 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
2728 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2729 if (N->getOperand(i).Val->hasOneUse())
2730 DumpNodes(N->getOperand(i).Val, indent+2, G);
2732 std::cerr << "\n" << std::string(indent+2, ' ')
2733 << (void*)N->getOperand(i).Val << ": <multiple use>";
2736 std::cerr << "\n" << std::string(indent, ' ');
2740 void SelectionDAG::dump() const {
2741 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
2742 std::vector<const SDNode*> Nodes;
2743 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2747 std::sort(Nodes.begin(), Nodes.end());
2749 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2750 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
2751 DumpNodes(Nodes[i], 2, this);
2754 DumpNodes(getRoot().Val, 2, this);
2756 std::cerr << "\n\n";
2759 const Type *ConstantPoolSDNode::getType() const {
2760 if (isMachineConstantPoolEntry())
2761 return Val.MachineCPVal->getType();
2762 return Val.ConstVal->getType();