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/Assembly/Writer.h"
18 #include "llvm/CodeGen/MachineBasicBlock.h"
19 #include "llvm/Support/MathExtras.h"
20 #include "llvm/Target/MRegisterInfo.h"
21 #include "llvm/Target/TargetLowering.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #include "llvm/Target/TargetMachine.h"
30 static bool isCommutativeBinOp(unsigned Opcode) {
38 case ISD::XOR: return true;
39 default: return false; // FIXME: Need commutative info for user ops!
43 static bool isAssociativeBinOp(unsigned Opcode) {
49 case ISD::XOR: return true;
50 default: return false; // FIXME: Need associative info for user ops!
54 // isInvertibleForFree - Return true if there is no cost to emitting the logical
55 // inverse of this node.
56 static bool isInvertibleForFree(SDOperand N) {
57 if (isa<ConstantSDNode>(N.Val)) return true;
58 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
63 //===----------------------------------------------------------------------===//
64 // ConstantFPSDNode Class
65 //===----------------------------------------------------------------------===//
67 /// isExactlyValue - We don't rely on operator== working on double values, as
68 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
69 /// As such, this method can be used to do an exact bit-for-bit comparison of
70 /// two floating point values.
71 bool ConstantFPSDNode::isExactlyValue(double V) const {
72 return DoubleToBits(V) == DoubleToBits(Value);
75 //===----------------------------------------------------------------------===//
77 //===----------------------------------------------------------------------===//
79 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
80 /// when given the operation for (X op Y).
81 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
82 // To perform this operation, we just need to swap the L and G bits of the
84 unsigned OldL = (Operation >> 2) & 1;
85 unsigned OldG = (Operation >> 1) & 1;
86 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
87 (OldL << 1) | // New G bit
88 (OldG << 2)); // New L bit.
91 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
92 /// 'op' is a valid SetCC operation.
93 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
94 unsigned Operation = Op;
96 Operation ^= 7; // Flip L, G, E bits, but not U.
98 Operation ^= 15; // Flip all of the condition bits.
99 if (Operation > ISD::SETTRUE2)
100 Operation &= ~8; // Don't let N and U bits get set.
101 return ISD::CondCode(Operation);
105 /// isSignedOp - For an integer comparison, return 1 if the comparison is a
106 /// signed operation and 2 if the result is an unsigned comparison. Return zero
107 /// if the operation does not depend on the sign of the input (setne and seteq).
108 static int isSignedOp(ISD::CondCode Opcode) {
110 default: assert(0 && "Illegal integer setcc operation!");
112 case ISD::SETNE: return 0;
116 case ISD::SETGE: return 1;
120 case ISD::SETUGE: return 2;
124 /// getSetCCOrOperation - Return the result of a logical OR between different
125 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
126 /// returns SETCC_INVALID if it is not possible to represent the resultant
128 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
130 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
131 // Cannot fold a signed integer setcc with an unsigned integer setcc.
132 return ISD::SETCC_INVALID;
134 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
136 // If the N and U bits get set then the resultant comparison DOES suddenly
137 // care about orderedness, and is true when ordered.
138 if (Op > ISD::SETTRUE2)
139 Op &= ~16; // Clear the N bit.
140 return ISD::CondCode(Op);
143 /// getSetCCAndOperation - Return the result of a logical AND between different
144 /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
145 /// function returns zero if it is not possible to represent the resultant
147 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
149 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
150 // Cannot fold a signed setcc with an unsigned setcc.
151 return ISD::SETCC_INVALID;
153 // Combine all of the condition bits.
154 return ISD::CondCode(Op1 & Op2);
157 const TargetMachine &SelectionDAG::getTarget() const {
158 return TLI.getTargetMachine();
161 //===----------------------------------------------------------------------===//
162 // SelectionDAG Class
163 //===----------------------------------------------------------------------===//
165 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
166 /// SelectionDAG, including nodes (like loads) that have uses of their token
167 /// chain but no other uses and no side effect. If a node is passed in as an
168 /// argument, it is used as the seed for node deletion.
169 void SelectionDAG::RemoveDeadNodes(SDNode *N) {
170 // Create a dummy node (which is not added to allnodes), that adds a reference
171 // to the root node, preventing it from being deleted.
172 HandleSDNode Dummy(getRoot());
174 bool MadeChange = false;
176 // If we have a hint to start from, use it.
177 if (N && N->use_empty()) {
182 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
183 if (I->use_empty() && I->getOpcode() != 65535) {
184 // Node is dead, recursively delete newly dead uses.
189 // Walk the nodes list, removing the nodes we've marked as dead.
191 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
198 // If the root changed (e.g. it was a dead load, update the root).
199 setRoot(Dummy.getValue());
202 /// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
203 /// graph. If it is the last user of any of its operands, recursively process
204 /// them the same way.
206 void SelectionDAG::DestroyDeadNode(SDNode *N) {
207 // Okay, we really are going to delete this node. First take this out of the
208 // appropriate CSE map.
209 RemoveNodeFromCSEMaps(N);
211 // Next, brutally remove the operand list. This is safe to do, as there are
212 // no cycles in the graph.
213 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
217 // Now that we removed this operand, see if there are no uses of it left.
221 delete[] N->OperandList;
225 // Mark the node as dead.
226 N->MorphNodeTo(65535);
229 void SelectionDAG::DeleteNode(SDNode *N) {
230 assert(N->use_empty() && "Cannot delete a node that is not dead!");
232 // First take this out of the appropriate CSE map.
233 RemoveNodeFromCSEMaps(N);
235 // Finally, remove uses due to operands of this node, remove from the
236 // AllNodes list, and delete the node.
237 DeleteNodeNotInCSEMaps(N);
240 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
242 // Remove it from the AllNodes list.
245 // Drop all of the operands and decrement used nodes use counts.
246 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
247 I->Val->removeUser(N);
248 delete[] N->OperandList;
255 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
256 /// correspond to it. This is useful when we're about to delete or repurpose
257 /// the node. We don't want future request for structurally identical nodes
258 /// to return N anymore.
259 void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
261 switch (N->getOpcode()) {
262 case ISD::HANDLENODE: return; // noop.
264 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
265 N->getValueType(0)));
267 case ISD::TargetConstant:
268 Erased = TargetConstants.erase(std::make_pair(
269 cast<ConstantSDNode>(N)->getValue(),
270 N->getValueType(0)));
272 case ISD::ConstantFP: {
273 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
274 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
278 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
281 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
282 "Cond code doesn't exist!");
283 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
284 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
286 case ISD::GlobalAddress: {
287 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
288 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
292 case ISD::TargetGlobalAddress: {
293 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
294 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
298 case ISD::FrameIndex:
299 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
301 case ISD::TargetFrameIndex:
302 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
304 case ISD::ConstantPool:
305 Erased = ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
307 case ISD::TargetConstantPool:
308 Erased =TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
310 case ISD::BasicBlock:
311 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
313 case ISD::ExternalSymbol:
314 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
316 case ISD::TargetExternalSymbol:
317 Erased = TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
320 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
321 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
324 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
325 N->getValueType(0)));
327 case ISD::SRCVALUE: {
328 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
329 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
333 Erased = Loads.erase(std::make_pair(N->getOperand(1),
334 std::make_pair(N->getOperand(0),
335 N->getValueType(0))));
338 if (N->getNumValues() == 1) {
339 if (N->getNumOperands() == 0) {
340 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
341 N->getValueType(0)));
342 } else if (N->getNumOperands() == 1) {
344 UnaryOps.erase(std::make_pair(N->getOpcode(),
345 std::make_pair(N->getOperand(0),
346 N->getValueType(0))));
347 } else if (N->getNumOperands() == 2) {
349 BinaryOps.erase(std::make_pair(N->getOpcode(),
350 std::make_pair(N->getOperand(0),
353 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
355 OneResultNodes.erase(std::make_pair(N->getOpcode(),
356 std::make_pair(N->getValueType(0),
360 // Remove the node from the ArbitraryNodes map.
361 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
362 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
364 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
365 std::make_pair(RV, Ops)));
370 // Verify that the node was actually in one of the CSE maps, unless it has a
371 // flag result (which cannot be CSE'd) or is one of the special cases that are
372 // not subject to CSE.
373 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
374 N->getOpcode() != ISD::CALL && N->getOpcode() != ISD::CALLSEQ_START &&
375 N->getOpcode() != ISD::CALLSEQ_END && !N->isTargetOpcode()) {
378 assert(0 && "Node is not in map!");
383 /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
384 /// has been taken out and modified in some way. If the specified node already
385 /// exists in the CSE maps, do not modify the maps, but return the existing node
386 /// instead. If it doesn't exist, add it and return null.
388 SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
389 assert(N->getNumOperands() && "This is a leaf node!");
390 if (N->getOpcode() == ISD::CALLSEQ_START ||
391 N->getOpcode() == ISD::CALLSEQ_END ||
392 N->getOpcode() == ISD::HANDLENODE)
393 return 0; // Never add these nodes.
395 if (N->getNumValues() == 1) {
396 if (N->getNumOperands() == 1) {
397 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
398 std::make_pair(N->getOperand(0),
399 N->getValueType(0)))];
402 } else if (N->getNumOperands() == 2) {
403 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
404 std::make_pair(N->getOperand(0),
409 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
410 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
411 std::make_pair(N->getValueType(0), Ops))];
416 if (N->getOpcode() == ISD::LOAD) {
417 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
418 std::make_pair(N->getOperand(0),
419 N->getValueType(0)))];
423 // Remove the node from the ArbitraryNodes map.
424 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
425 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
426 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
427 std::make_pair(RV, Ops))];
437 SelectionDAG::~SelectionDAG() {
438 while (!AllNodes.empty()) {
439 SDNode *N = AllNodes.begin();
440 delete [] N->OperandList;
443 AllNodes.pop_front();
447 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
448 if (Op.getValueType() == VT) return Op;
449 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
450 return getNode(ISD::AND, Op.getValueType(), Op,
451 getConstant(Imm, Op.getValueType()));
454 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
455 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
456 // Mask out any bits that are not valid for this constant.
458 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
460 SDNode *&N = Constants[std::make_pair(Val, VT)];
461 if (N) return SDOperand(N, 0);
462 N = new ConstantSDNode(false, Val, VT);
463 AllNodes.push_back(N);
464 return SDOperand(N, 0);
467 SDOperand SelectionDAG::getString(const std::string &Val) {
468 StringSDNode *&N = StringNodes[Val];
470 N = new StringSDNode(Val);
471 AllNodes.push_back(N);
473 return SDOperand(N, 0);
476 SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
477 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
478 // Mask out any bits that are not valid for this constant.
480 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
482 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
483 if (N) return SDOperand(N, 0);
484 N = new ConstantSDNode(true, Val, VT);
485 AllNodes.push_back(N);
486 return SDOperand(N, 0);
489 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
490 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
492 Val = (float)Val; // Mask out extra precision.
494 // Do the map lookup using the actual bit pattern for the floating point
495 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
496 // we don't have issues with SNANs.
497 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
498 if (N) return SDOperand(N, 0);
499 N = new ConstantFPSDNode(Val, VT);
500 AllNodes.push_back(N);
501 return SDOperand(N, 0);
506 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
507 MVT::ValueType VT, int offset) {
508 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
509 if (N) return SDOperand(N, 0);
510 N = new GlobalAddressSDNode(false, GV, VT);
511 AllNodes.push_back(N);
512 return SDOperand(N, 0);
515 SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
516 MVT::ValueType VT, int offset) {
517 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
518 if (N) return SDOperand(N, 0);
519 N = new GlobalAddressSDNode(true, GV, VT, offset);
520 AllNodes.push_back(N);
521 return SDOperand(N, 0);
524 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
525 SDNode *&N = FrameIndices[FI];
526 if (N) return SDOperand(N, 0);
527 N = new FrameIndexSDNode(FI, VT, false);
528 AllNodes.push_back(N);
529 return SDOperand(N, 0);
532 SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
533 SDNode *&N = TargetFrameIndices[FI];
534 if (N) return SDOperand(N, 0);
535 N = new FrameIndexSDNode(FI, VT, true);
536 AllNodes.push_back(N);
537 return SDOperand(N, 0);
540 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
541 SDNode *&N = ConstantPoolIndices[C];
542 if (N) return SDOperand(N, 0);
543 N = new ConstantPoolSDNode(C, VT, false);
544 AllNodes.push_back(N);
545 return SDOperand(N, 0);
548 SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
549 SDNode *&N = TargetConstantPoolIndices[C];
550 if (N) return SDOperand(N, 0);
551 N = new ConstantPoolSDNode(C, VT, true);
552 AllNodes.push_back(N);
553 return SDOperand(N, 0);
556 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
557 SDNode *&N = BBNodes[MBB];
558 if (N) return SDOperand(N, 0);
559 N = new BasicBlockSDNode(MBB);
560 AllNodes.push_back(N);
561 return SDOperand(N, 0);
564 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
565 if ((unsigned)VT >= ValueTypeNodes.size())
566 ValueTypeNodes.resize(VT+1);
567 if (ValueTypeNodes[VT] == 0) {
568 ValueTypeNodes[VT] = new VTSDNode(VT);
569 AllNodes.push_back(ValueTypeNodes[VT]);
572 return SDOperand(ValueTypeNodes[VT], 0);
575 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
576 SDNode *&N = ExternalSymbols[Sym];
577 if (N) return SDOperand(N, 0);
578 N = new ExternalSymbolSDNode(false, Sym, VT);
579 AllNodes.push_back(N);
580 return SDOperand(N, 0);
583 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT::ValueType VT) {
584 SDNode *&N = TargetExternalSymbols[Sym];
585 if (N) return SDOperand(N, 0);
586 N = new ExternalSymbolSDNode(true, Sym, VT);
587 AllNodes.push_back(N);
588 return SDOperand(N, 0);
591 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
592 if ((unsigned)Cond >= CondCodeNodes.size())
593 CondCodeNodes.resize(Cond+1);
595 if (CondCodeNodes[Cond] == 0) {
596 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
597 AllNodes.push_back(CondCodeNodes[Cond]);
599 return SDOperand(CondCodeNodes[Cond], 0);
602 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
603 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
605 Reg = new RegisterSDNode(RegNo, VT);
606 AllNodes.push_back(Reg);
608 return SDOperand(Reg, 0);
611 SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
612 SDOperand N2, ISD::CondCode Cond) {
613 // These setcc operations always fold.
617 case ISD::SETFALSE2: return getConstant(0, VT);
619 case ISD::SETTRUE2: return getConstant(1, VT);
622 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
623 uint64_t C2 = N2C->getValue();
624 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
625 uint64_t C1 = N1C->getValue();
627 // Sign extend the operands if required
628 if (ISD::isSignedIntSetCC(Cond)) {
629 C1 = N1C->getSignExtended();
630 C2 = N2C->getSignExtended();
634 default: assert(0 && "Unknown integer setcc!");
635 case ISD::SETEQ: return getConstant(C1 == C2, VT);
636 case ISD::SETNE: return getConstant(C1 != C2, VT);
637 case ISD::SETULT: return getConstant(C1 < C2, VT);
638 case ISD::SETUGT: return getConstant(C1 > C2, VT);
639 case ISD::SETULE: return getConstant(C1 <= C2, VT);
640 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
641 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
642 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
643 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
644 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
647 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
648 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
649 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
651 // If the comparison constant has bits in the upper part, the
652 // zero-extended value could never match.
653 if (C2 & (~0ULL << InSize)) {
654 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
658 case ISD::SETEQ: return getConstant(0, VT);
661 case ISD::SETNE: return getConstant(1, VT);
664 // True if the sign bit of C2 is set.
665 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
668 // True if the sign bit of C2 isn't set.
669 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
675 // Otherwise, we can perform the comparison with the low bits.
683 return getSetCC(VT, N1.getOperand(0),
684 getConstant(C2, N1.getOperand(0).getValueType()),
687 break; // todo, be more careful with signed comparisons
689 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
690 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
691 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
692 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
693 MVT::ValueType ExtDstTy = N1.getValueType();
694 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
696 // If the extended part has any inconsistent bits, it cannot ever
697 // compare equal. In other words, they have to be all ones or all
700 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
701 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
702 return getConstant(Cond == ISD::SETNE, VT);
704 // Otherwise, make this a use of a zext.
705 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
706 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
710 uint64_t MinVal, MaxVal;
711 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
712 if (ISD::isSignedIntSetCC(Cond)) {
713 MinVal = 1ULL << (OperandBitSize-1);
714 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
715 MaxVal = ~0ULL >> (65-OperandBitSize);
720 MaxVal = ~0ULL >> (64-OperandBitSize);
723 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
724 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
725 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
726 --C2; // X >= C1 --> X > (C1-1)
727 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
728 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
731 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
732 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
733 ++C2; // X <= C1 --> X < (C1+1)
734 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
735 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
738 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
739 return getConstant(0, VT); // X < MIN --> false
741 // Canonicalize setgt X, Min --> setne X, Min
742 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
743 return getSetCC(VT, N1, N2, ISD::SETNE);
745 // If we have setult X, 1, turn it into seteq X, 0
746 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
747 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
749 // If we have setugt X, Max-1, turn it into seteq X, Max
750 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
751 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
754 // If we have "setcc X, C1", check to see if we can shrink the immediate
757 // SETUGT X, SINTMAX -> SETLT X, 0
758 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
759 C2 == (~0ULL >> (65-OperandBitSize)))
760 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
762 // FIXME: Implement the rest of these.
765 // Fold bit comparisons when we can.
766 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
767 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
768 if (ConstantSDNode *AndRHS =
769 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
770 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
771 // Perform the xform if the AND RHS is a single bit.
772 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
773 return getNode(ISD::SRL, VT, N1,
774 getConstant(Log2_64(AndRHS->getValue()),
775 TLI.getShiftAmountTy()));
777 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
778 // (X & 8) == 8 --> (X & 8) >> 3
779 // Perform the xform if C2 is a single bit.
780 if ((C2 & (C2-1)) == 0) {
781 return getNode(ISD::SRL, VT, N1,
782 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
787 } else if (isa<ConstantSDNode>(N1.Val)) {
788 // Ensure that the constant occurs on the RHS.
789 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
792 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
793 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
794 double C1 = N1C->getValue(), C2 = N2C->getValue();
797 default: break; // FIXME: Implement the rest of these!
798 case ISD::SETEQ: return getConstant(C1 == C2, VT);
799 case ISD::SETNE: return getConstant(C1 != C2, VT);
800 case ISD::SETLT: return getConstant(C1 < C2, VT);
801 case ISD::SETGT: return getConstant(C1 > C2, VT);
802 case ISD::SETLE: return getConstant(C1 <= C2, VT);
803 case ISD::SETGE: return getConstant(C1 >= C2, VT);
806 // Ensure that the constant occurs on the RHS.
807 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
810 // Could not fold it.
814 /// getNode - Gets or creates the specified node.
816 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
817 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
819 N = new SDNode(Opcode, VT);
820 AllNodes.push_back(N);
822 return SDOperand(N, 0);
825 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
827 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
828 uint64_t Val = C->getValue();
831 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
832 case ISD::ANY_EXTEND:
833 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
834 case ISD::TRUNCATE: return getConstant(Val, VT);
835 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
836 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
840 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
843 return getConstantFP(-C->getValue(), VT);
846 return getConstantFP(C->getValue(), VT);
847 case ISD::FP_TO_SINT:
848 return getConstant((int64_t)C->getValue(), VT);
849 case ISD::FP_TO_UINT:
850 return getConstant((uint64_t)C->getValue(), VT);
853 unsigned OpOpcode = Operand.Val->getOpcode();
855 case ISD::TokenFactor:
856 return Operand; // Factor of one node? No factor.
857 case ISD::SIGN_EXTEND:
858 if (Operand.getValueType() == VT) return Operand; // noop extension
859 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
860 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
862 case ISD::ZERO_EXTEND:
863 if (Operand.getValueType() == VT) return Operand; // noop extension
864 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
865 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
867 case ISD::ANY_EXTEND:
868 if (Operand.getValueType() == VT) return Operand; // noop extension
869 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
870 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
871 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
874 if (Operand.getValueType() == VT) return Operand; // noop truncate
875 if (OpOpcode == ISD::TRUNCATE)
876 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
877 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
878 OpOpcode == ISD::ANY_EXTEND) {
879 // If the source is smaller than the dest, we still need an extend.
880 if (Operand.Val->getOperand(0).getValueType() < VT)
881 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
882 else if (Operand.Val->getOperand(0).getValueType() > VT)
883 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
885 return Operand.Val->getOperand(0);
889 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
890 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
891 Operand.Val->getOperand(0));
892 if (OpOpcode == ISD::FNEG) // --X -> X
893 return Operand.Val->getOperand(0);
896 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
897 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
902 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
903 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
904 if (E) return SDOperand(E, 0);
905 E = N = new SDNode(Opcode, Operand);
907 N = new SDNode(Opcode, Operand);
909 N->setValueTypes(VT);
910 AllNodes.push_back(N);
911 return SDOperand(N, 0);
916 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
917 SDOperand N1, SDOperand N2) {
920 case ISD::TokenFactor:
921 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
922 N2.getValueType() == MVT::Other && "Invalid token factor!");
931 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
938 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
945 assert(N1.getValueType() == N2.getValueType() &&
946 N1.getValueType() == VT && "Binary operator types must match!");
952 assert(VT == N1.getValueType() &&
953 "Shift operators return type must be the same as their first arg");
954 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
955 VT != MVT::i1 && "Shifts only work on integers");
957 case ISD::FP_ROUND_INREG: {
958 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
959 assert(VT == N1.getValueType() && "Not an inreg round!");
960 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
961 "Cannot FP_ROUND_INREG integer types");
962 assert(EVT <= VT && "Not rounding down!");
965 case ISD::AssertSext:
966 case ISD::AssertZext:
967 case ISD::SIGN_EXTEND_INREG: {
968 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
969 assert(VT == N1.getValueType() && "Not an inreg extend!");
970 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
971 "Cannot *_EXTEND_INREG FP types");
972 assert(EVT <= VT && "Not extending!");
979 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
980 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
983 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
985 case ISD::ADD: return getConstant(C1 + C2, VT);
986 case ISD::SUB: return getConstant(C1 - C2, VT);
987 case ISD::MUL: return getConstant(C1 * C2, VT);
989 if (C2) return getConstant(C1 / C2, VT);
992 if (C2) return getConstant(C1 % C2, VT);
995 if (C2) return getConstant(N1C->getSignExtended() /
996 N2C->getSignExtended(), VT);
999 if (C2) return getConstant(N1C->getSignExtended() %
1000 N2C->getSignExtended(), VT);
1002 case ISD::AND : return getConstant(C1 & C2, VT);
1003 case ISD::OR : return getConstant(C1 | C2, VT);
1004 case ISD::XOR : return getConstant(C1 ^ C2, VT);
1005 case ISD::SHL : return getConstant(C1 << C2, VT);
1006 case ISD::SRL : return getConstant(C1 >> C2, VT);
1007 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
1010 } else { // Cannonicalize constant to RHS if commutative
1011 if (isCommutativeBinOp(Opcode)) {
1012 std::swap(N1C, N2C);
1018 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1019 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1022 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1024 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1025 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1026 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1028 if (C2) return getConstantFP(C1 / C2, VT);
1031 if (C2) return getConstantFP(fmod(C1, C2), VT);
1035 } else { // Cannonicalize constant to RHS if commutative
1036 if (isCommutativeBinOp(Opcode)) {
1037 std::swap(N1CFP, N2CFP);
1043 // Finally, fold operations that do not require constants.
1045 case ISD::FP_ROUND_INREG:
1046 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1048 case ISD::SIGN_EXTEND_INREG: {
1049 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1050 if (EVT == VT) return N1; // Not actually extending
1054 // FIXME: figure out how to safely handle things like
1055 // int foo(int x) { return 1 << (x & 255); }
1056 // int bar() { return foo(256); }
1061 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1062 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
1063 return getNode(Opcode, VT, N1, N2.getOperand(0));
1064 else if (N2.getOpcode() == ISD::AND)
1065 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1066 // If the and is only masking out bits that cannot effect the shift,
1067 // eliminate the and.
1068 unsigned NumBits = MVT::getSizeInBits(VT);
1069 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1070 return getNode(Opcode, VT, N1, N2.getOperand(0));
1076 // Memoize this node if possible.
1078 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END &&
1080 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1081 if (BON) return SDOperand(BON, 0);
1083 BON = N = new SDNode(Opcode, N1, N2);
1085 N = new SDNode(Opcode, N1, N2);
1088 N->setValueTypes(VT);
1089 AllNodes.push_back(N);
1090 return SDOperand(N, 0);
1093 // setAdjCallChain - This method changes the token chain of an
1094 // CALLSEQ_START/END node to be the specified operand.
1095 void SDNode::setAdjCallChain(SDOperand N) {
1096 assert(N.getValueType() == MVT::Other);
1097 assert((getOpcode() == ISD::CALLSEQ_START ||
1098 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
1100 OperandList[0].Val->removeUser(this);
1102 OperandList[0].Val->Uses.push_back(this);
1107 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1108 SDOperand Chain, SDOperand Ptr,
1110 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1111 if (N) return SDOperand(N, 0);
1112 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1114 // Loads have a token chain.
1115 setNodeValueTypes(N, VT, MVT::Other);
1116 AllNodes.push_back(N);
1117 return SDOperand(N, 0);
1120 SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1121 SDOperand Chain, SDOperand Ptr,
1123 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1124 if (N) return SDOperand(N, 0);
1125 std::vector<SDOperand> Ops;
1127 Ops.push_back(Chain);
1129 Ops.push_back(getConstant(Count, MVT::i32));
1130 Ops.push_back(getValueType(EVT));
1132 std::vector<MVT::ValueType> VTs;
1134 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1135 return getNode(ISD::VLOAD, VTs, Ops);
1138 SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1139 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1140 MVT::ValueType EVT) {
1141 std::vector<SDOperand> Ops;
1143 Ops.push_back(Chain);
1146 Ops.push_back(getValueType(EVT));
1147 std::vector<MVT::ValueType> VTs;
1149 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1150 return getNode(Opcode, VTs, Ops);
1153 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1154 SDOperand N1, SDOperand N2, SDOperand N3) {
1155 // Perform various simplifications.
1156 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1157 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1158 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1161 // Use SimplifySetCC to simplify SETCC's.
1162 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
1163 if (Simp.Val) return Simp;
1168 if (N1C->getValue())
1169 return N2; // select true, X, Y -> X
1171 return N3; // select false, X, Y -> Y
1173 if (N2 == N3) return N2; // select C, X, X -> X
1177 if (N2C->getValue()) // Unconditional branch
1178 return getNode(ISD::BR, MVT::Other, N1, N3);
1180 return N1; // Never-taken branch
1184 std::vector<SDOperand> Ops;
1190 // Memoize node if it doesn't produce a flag.
1192 if (VT != MVT::Flag) {
1193 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1194 if (E) return SDOperand(E, 0);
1195 E = N = new SDNode(Opcode, N1, N2, N3);
1197 N = new SDNode(Opcode, N1, N2, N3);
1199 N->setValueTypes(VT);
1200 AllNodes.push_back(N);
1201 return SDOperand(N, 0);
1204 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1205 SDOperand N1, SDOperand N2, SDOperand N3,
1207 std::vector<SDOperand> Ops;
1213 return getNode(Opcode, VT, Ops);
1216 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1217 SDOperand N1, SDOperand N2, SDOperand N3,
1218 SDOperand N4, SDOperand N5) {
1219 std::vector<SDOperand> Ops;
1226 return getNode(Opcode, VT, Ops);
1230 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
1231 assert((!V || isa<PointerType>(V->getType())) &&
1232 "SrcValue is not a pointer?");
1233 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1234 if (N) return SDOperand(N, 0);
1236 N = new SrcValueSDNode(V, Offset);
1237 AllNodes.push_back(N);
1238 return SDOperand(N, 0);
1241 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1242 std::vector<SDOperand> &Ops) {
1243 switch (Ops.size()) {
1244 case 0: return getNode(Opcode, VT);
1245 case 1: return getNode(Opcode, VT, Ops[0]);
1246 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1247 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
1251 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
1254 case ISD::BRCONDTWOWAY:
1256 if (N1C->getValue()) // Unconditional branch to true dest.
1257 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
1258 else // Unconditional branch to false dest.
1259 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
1261 case ISD::BRTWOWAY_CC:
1262 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1263 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1264 "LHS and RHS of comparison must have same type!");
1266 case ISD::TRUNCSTORE: {
1267 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1268 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1269 #if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1270 // If this is a truncating store of a constant, convert to the desired type
1271 // and store it instead.
1272 if (isa<Constant>(Ops[0])) {
1273 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1274 if (isa<Constant>(Op))
1277 // Also for ConstantFP?
1279 if (Ops[0].getValueType() == EVT) // Normal store?
1280 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1281 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1282 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1283 "Can't do FP-INT conversion!");
1286 case ISD::SELECT_CC: {
1287 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
1288 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1289 "LHS and RHS of condition must have same type!");
1290 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1291 "True and False arms of SelectCC must have same type!");
1292 assert(Ops[2].getValueType() == VT &&
1293 "select_cc node must be of same type as true and false value!");
1297 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
1298 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1299 "LHS/RHS of comparison should match types!");
1306 if (VT != MVT::Flag) {
1308 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1309 if (E) return SDOperand(E, 0);
1310 E = N = new SDNode(Opcode, Ops);
1312 N = new SDNode(Opcode, Ops);
1314 N->setValueTypes(VT);
1315 AllNodes.push_back(N);
1316 return SDOperand(N, 0);
1319 SDOperand SelectionDAG::getNode(unsigned Opcode,
1320 std::vector<MVT::ValueType> &ResultTys,
1321 std::vector<SDOperand> &Ops) {
1322 if (ResultTys.size() == 1)
1323 return getNode(Opcode, ResultTys[0], Ops);
1328 case ISD::ZEXTLOAD: {
1329 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1330 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1331 // If they are asking for an extending load from/to the same thing, return a
1333 if (ResultTys[0] == EVT)
1334 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1335 assert(EVT < ResultTys[0] &&
1336 "Should only be an extending load, not truncating!");
1337 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1338 "Cannot sign/zero extend a FP load!");
1339 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1340 "Cannot convert from FP to Int or Int -> FP!");
1344 // FIXME: figure out how to safely handle things like
1345 // int foo(int x) { return 1 << (x & 255); }
1346 // int bar() { return foo(256); }
1348 case ISD::SRA_PARTS:
1349 case ISD::SRL_PARTS:
1350 case ISD::SHL_PARTS:
1351 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1352 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
1353 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1354 else if (N3.getOpcode() == ISD::AND)
1355 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1356 // If the and is only masking out bits that cannot effect the shift,
1357 // eliminate the and.
1358 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1359 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1360 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1366 // Memoize the node unless it returns a flag.
1368 if (ResultTys.back() != MVT::Flag) {
1370 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1371 if (E) return SDOperand(E, 0);
1372 E = N = new SDNode(Opcode, Ops);
1374 N = new SDNode(Opcode, Ops);
1376 setNodeValueTypes(N, ResultTys);
1377 AllNodes.push_back(N);
1378 return SDOperand(N, 0);
1381 void SelectionDAG::setNodeValueTypes(SDNode *N,
1382 std::vector<MVT::ValueType> &RetVals) {
1383 switch (RetVals.size()) {
1385 case 1: N->setValueTypes(RetVals[0]); return;
1386 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1390 std::list<std::vector<MVT::ValueType> >::iterator I =
1391 std::find(VTList.begin(), VTList.end(), RetVals);
1392 if (I == VTList.end()) {
1393 VTList.push_front(RetVals);
1397 N->setValueTypes(&(*I)[0], I->size());
1400 void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1401 MVT::ValueType VT2) {
1402 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1403 E = VTList.end(); I != E; ++I) {
1404 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1405 N->setValueTypes(&(*I)[0], 2);
1409 std::vector<MVT::ValueType> V;
1412 VTList.push_front(V);
1413 N->setValueTypes(&(*VTList.begin())[0], 2);
1417 /// SelectNodeTo - These are used for target selectors to *mutate* the
1418 /// specified node to have the specified return type, Target opcode, and
1419 /// operands. Note that target opcodes are stored as
1420 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1422 /// Note that SelectNodeTo returns the resultant node. If there is already a
1423 /// node of the specified opcode and operands, it returns that node instead of
1424 /// the current one.
1425 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1426 MVT::ValueType VT) {
1427 // If an identical node already exists, use it.
1428 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1429 if (ON) return SDOperand(ON, 0);
1431 RemoveNodeFromCSEMaps(N);
1433 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1434 N->setValueTypes(VT);
1436 ON = N; // Memoize the new node.
1437 return SDOperand(N, 0);
1440 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1441 MVT::ValueType VT, SDOperand Op1) {
1442 // If an identical node already exists, use it.
1443 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1444 std::make_pair(Op1, VT))];
1445 if (ON) return SDOperand(ON, 0);
1447 RemoveNodeFromCSEMaps(N);
1448 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1449 N->setValueTypes(VT);
1450 N->setOperands(Op1);
1452 ON = N; // Memoize the new node.
1453 return SDOperand(N, 0);
1456 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1457 MVT::ValueType VT, SDOperand Op1,
1459 // If an identical node already exists, use it.
1460 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1461 std::make_pair(Op1, Op2))];
1462 if (ON) return SDOperand(ON, 0);
1464 RemoveNodeFromCSEMaps(N);
1465 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1466 N->setValueTypes(VT);
1467 N->setOperands(Op1, Op2);
1469 ON = N; // Memoize the new node.
1470 return SDOperand(N, 0);
1473 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1474 MVT::ValueType VT, SDOperand Op1,
1475 SDOperand Op2, SDOperand Op3) {
1476 // If an identical node already exists, use it.
1477 std::vector<SDOperand> OpList;
1478 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1479 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1480 std::make_pair(VT, OpList))];
1481 if (ON) return SDOperand(ON, 0);
1483 RemoveNodeFromCSEMaps(N);
1484 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1485 N->setValueTypes(VT);
1486 N->setOperands(Op1, Op2, Op3);
1488 ON = N; // Memoize the new node.
1489 return SDOperand(N, 0);
1492 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1493 MVT::ValueType VT, SDOperand Op1,
1494 SDOperand Op2, SDOperand Op3,
1496 // If an identical node already exists, use it.
1497 std::vector<SDOperand> OpList;
1498 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1499 OpList.push_back(Op4);
1500 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1501 std::make_pair(VT, OpList))];
1502 if (ON) return SDOperand(ON, 0);
1504 RemoveNodeFromCSEMaps(N);
1505 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1506 N->setValueTypes(VT);
1507 N->setOperands(Op1, Op2, Op3, Op4);
1509 ON = N; // Memoize the new node.
1510 return SDOperand(N, 0);
1513 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1514 MVT::ValueType VT, SDOperand Op1,
1515 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1517 // If an identical node already exists, use it.
1518 std::vector<SDOperand> OpList;
1519 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1520 OpList.push_back(Op4); OpList.push_back(Op5);
1521 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1522 std::make_pair(VT, OpList))];
1523 if (ON) return SDOperand(ON, 0);
1525 RemoveNodeFromCSEMaps(N);
1526 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1527 N->setValueTypes(VT);
1528 N->setOperands(Op1, Op2, Op3, Op4, Op5);
1530 ON = N; // Memoize the new node.
1531 return SDOperand(N, 0);
1534 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1535 MVT::ValueType VT, SDOperand Op1,
1536 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1537 SDOperand Op5, SDOperand Op6) {
1538 // If an identical node already exists, use it.
1539 std::vector<SDOperand> OpList;
1540 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1541 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1542 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1543 std::make_pair(VT, OpList))];
1544 if (ON) return SDOperand(ON, 0);
1546 RemoveNodeFromCSEMaps(N);
1547 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1548 N->setValueTypes(VT);
1549 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
1551 ON = N; // Memoize the new node.
1552 return SDOperand(N, 0);
1555 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1556 MVT::ValueType VT1, MVT::ValueType VT2,
1557 SDOperand Op1, SDOperand Op2) {
1558 // If an identical node already exists, use it.
1559 std::vector<SDOperand> OpList;
1560 OpList.push_back(Op1); OpList.push_back(Op2);
1561 std::vector<MVT::ValueType> VTList;
1562 VTList.push_back(VT1); VTList.push_back(VT2);
1563 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1564 std::make_pair(VTList, OpList))];
1565 if (ON) return SDOperand(ON, 0);
1567 RemoveNodeFromCSEMaps(N);
1568 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1569 setNodeValueTypes(N, VT1, VT2);
1570 N->setOperands(Op1, Op2);
1572 ON = N; // Memoize the new node.
1573 return SDOperand(N, 0);
1576 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1577 MVT::ValueType VT1, MVT::ValueType VT2,
1578 SDOperand Op1, SDOperand Op2,
1580 // If an identical node already exists, use it.
1581 std::vector<SDOperand> OpList;
1582 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1583 std::vector<MVT::ValueType> VTList;
1584 VTList.push_back(VT1); VTList.push_back(VT2);
1585 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1586 std::make_pair(VTList, OpList))];
1587 if (ON) return SDOperand(ON, 0);
1589 RemoveNodeFromCSEMaps(N);
1590 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1591 setNodeValueTypes(N, VT1, VT2);
1592 N->setOperands(Op1, Op2, Op3);
1594 ON = N; // Memoize the new node.
1595 return SDOperand(N, 0);
1598 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1599 MVT::ValueType VT1, MVT::ValueType VT2,
1600 SDOperand Op1, SDOperand Op2,
1601 SDOperand Op3, SDOperand Op4) {
1602 // If an identical node already exists, use it.
1603 std::vector<SDOperand> OpList;
1604 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1605 OpList.push_back(Op4);
1606 std::vector<MVT::ValueType> VTList;
1607 VTList.push_back(VT1); VTList.push_back(VT2);
1608 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1609 std::make_pair(VTList, OpList))];
1610 if (ON) return SDOperand(ON, 0);
1612 RemoveNodeFromCSEMaps(N);
1613 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1614 setNodeValueTypes(N, VT1, VT2);
1615 N->setOperands(Op1, Op2, Op3, Op4);
1617 ON = N; // Memoize the new node.
1618 return SDOperand(N, 0);
1621 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1622 MVT::ValueType VT1, MVT::ValueType VT2,
1623 SDOperand Op1, SDOperand Op2,
1624 SDOperand Op3, SDOperand Op4,
1626 // If an identical node already exists, use it.
1627 std::vector<SDOperand> OpList;
1628 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1629 OpList.push_back(Op4); OpList.push_back(Op5);
1630 std::vector<MVT::ValueType> VTList;
1631 VTList.push_back(VT1); VTList.push_back(VT2);
1632 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1633 std::make_pair(VTList, OpList))];
1634 if (ON) return SDOperand(ON, 0);
1636 RemoveNodeFromCSEMaps(N);
1637 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1638 setNodeValueTypes(N, VT1, VT2);
1639 N->setOperands(Op1, Op2, Op3, Op4, Op5);
1641 ON = N; // Memoize the new node.
1642 return SDOperand(N, 0);
1645 // ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1646 /// This can cause recursive merging of nodes in the DAG.
1648 /// This version assumes From/To have a single result value.
1650 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
1651 std::vector<SDNode*> *Deleted) {
1652 SDNode *From = FromN.Val, *To = ToN.Val;
1653 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
1654 "Cannot replace with this method!");
1655 assert(From != To && "Cannot replace uses of with self");
1657 while (!From->use_empty()) {
1658 // Process users until they are all gone.
1659 SDNode *U = *From->use_begin();
1661 // This node is about to morph, remove its old self from the CSE maps.
1662 RemoveNodeFromCSEMaps(U);
1664 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1666 if (I->Val == From) {
1667 From->removeUser(U);
1672 // Now that we have modified U, add it back to the CSE maps. If it already
1673 // exists there, recursively merge the results together.
1674 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1675 ReplaceAllUsesWith(U, Existing, Deleted);
1677 if (Deleted) Deleted->push_back(U);
1678 DeleteNodeNotInCSEMaps(U);
1683 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1684 /// This can cause recursive merging of nodes in the DAG.
1686 /// This version assumes From/To have matching types and numbers of result
1689 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
1690 std::vector<SDNode*> *Deleted) {
1691 assert(From != To && "Cannot replace uses of with self");
1692 assert(From->getNumValues() == To->getNumValues() &&
1693 "Cannot use this version of ReplaceAllUsesWith!");
1694 if (From->getNumValues() == 1) { // If possible, use the faster version.
1695 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
1699 while (!From->use_empty()) {
1700 // Process users until they are all gone.
1701 SDNode *U = *From->use_begin();
1703 // This node is about to morph, remove its old self from the CSE maps.
1704 RemoveNodeFromCSEMaps(U);
1706 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1708 if (I->Val == From) {
1709 From->removeUser(U);
1714 // Now that we have modified U, add it back to the CSE maps. If it already
1715 // exists there, recursively merge the results together.
1716 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1717 ReplaceAllUsesWith(U, Existing, Deleted);
1719 if (Deleted) Deleted->push_back(U);
1720 DeleteNodeNotInCSEMaps(U);
1725 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1726 /// This can cause recursive merging of nodes in the DAG.
1728 /// This version can replace From with any result values. To must match the
1729 /// number and types of values returned by From.
1730 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
1731 const std::vector<SDOperand> &To,
1732 std::vector<SDNode*> *Deleted) {
1733 assert(From->getNumValues() == To.size() &&
1734 "Incorrect number of values to replace with!");
1735 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
1736 // Degenerate case handled above.
1737 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
1741 while (!From->use_empty()) {
1742 // Process users until they are all gone.
1743 SDNode *U = *From->use_begin();
1745 // This node is about to morph, remove its old self from the CSE maps.
1746 RemoveNodeFromCSEMaps(U);
1748 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1750 if (I->Val == From) {
1751 const SDOperand &ToOp = To[I->ResNo];
1752 From->removeUser(U);
1754 ToOp.Val->addUser(U);
1757 // Now that we have modified U, add it back to the CSE maps. If it already
1758 // exists there, recursively merge the results together.
1759 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1760 ReplaceAllUsesWith(U, Existing, Deleted);
1762 if (Deleted) Deleted->push_back(U);
1763 DeleteNodeNotInCSEMaps(U);
1769 //===----------------------------------------------------------------------===//
1771 //===----------------------------------------------------------------------===//
1774 /// getValueTypeList - Return a pointer to the specified value type.
1776 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
1777 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
1782 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1783 /// indicated value. This method ignores uses of other values defined by this
1785 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1786 assert(Value < getNumValues() && "Bad value!");
1788 // If there is only one value, this is easy.
1789 if (getNumValues() == 1)
1790 return use_size() == NUses;
1791 if (Uses.size() < NUses) return false;
1793 SDOperand TheValue(this, Value);
1795 std::set<SDNode*> UsersHandled;
1797 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1800 if (User->getNumOperands() == 1 ||
1801 UsersHandled.insert(User).second) // First time we've seen this?
1802 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1803 if (User->getOperand(i) == TheValue) {
1805 return false; // too many uses
1810 // Found exactly the right number of uses?
1815 const char *SDNode::getOperationName(const SelectionDAG *G) const {
1816 switch (getOpcode()) {
1818 if (getOpcode() < ISD::BUILTIN_OP_END)
1819 return "<<Unknown DAG Node>>";
1822 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
1823 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
1824 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
1825 return "<<Unknown Target Node>>";
1828 case ISD::PCMARKER: return "PCMarker";
1829 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
1830 case ISD::SRCVALUE: return "SrcValue";
1831 case ISD::VALUETYPE: return "ValueType";
1832 case ISD::STRING: return "String";
1833 case ISD::EntryToken: return "EntryToken";
1834 case ISD::TokenFactor: return "TokenFactor";
1835 case ISD::AssertSext: return "AssertSext";
1836 case ISD::AssertZext: return "AssertZext";
1837 case ISD::Constant: return "Constant";
1838 case ISD::TargetConstant: return "TargetConstant";
1839 case ISD::ConstantFP: return "ConstantFP";
1840 case ISD::GlobalAddress: return "GlobalAddress";
1841 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
1842 case ISD::FrameIndex: return "FrameIndex";
1843 case ISD::TargetFrameIndex: return "TargetFrameIndex";
1844 case ISD::BasicBlock: return "BasicBlock";
1845 case ISD::Register: return "Register";
1846 case ISD::ExternalSymbol: return "ExternalSymbol";
1847 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
1848 case ISD::ConstantPool: return "ConstantPool";
1849 case ISD::TargetConstantPool: return "TargetConstantPool";
1850 case ISD::CopyToReg: return "CopyToReg";
1851 case ISD::CopyFromReg: return "CopyFromReg";
1852 case ISD::ImplicitDef: return "ImplicitDef";
1853 case ISD::UNDEF: return "undef";
1856 case ISD::FABS: return "fabs";
1857 case ISD::FNEG: return "fneg";
1858 case ISD::FSQRT: return "fsqrt";
1859 case ISD::FSIN: return "fsin";
1860 case ISD::FCOS: return "fcos";
1863 case ISD::ADD: return "add";
1864 case ISD::SUB: return "sub";
1865 case ISD::MUL: return "mul";
1866 case ISD::MULHU: return "mulhu";
1867 case ISD::MULHS: return "mulhs";
1868 case ISD::SDIV: return "sdiv";
1869 case ISD::UDIV: return "udiv";
1870 case ISD::SREM: return "srem";
1871 case ISD::UREM: return "urem";
1872 case ISD::AND: return "and";
1873 case ISD::OR: return "or";
1874 case ISD::XOR: return "xor";
1875 case ISD::SHL: return "shl";
1876 case ISD::SRA: return "sra";
1877 case ISD::SRL: return "srl";
1878 case ISD::FADD: return "fadd";
1879 case ISD::FSUB: return "fsub";
1880 case ISD::FMUL: return "fmul";
1881 case ISD::FDIV: return "fdiv";
1882 case ISD::FREM: return "frem";
1883 case ISD::VADD: return "vadd";
1884 case ISD::VSUB: return "vsub";
1885 case ISD::VMUL: return "vmul";
1887 case ISD::SETCC: return "setcc";
1888 case ISD::SELECT: return "select";
1889 case ISD::SELECT_CC: return "select_cc";
1890 case ISD::ADD_PARTS: return "add_parts";
1891 case ISD::SUB_PARTS: return "sub_parts";
1892 case ISD::SHL_PARTS: return "shl_parts";
1893 case ISD::SRA_PARTS: return "sra_parts";
1894 case ISD::SRL_PARTS: return "srl_parts";
1896 // Conversion operators.
1897 case ISD::SIGN_EXTEND: return "sign_extend";
1898 case ISD::ZERO_EXTEND: return "zero_extend";
1899 case ISD::ANY_EXTEND: return "any_extend";
1900 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
1901 case ISD::TRUNCATE: return "truncate";
1902 case ISD::FP_ROUND: return "fp_round";
1903 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
1904 case ISD::FP_EXTEND: return "fp_extend";
1906 case ISD::SINT_TO_FP: return "sint_to_fp";
1907 case ISD::UINT_TO_FP: return "uint_to_fp";
1908 case ISD::FP_TO_SINT: return "fp_to_sint";
1909 case ISD::FP_TO_UINT: return "fp_to_uint";
1911 // Control flow instructions
1912 case ISD::BR: return "br";
1913 case ISD::BRCOND: return "brcond";
1914 case ISD::BRCONDTWOWAY: return "brcondtwoway";
1915 case ISD::BR_CC: return "br_cc";
1916 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
1917 case ISD::RET: return "ret";
1918 case ISD::CALL: return "call";
1919 case ISD::TAILCALL:return "tailcall";
1920 case ISD::CALLSEQ_START: return "callseq_start";
1921 case ISD::CALLSEQ_END: return "callseq_end";
1924 case ISD::LOAD: return "load";
1925 case ISD::STORE: return "store";
1926 case ISD::VLOAD: return "vload";
1927 case ISD::EXTLOAD: return "extload";
1928 case ISD::SEXTLOAD: return "sextload";
1929 case ISD::ZEXTLOAD: return "zextload";
1930 case ISD::TRUNCSTORE: return "truncstore";
1932 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1933 case ISD::EXTRACT_ELEMENT: return "extract_element";
1934 case ISD::BUILD_PAIR: return "build_pair";
1935 case ISD::MEMSET: return "memset";
1936 case ISD::MEMCPY: return "memcpy";
1937 case ISD::MEMMOVE: return "memmove";
1940 case ISD::CTPOP: return "ctpop";
1941 case ISD::CTTZ: return "cttz";
1942 case ISD::CTLZ: return "ctlz";
1945 case ISD::READPORT: return "readport";
1946 case ISD::WRITEPORT: return "writeport";
1947 case ISD::READIO: return "readio";
1948 case ISD::WRITEIO: return "writeio";
1951 case ISD::LOCATION: return "location";
1954 switch (cast<CondCodeSDNode>(this)->get()) {
1955 default: assert(0 && "Unknown setcc condition!");
1956 case ISD::SETOEQ: return "setoeq";
1957 case ISD::SETOGT: return "setogt";
1958 case ISD::SETOGE: return "setoge";
1959 case ISD::SETOLT: return "setolt";
1960 case ISD::SETOLE: return "setole";
1961 case ISD::SETONE: return "setone";
1963 case ISD::SETO: return "seto";
1964 case ISD::SETUO: return "setuo";
1965 case ISD::SETUEQ: return "setue";
1966 case ISD::SETUGT: return "setugt";
1967 case ISD::SETUGE: return "setuge";
1968 case ISD::SETULT: return "setult";
1969 case ISD::SETULE: return "setule";
1970 case ISD::SETUNE: return "setune";
1972 case ISD::SETEQ: return "seteq";
1973 case ISD::SETGT: return "setgt";
1974 case ISD::SETGE: return "setge";
1975 case ISD::SETLT: return "setlt";
1976 case ISD::SETLE: return "setle";
1977 case ISD::SETNE: return "setne";
1982 void SDNode::dump() const { dump(0); }
1983 void SDNode::dump(const SelectionDAG *G) const {
1984 std::cerr << (void*)this << ": ";
1986 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1987 if (i) std::cerr << ",";
1988 if (getValueType(i) == MVT::Other)
1991 std::cerr << MVT::getValueTypeString(getValueType(i));
1993 std::cerr << " = " << getOperationName(G);
1996 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1997 if (i) std::cerr << ", ";
1998 std::cerr << (void*)getOperand(i).Val;
1999 if (unsigned RN = getOperand(i).ResNo)
2000 std::cerr << ":" << RN;
2003 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2004 std::cerr << "<" << CSDN->getValue() << ">";
2005 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2006 std::cerr << "<" << CSDN->getValue() << ">";
2007 } else if (const GlobalAddressSDNode *GADN =
2008 dyn_cast<GlobalAddressSDNode>(this)) {
2009 int offset = GADN->getOffset();
2011 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
2013 std::cerr << " + " << offset;
2015 std::cerr << " " << offset;
2016 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
2017 std::cerr << "<" << FIDN->getIndex() << ">";
2018 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
2019 std::cerr << "<" << *CP->get() << ">";
2020 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
2022 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2024 std::cerr << LBB->getName() << " ";
2025 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
2026 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
2027 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2028 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2030 std::cerr << " #" << R->getReg();
2032 } else if (const ExternalSymbolSDNode *ES =
2033 dyn_cast<ExternalSymbolSDNode>(this)) {
2034 std::cerr << "'" << ES->getSymbol() << "'";
2035 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2037 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2039 std::cerr << "<null:" << M->getOffset() << ">";
2040 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2041 std::cerr << ":" << getValueTypeString(N->getVT());
2045 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
2046 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2047 if (N->getOperand(i).Val->hasOneUse())
2048 DumpNodes(N->getOperand(i).Val, indent+2, G);
2050 std::cerr << "\n" << std::string(indent+2, ' ')
2051 << (void*)N->getOperand(i).Val << ": <multiple use>";
2054 std::cerr << "\n" << std::string(indent, ' ');
2058 void SelectionDAG::dump() const {
2059 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
2060 std::vector<const SDNode*> Nodes;
2061 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2065 std::sort(Nodes.begin(), Nodes.end());
2067 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2068 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
2069 DumpNodes(Nodes[i], 2, this);
2072 DumpNodes(getRoot().Val, 2, this);
2074 std::cerr << "\n\n";