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"
24 #include "llvm/ADT/StringExtras.h"
31 static bool isCommutativeBinOp(unsigned Opcode) {
39 case ISD::XOR: return true;
40 default: return false; // FIXME: Need commutative info for user ops!
44 static bool isAssociativeBinOp(unsigned Opcode) {
50 case ISD::XOR: return true;
51 default: return false; // FIXME: Need associative info for user ops!
55 // isInvertibleForFree - Return true if there is no cost to emitting the logical
56 // inverse of this node.
57 static bool isInvertibleForFree(SDOperand N) {
58 if (isa<ConstantSDNode>(N.Val)) return true;
59 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
64 //===----------------------------------------------------------------------===//
65 // ConstantFPSDNode Class
66 //===----------------------------------------------------------------------===//
68 /// isExactlyValue - We don't rely on operator== working on double values, as
69 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
70 /// As such, this method can be used to do an exact bit-for-bit comparison of
71 /// two floating point values.
72 bool ConstantFPSDNode::isExactlyValue(double V) const {
73 return DoubleToBits(V) == DoubleToBits(Value);
76 //===----------------------------------------------------------------------===//
78 //===----------------------------------------------------------------------===//
80 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
81 /// when given the operation for (X op Y).
82 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
83 // To perform this operation, we just need to swap the L and G bits of the
85 unsigned OldL = (Operation >> 2) & 1;
86 unsigned OldG = (Operation >> 1) & 1;
87 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
88 (OldL << 1) | // New G bit
89 (OldG << 2)); // New L bit.
92 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
93 /// 'op' is a valid SetCC operation.
94 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
95 unsigned Operation = Op;
97 Operation ^= 7; // Flip L, G, E bits, but not U.
99 Operation ^= 15; // Flip all of the condition bits.
100 if (Operation > ISD::SETTRUE2)
101 Operation &= ~8; // Don't let N and U bits get set.
102 return ISD::CondCode(Operation);
106 /// isSignedOp - For an integer comparison, return 1 if the comparison is a
107 /// signed operation and 2 if the result is an unsigned comparison. Return zero
108 /// if the operation does not depend on the sign of the input (setne and seteq).
109 static int isSignedOp(ISD::CondCode Opcode) {
111 default: assert(0 && "Illegal integer setcc operation!");
113 case ISD::SETNE: return 0;
117 case ISD::SETGE: return 1;
121 case ISD::SETUGE: return 2;
125 /// getSetCCOrOperation - Return the result of a logical OR between different
126 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
127 /// returns SETCC_INVALID if it is not possible to represent the resultant
129 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
131 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
132 // Cannot fold a signed integer setcc with an unsigned integer setcc.
133 return ISD::SETCC_INVALID;
135 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
137 // If the N and U bits get set then the resultant comparison DOES suddenly
138 // care about orderedness, and is true when ordered.
139 if (Op > ISD::SETTRUE2)
140 Op &= ~16; // Clear the N bit.
141 return ISD::CondCode(Op);
144 /// getSetCCAndOperation - Return the result of a logical AND between different
145 /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
146 /// function returns zero if it is not possible to represent the resultant
148 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
150 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
151 // Cannot fold a signed setcc with an unsigned setcc.
152 return ISD::SETCC_INVALID;
154 // Combine all of the condition bits.
155 return ISD::CondCode(Op1 & Op2);
158 const TargetMachine &SelectionDAG::getTarget() const {
159 return TLI.getTargetMachine();
162 //===----------------------------------------------------------------------===//
163 // SelectionDAG Class
164 //===----------------------------------------------------------------------===//
166 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
167 /// SelectionDAG, including nodes (like loads) that have uses of their token
168 /// chain but no other uses and no side effect. If a node is passed in as an
169 /// argument, it is used as the seed for node deletion.
170 void SelectionDAG::RemoveDeadNodes(SDNode *N) {
171 // Create a dummy node (which is not added to allnodes), that adds a reference
172 // to the root node, preventing it from being deleted.
173 HandleSDNode Dummy(getRoot());
175 bool MadeChange = false;
177 // If we have a hint to start from, use it.
178 if (N && N->use_empty()) {
183 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
184 if (I->use_empty() && I->getOpcode() != 65535) {
185 // Node is dead, recursively delete newly dead uses.
190 // Walk the nodes list, removing the nodes we've marked as dead.
192 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
199 // If the root changed (e.g. it was a dead load, update the root).
200 setRoot(Dummy.getValue());
203 /// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
204 /// graph. If it is the last user of any of its operands, recursively process
205 /// them the same way.
207 void SelectionDAG::DestroyDeadNode(SDNode *N) {
208 // Okay, we really are going to delete this node. First take this out of the
209 // appropriate CSE map.
210 RemoveNodeFromCSEMaps(N);
212 // Next, brutally remove the operand list. This is safe to do, as there are
213 // no cycles in the graph.
214 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
218 // Now that we removed this operand, see if there are no uses of it left.
222 delete[] N->OperandList;
226 // Mark the node as dead.
227 N->MorphNodeTo(65535);
230 void SelectionDAG::DeleteNode(SDNode *N) {
231 assert(N->use_empty() && "Cannot delete a node that is not dead!");
233 // First take this out of the appropriate CSE map.
234 RemoveNodeFromCSEMaps(N);
236 // Finally, remove uses due to operands of this node, remove from the
237 // AllNodes list, and delete the node.
238 DeleteNodeNotInCSEMaps(N);
241 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
243 // Remove it from the AllNodes list.
246 // Drop all of the operands and decrement used nodes use counts.
247 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
248 I->Val->removeUser(N);
249 delete[] N->OperandList;
256 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
257 /// correspond to it. This is useful when we're about to delete or repurpose
258 /// the node. We don't want future request for structurally identical nodes
259 /// to return N anymore.
260 void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
262 switch (N->getOpcode()) {
263 case ISD::HANDLENODE: return; // noop.
265 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
266 N->getValueType(0)));
268 case ISD::TargetConstant:
269 Erased = TargetConstants.erase(std::make_pair(
270 cast<ConstantSDNode>(N)->getValue(),
271 N->getValueType(0)));
273 case ISD::ConstantFP: {
274 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
275 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
279 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
282 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
283 "Cond code doesn't exist!");
284 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
285 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
287 case ISD::GlobalAddress: {
288 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
289 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
293 case ISD::TargetGlobalAddress: {
294 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
295 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
299 case ISD::FrameIndex:
300 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
302 case ISD::TargetFrameIndex:
303 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
305 case ISD::ConstantPool:
306 Erased = ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
308 case ISD::TargetConstantPool:
309 Erased =TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
311 case ISD::BasicBlock:
312 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
314 case ISD::ExternalSymbol:
315 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
317 case ISD::TargetExternalSymbol:
318 Erased = TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
321 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
322 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
325 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
326 N->getValueType(0)));
328 case ISD::SRCVALUE: {
329 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
330 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
334 Erased = Loads.erase(std::make_pair(N->getOperand(1),
335 std::make_pair(N->getOperand(0),
336 N->getValueType(0))));
339 if (N->getNumValues() == 1) {
340 if (N->getNumOperands() == 0) {
341 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
342 N->getValueType(0)));
343 } else if (N->getNumOperands() == 1) {
345 UnaryOps.erase(std::make_pair(N->getOpcode(),
346 std::make_pair(N->getOperand(0),
347 N->getValueType(0))));
348 } else if (N->getNumOperands() == 2) {
350 BinaryOps.erase(std::make_pair(N->getOpcode(),
351 std::make_pair(N->getOperand(0),
354 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
356 OneResultNodes.erase(std::make_pair(N->getOpcode(),
357 std::make_pair(N->getValueType(0),
361 // Remove the node from the ArbitraryNodes map.
362 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
363 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
365 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
366 std::make_pair(RV, Ops)));
371 // Verify that the node was actually in one of the CSE maps, unless it has a
372 // flag result (which cannot be CSE'd) or is one of the special cases that are
373 // not subject to CSE.
374 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
375 N->getOpcode() != ISD::CALL && N->getOpcode() != ISD::CALLSEQ_START &&
376 N->getOpcode() != ISD::CALLSEQ_END && !N->isTargetOpcode()) {
379 assert(0 && "Node is not in map!");
384 /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
385 /// has been taken out and modified in some way. If the specified node already
386 /// exists in the CSE maps, do not modify the maps, but return the existing node
387 /// instead. If it doesn't exist, add it and return null.
389 SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
390 assert(N->getNumOperands() && "This is a leaf node!");
391 if (N->getOpcode() == ISD::CALLSEQ_START ||
392 N->getOpcode() == ISD::CALLSEQ_END ||
393 N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
394 return 0; // Never add these nodes.
396 // Check that remaining values produced are not flags.
397 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
398 if (N->getValueType(i) == MVT::Flag)
399 return 0; // Never CSE anything that produces a flag.
401 if (N->getNumValues() == 1) {
402 if (N->getNumOperands() == 1) {
403 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
404 std::make_pair(N->getOperand(0),
405 N->getValueType(0)))];
408 } else if (N->getNumOperands() == 2) {
409 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
410 std::make_pair(N->getOperand(0),
415 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
416 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
417 std::make_pair(N->getValueType(0), Ops))];
422 if (N->getOpcode() == ISD::LOAD) {
423 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
424 std::make_pair(N->getOperand(0),
425 N->getValueType(0)))];
429 // Remove the node from the ArbitraryNodes map.
430 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
431 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
432 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
433 std::make_pair(RV, Ops))];
443 SelectionDAG::~SelectionDAG() {
444 while (!AllNodes.empty()) {
445 SDNode *N = AllNodes.begin();
446 delete [] N->OperandList;
449 AllNodes.pop_front();
453 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
454 if (Op.getValueType() == VT) return Op;
455 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
456 return getNode(ISD::AND, Op.getValueType(), Op,
457 getConstant(Imm, Op.getValueType()));
460 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
461 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
462 // Mask out any bits that are not valid for this constant.
464 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
466 SDNode *&N = Constants[std::make_pair(Val, VT)];
467 if (N) return SDOperand(N, 0);
468 N = new ConstantSDNode(false, Val, VT);
469 AllNodes.push_back(N);
470 return SDOperand(N, 0);
473 SDOperand SelectionDAG::getString(const std::string &Val) {
474 StringSDNode *&N = StringNodes[Val];
476 N = new StringSDNode(Val);
477 AllNodes.push_back(N);
479 return SDOperand(N, 0);
482 SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
483 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
484 // Mask out any bits that are not valid for this constant.
486 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
488 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
489 if (N) return SDOperand(N, 0);
490 N = new ConstantSDNode(true, Val, VT);
491 AllNodes.push_back(N);
492 return SDOperand(N, 0);
495 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
496 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
498 Val = (float)Val; // Mask out extra precision.
500 // Do the map lookup using the actual bit pattern for the floating point
501 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
502 // we don't have issues with SNANs.
503 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
504 if (N) return SDOperand(N, 0);
505 N = new ConstantFPSDNode(Val, VT);
506 AllNodes.push_back(N);
507 return SDOperand(N, 0);
510 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
511 MVT::ValueType VT, int offset) {
512 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
513 if (N) return SDOperand(N, 0);
514 N = new GlobalAddressSDNode(false, GV, VT);
515 AllNodes.push_back(N);
516 return SDOperand(N, 0);
519 SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
520 MVT::ValueType VT, int offset) {
521 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
522 if (N) return SDOperand(N, 0);
523 N = new GlobalAddressSDNode(true, GV, VT, offset);
524 AllNodes.push_back(N);
525 return SDOperand(N, 0);
528 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
529 SDNode *&N = FrameIndices[FI];
530 if (N) return SDOperand(N, 0);
531 N = new FrameIndexSDNode(FI, VT, false);
532 AllNodes.push_back(N);
533 return SDOperand(N, 0);
536 SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
537 SDNode *&N = TargetFrameIndices[FI];
538 if (N) return SDOperand(N, 0);
539 N = new FrameIndexSDNode(FI, VT, true);
540 AllNodes.push_back(N);
541 return SDOperand(N, 0);
544 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
545 SDNode *&N = ConstantPoolIndices[C];
546 if (N) return SDOperand(N, 0);
547 N = new ConstantPoolSDNode(C, VT, false);
548 AllNodes.push_back(N);
549 return SDOperand(N, 0);
552 SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
553 SDNode *&N = TargetConstantPoolIndices[C];
554 if (N) return SDOperand(N, 0);
555 N = new ConstantPoolSDNode(C, VT, true);
556 AllNodes.push_back(N);
557 return SDOperand(N, 0);
560 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
561 SDNode *&N = BBNodes[MBB];
562 if (N) return SDOperand(N, 0);
563 N = new BasicBlockSDNode(MBB);
564 AllNodes.push_back(N);
565 return SDOperand(N, 0);
568 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
569 if ((unsigned)VT >= ValueTypeNodes.size())
570 ValueTypeNodes.resize(VT+1);
571 if (ValueTypeNodes[VT] == 0) {
572 ValueTypeNodes[VT] = new VTSDNode(VT);
573 AllNodes.push_back(ValueTypeNodes[VT]);
576 return SDOperand(ValueTypeNodes[VT], 0);
579 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
580 SDNode *&N = ExternalSymbols[Sym];
581 if (N) return SDOperand(N, 0);
582 N = new ExternalSymbolSDNode(false, Sym, VT);
583 AllNodes.push_back(N);
584 return SDOperand(N, 0);
587 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT::ValueType VT) {
588 SDNode *&N = TargetExternalSymbols[Sym];
589 if (N) return SDOperand(N, 0);
590 N = new ExternalSymbolSDNode(true, Sym, VT);
591 AllNodes.push_back(N);
592 return SDOperand(N, 0);
595 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
596 if ((unsigned)Cond >= CondCodeNodes.size())
597 CondCodeNodes.resize(Cond+1);
599 if (CondCodeNodes[Cond] == 0) {
600 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
601 AllNodes.push_back(CondCodeNodes[Cond]);
603 return SDOperand(CondCodeNodes[Cond], 0);
606 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
607 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
609 Reg = new RegisterSDNode(RegNo, VT);
610 AllNodes.push_back(Reg);
612 return SDOperand(Reg, 0);
615 SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
616 SDOperand N2, ISD::CondCode Cond) {
617 // These setcc operations always fold.
621 case ISD::SETFALSE2: return getConstant(0, VT);
623 case ISD::SETTRUE2: return getConstant(1, VT);
626 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
627 uint64_t C2 = N2C->getValue();
628 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
629 uint64_t C1 = N1C->getValue();
631 // Sign extend the operands if required
632 if (ISD::isSignedIntSetCC(Cond)) {
633 C1 = N1C->getSignExtended();
634 C2 = N2C->getSignExtended();
638 default: assert(0 && "Unknown integer setcc!");
639 case ISD::SETEQ: return getConstant(C1 == C2, VT);
640 case ISD::SETNE: return getConstant(C1 != C2, VT);
641 case ISD::SETULT: return getConstant(C1 < C2, VT);
642 case ISD::SETUGT: return getConstant(C1 > C2, VT);
643 case ISD::SETULE: return getConstant(C1 <= C2, VT);
644 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
645 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
646 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
647 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
648 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
651 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
652 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
653 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
655 // If the comparison constant has bits in the upper part, the
656 // zero-extended value could never match.
657 if (C2 & (~0ULL << InSize)) {
658 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
662 case ISD::SETEQ: return getConstant(0, VT);
665 case ISD::SETNE: return getConstant(1, VT);
668 // True if the sign bit of C2 is set.
669 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
672 // True if the sign bit of C2 isn't set.
673 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
679 // Otherwise, we can perform the comparison with the low bits.
687 return getSetCC(VT, N1.getOperand(0),
688 getConstant(C2, N1.getOperand(0).getValueType()),
691 break; // todo, be more careful with signed comparisons
693 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
694 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
695 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
696 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
697 MVT::ValueType ExtDstTy = N1.getValueType();
698 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
700 // If the extended part has any inconsistent bits, it cannot ever
701 // compare equal. In other words, they have to be all ones or all
704 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
705 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
706 return getConstant(Cond == ISD::SETNE, VT);
708 // Otherwise, make this a use of a zext.
709 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
710 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
714 uint64_t MinVal, MaxVal;
715 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
716 if (ISD::isSignedIntSetCC(Cond)) {
717 MinVal = 1ULL << (OperandBitSize-1);
718 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
719 MaxVal = ~0ULL >> (65-OperandBitSize);
724 MaxVal = ~0ULL >> (64-OperandBitSize);
727 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
728 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
729 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
730 --C2; // X >= C1 --> X > (C1-1)
731 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
732 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
735 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
736 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
737 ++C2; // X <= C1 --> X < (C1+1)
738 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
739 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
742 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
743 return getConstant(0, VT); // X < MIN --> false
745 // Canonicalize setgt X, Min --> setne X, Min
746 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
747 return getSetCC(VT, N1, N2, ISD::SETNE);
749 // If we have setult X, 1, turn it into seteq X, 0
750 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
751 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
753 // If we have setugt X, Max-1, turn it into seteq X, Max
754 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
755 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
758 // If we have "setcc X, C1", check to see if we can shrink the immediate
761 // SETUGT X, SINTMAX -> SETLT X, 0
762 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
763 C2 == (~0ULL >> (65-OperandBitSize)))
764 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
766 // FIXME: Implement the rest of these.
769 // Fold bit comparisons when we can.
770 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
771 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
772 if (ConstantSDNode *AndRHS =
773 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
774 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
775 // Perform the xform if the AND RHS is a single bit.
776 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
777 return getNode(ISD::SRL, VT, N1,
778 getConstant(Log2_64(AndRHS->getValue()),
779 TLI.getShiftAmountTy()));
781 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
782 // (X & 8) == 8 --> (X & 8) >> 3
783 // Perform the xform if C2 is a single bit.
784 if ((C2 & (C2-1)) == 0) {
785 return getNode(ISD::SRL, VT, N1,
786 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
791 } else if (isa<ConstantSDNode>(N1.Val)) {
792 // Ensure that the constant occurs on the RHS.
793 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
796 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
797 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
798 double C1 = N1C->getValue(), C2 = N2C->getValue();
801 default: break; // FIXME: Implement the rest of these!
802 case ISD::SETEQ: return getConstant(C1 == C2, VT);
803 case ISD::SETNE: return getConstant(C1 != C2, VT);
804 case ISD::SETLT: return getConstant(C1 < C2, VT);
805 case ISD::SETGT: return getConstant(C1 > C2, VT);
806 case ISD::SETLE: return getConstant(C1 <= C2, VT);
807 case ISD::SETGE: return getConstant(C1 >= C2, VT);
810 // Ensure that the constant occurs on the RHS.
811 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
814 // Could not fold it.
818 /// getNode - Gets or creates the specified node.
820 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
821 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
823 N = new SDNode(Opcode, VT);
824 AllNodes.push_back(N);
826 return SDOperand(N, 0);
829 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
831 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
832 uint64_t Val = C->getValue();
835 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
836 case ISD::ANY_EXTEND:
837 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
838 case ISD::TRUNCATE: return getConstant(Val, VT);
839 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
840 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
844 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
847 return getConstantFP(-C->getValue(), VT);
850 return getConstantFP(C->getValue(), VT);
851 case ISD::FP_TO_SINT:
852 return getConstant((int64_t)C->getValue(), VT);
853 case ISD::FP_TO_UINT:
854 return getConstant((uint64_t)C->getValue(), VT);
857 unsigned OpOpcode = Operand.Val->getOpcode();
859 case ISD::TokenFactor:
860 return Operand; // Factor of one node? No factor.
861 case ISD::SIGN_EXTEND:
862 if (Operand.getValueType() == VT) return Operand; // noop extension
863 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
864 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
866 case ISD::ZERO_EXTEND:
867 if (Operand.getValueType() == VT) return Operand; // noop extension
868 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
869 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
871 case ISD::ANY_EXTEND:
872 if (Operand.getValueType() == VT) return Operand; // noop extension
873 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
874 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
875 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
878 if (Operand.getValueType() == VT) return Operand; // noop truncate
879 if (OpOpcode == ISD::TRUNCATE)
880 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
881 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
882 OpOpcode == ISD::ANY_EXTEND) {
883 // If the source is smaller than the dest, we still need an extend.
884 if (Operand.Val->getOperand(0).getValueType() < VT)
885 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
886 else if (Operand.Val->getOperand(0).getValueType() > VT)
887 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
889 return Operand.Val->getOperand(0);
893 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
894 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
895 Operand.Val->getOperand(0));
896 if (OpOpcode == ISD::FNEG) // --X -> X
897 return Operand.Val->getOperand(0);
900 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
901 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
906 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
907 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
908 if (E) return SDOperand(E, 0);
909 E = N = new SDNode(Opcode, Operand);
911 N = new SDNode(Opcode, Operand);
913 N->setValueTypes(VT);
914 AllNodes.push_back(N);
915 return SDOperand(N, 0);
920 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
921 SDOperand N1, SDOperand N2) {
924 case ISD::TokenFactor:
925 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
926 N2.getValueType() == MVT::Other && "Invalid token factor!");
935 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
942 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
949 assert(N1.getValueType() == N2.getValueType() &&
950 N1.getValueType() == VT && "Binary operator types must match!");
956 assert(VT == N1.getValueType() &&
957 "Shift operators return type must be the same as their first arg");
958 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
959 VT != MVT::i1 && "Shifts only work on integers");
961 case ISD::FP_ROUND_INREG: {
962 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
963 assert(VT == N1.getValueType() && "Not an inreg round!");
964 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
965 "Cannot FP_ROUND_INREG integer types");
966 assert(EVT <= VT && "Not rounding down!");
969 case ISD::AssertSext:
970 case ISD::AssertZext:
971 case ISD::SIGN_EXTEND_INREG: {
972 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
973 assert(VT == N1.getValueType() && "Not an inreg extend!");
974 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
975 "Cannot *_EXTEND_INREG FP types");
976 assert(EVT <= VT && "Not extending!");
983 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
984 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
987 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
989 case ISD::ADD: return getConstant(C1 + C2, VT);
990 case ISD::SUB: return getConstant(C1 - C2, VT);
991 case ISD::MUL: return getConstant(C1 * C2, VT);
993 if (C2) return getConstant(C1 / C2, VT);
996 if (C2) return getConstant(C1 % C2, VT);
999 if (C2) return getConstant(N1C->getSignExtended() /
1000 N2C->getSignExtended(), VT);
1003 if (C2) return getConstant(N1C->getSignExtended() %
1004 N2C->getSignExtended(), VT);
1006 case ISD::AND : return getConstant(C1 & C2, VT);
1007 case ISD::OR : return getConstant(C1 | C2, VT);
1008 case ISD::XOR : return getConstant(C1 ^ C2, VT);
1009 case ISD::SHL : return getConstant(C1 << C2, VT);
1010 case ISD::SRL : return getConstant(C1 >> C2, VT);
1011 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
1014 } else { // Cannonicalize constant to RHS if commutative
1015 if (isCommutativeBinOp(Opcode)) {
1016 std::swap(N1C, N2C);
1022 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1023 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1026 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1028 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1029 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1030 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1032 if (C2) return getConstantFP(C1 / C2, VT);
1035 if (C2) return getConstantFP(fmod(C1, C2), VT);
1039 } else { // Cannonicalize constant to RHS if commutative
1040 if (isCommutativeBinOp(Opcode)) {
1041 std::swap(N1CFP, N2CFP);
1047 // Finally, fold operations that do not require constants.
1049 case ISD::FP_ROUND_INREG:
1050 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1052 case ISD::SIGN_EXTEND_INREG: {
1053 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1054 if (EVT == VT) return N1; // Not actually extending
1058 // FIXME: figure out how to safely handle things like
1059 // int foo(int x) { return 1 << (x & 255); }
1060 // int bar() { return foo(256); }
1065 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1066 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
1067 return getNode(Opcode, VT, N1, N2.getOperand(0));
1068 else if (N2.getOpcode() == ISD::AND)
1069 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1070 // If the and is only masking out bits that cannot effect the shift,
1071 // eliminate the and.
1072 unsigned NumBits = MVT::getSizeInBits(VT);
1073 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1074 return getNode(Opcode, VT, N1, N2.getOperand(0));
1080 // Memoize this node if possible.
1082 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END &&
1084 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1085 if (BON) return SDOperand(BON, 0);
1087 BON = N = new SDNode(Opcode, N1, N2);
1089 N = new SDNode(Opcode, N1, N2);
1092 N->setValueTypes(VT);
1093 AllNodes.push_back(N);
1094 return SDOperand(N, 0);
1097 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1098 SDOperand N1, SDOperand N2, SDOperand N3) {
1099 // Perform various simplifications.
1100 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1101 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1102 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1105 // Use SimplifySetCC to simplify SETCC's.
1106 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
1107 if (Simp.Val) return Simp;
1112 if (N1C->getValue())
1113 return N2; // select true, X, Y -> X
1115 return N3; // select false, X, Y -> Y
1117 if (N2 == N3) return N2; // select C, X, X -> X
1121 if (N2C->getValue()) // Unconditional branch
1122 return getNode(ISD::BR, MVT::Other, N1, N3);
1124 return N1; // Never-taken branch
1128 std::vector<SDOperand> Ops;
1134 // Memoize node if it doesn't produce a flag.
1136 if (VT != MVT::Flag) {
1137 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1138 if (E) return SDOperand(E, 0);
1139 E = N = new SDNode(Opcode, N1, N2, N3);
1141 N = new SDNode(Opcode, N1, N2, N3);
1143 N->setValueTypes(VT);
1144 AllNodes.push_back(N);
1145 return SDOperand(N, 0);
1148 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1149 SDOperand N1, SDOperand N2, SDOperand N3,
1151 std::vector<SDOperand> Ops;
1157 return getNode(Opcode, VT, Ops);
1160 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1161 SDOperand N1, SDOperand N2, SDOperand N3,
1162 SDOperand N4, SDOperand N5) {
1163 std::vector<SDOperand> Ops;
1170 return getNode(Opcode, VT, Ops);
1173 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1174 SDOperand N1, SDOperand N2, SDOperand N3,
1175 SDOperand N4, SDOperand N5, SDOperand N6) {
1176 std::vector<SDOperand> Ops;
1184 return getNode(Opcode, VT, Ops);
1187 // setAdjCallChain - This method changes the token chain of an
1188 // CALLSEQ_START/END node to be the specified operand.
1189 void SDNode::setAdjCallChain(SDOperand N) {
1190 assert(N.getValueType() == MVT::Other);
1191 assert((getOpcode() == ISD::CALLSEQ_START ||
1192 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
1194 OperandList[0].Val->removeUser(this);
1196 OperandList[0].Val->Uses.push_back(this);
1201 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1202 SDOperand Chain, SDOperand Ptr,
1204 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1205 if (N) return SDOperand(N, 0);
1206 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1208 // Loads have a token chain.
1209 setNodeValueTypes(N, VT, MVT::Other);
1210 AllNodes.push_back(N);
1211 return SDOperand(N, 0);
1214 SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1215 SDOperand Chain, SDOperand Ptr,
1217 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1218 if (N) return SDOperand(N, 0);
1219 std::vector<SDOperand> Ops;
1221 Ops.push_back(Chain);
1223 Ops.push_back(getConstant(Count, MVT::i32));
1224 Ops.push_back(getValueType(EVT));
1226 std::vector<MVT::ValueType> VTs;
1228 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1229 return getNode(ISD::VLOAD, VTs, Ops);
1232 SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1233 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1234 MVT::ValueType EVT) {
1235 std::vector<SDOperand> Ops;
1237 Ops.push_back(Chain);
1240 Ops.push_back(getValueType(EVT));
1241 std::vector<MVT::ValueType> VTs;
1243 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1244 return getNode(Opcode, VTs, Ops);
1247 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
1248 assert((!V || isa<PointerType>(V->getType())) &&
1249 "SrcValue is not a pointer?");
1250 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1251 if (N) return SDOperand(N, 0);
1253 N = new SrcValueSDNode(V, Offset);
1254 AllNodes.push_back(N);
1255 return SDOperand(N, 0);
1258 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1259 std::vector<SDOperand> &Ops) {
1260 switch (Ops.size()) {
1261 case 0: return getNode(Opcode, VT);
1262 case 1: return getNode(Opcode, VT, Ops[0]);
1263 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1264 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
1268 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
1271 case ISD::BRCONDTWOWAY:
1273 if (N1C->getValue()) // Unconditional branch to true dest.
1274 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
1275 else // Unconditional branch to false dest.
1276 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
1278 case ISD::BRTWOWAY_CC:
1279 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1280 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1281 "LHS and RHS of comparison must have same type!");
1283 case ISD::TRUNCSTORE: {
1284 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1285 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1286 #if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1287 // If this is a truncating store of a constant, convert to the desired type
1288 // and store it instead.
1289 if (isa<Constant>(Ops[0])) {
1290 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1291 if (isa<Constant>(Op))
1294 // Also for ConstantFP?
1296 if (Ops[0].getValueType() == EVT) // Normal store?
1297 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1298 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1299 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1300 "Can't do FP-INT conversion!");
1303 case ISD::SELECT_CC: {
1304 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
1305 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1306 "LHS and RHS of condition must have same type!");
1307 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1308 "True and False arms of SelectCC must have same type!");
1309 assert(Ops[2].getValueType() == VT &&
1310 "select_cc node must be of same type as true and false value!");
1314 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
1315 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1316 "LHS/RHS of comparison should match types!");
1323 if (VT != MVT::Flag) {
1325 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1326 if (E) return SDOperand(E, 0);
1327 E = N = new SDNode(Opcode, Ops);
1329 N = new SDNode(Opcode, Ops);
1331 N->setValueTypes(VT);
1332 AllNodes.push_back(N);
1333 return SDOperand(N, 0);
1336 SDOperand SelectionDAG::getNode(unsigned Opcode,
1337 std::vector<MVT::ValueType> &ResultTys,
1338 std::vector<SDOperand> &Ops) {
1339 if (ResultTys.size() == 1)
1340 return getNode(Opcode, ResultTys[0], Ops);
1345 case ISD::ZEXTLOAD: {
1346 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1347 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1348 // If they are asking for an extending load from/to the same thing, return a
1350 if (ResultTys[0] == EVT)
1351 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1352 assert(EVT < ResultTys[0] &&
1353 "Should only be an extending load, not truncating!");
1354 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1355 "Cannot sign/zero extend a FP load!");
1356 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1357 "Cannot convert from FP to Int or Int -> FP!");
1361 // FIXME: figure out how to safely handle things like
1362 // int foo(int x) { return 1 << (x & 255); }
1363 // int bar() { return foo(256); }
1365 case ISD::SRA_PARTS:
1366 case ISD::SRL_PARTS:
1367 case ISD::SHL_PARTS:
1368 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1369 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
1370 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1371 else if (N3.getOpcode() == ISD::AND)
1372 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1373 // If the and is only masking out bits that cannot effect the shift,
1374 // eliminate the and.
1375 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1376 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1377 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1383 // Memoize the node unless it returns a flag.
1385 if (ResultTys.back() != MVT::Flag) {
1387 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1388 if (E) return SDOperand(E, 0);
1389 E = N = new SDNode(Opcode, Ops);
1391 N = new SDNode(Opcode, Ops);
1393 setNodeValueTypes(N, ResultTys);
1394 AllNodes.push_back(N);
1395 return SDOperand(N, 0);
1398 void SelectionDAG::setNodeValueTypes(SDNode *N,
1399 std::vector<MVT::ValueType> &RetVals) {
1400 switch (RetVals.size()) {
1402 case 1: N->setValueTypes(RetVals[0]); return;
1403 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1407 std::list<std::vector<MVT::ValueType> >::iterator I =
1408 std::find(VTList.begin(), VTList.end(), RetVals);
1409 if (I == VTList.end()) {
1410 VTList.push_front(RetVals);
1414 N->setValueTypes(&(*I)[0], I->size());
1417 void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1418 MVT::ValueType VT2) {
1419 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1420 E = VTList.end(); I != E; ++I) {
1421 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1422 N->setValueTypes(&(*I)[0], 2);
1426 std::vector<MVT::ValueType> V;
1429 VTList.push_front(V);
1430 N->setValueTypes(&(*VTList.begin())[0], 2);
1434 /// SelectNodeTo - These are used for target selectors to *mutate* the
1435 /// specified node to have the specified return type, Target opcode, and
1436 /// operands. Note that target opcodes are stored as
1437 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1439 /// Note that SelectNodeTo returns the resultant node. If there is already a
1440 /// node of the specified opcode and operands, it returns that node instead of
1441 /// the current one.
1442 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1443 MVT::ValueType VT) {
1444 // If an identical node already exists, use it.
1445 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1446 if (ON) return SDOperand(ON, 0);
1448 RemoveNodeFromCSEMaps(N);
1450 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1451 N->setValueTypes(VT);
1453 ON = N; // Memoize the new node.
1454 return SDOperand(N, 0);
1457 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1458 MVT::ValueType VT, SDOperand Op1) {
1459 // If an identical node already exists, use it.
1460 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1461 std::make_pair(Op1, VT))];
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);
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,
1476 // If an identical node already exists, use it.
1477 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1478 std::make_pair(Op1, Op2))];
1479 if (ON) return SDOperand(ON, 0);
1481 RemoveNodeFromCSEMaps(N);
1482 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1483 N->setValueTypes(VT);
1484 N->setOperands(Op1, Op2);
1486 ON = N; // Memoize the new node.
1487 return SDOperand(N, 0);
1490 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1491 MVT::ValueType VT, SDOperand Op1,
1492 SDOperand Op2, SDOperand Op3) {
1493 // If an identical node already exists, use it.
1494 std::vector<SDOperand> OpList;
1495 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1496 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1497 std::make_pair(VT, OpList))];
1498 if (ON) return SDOperand(ON, 0);
1500 RemoveNodeFromCSEMaps(N);
1501 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1502 N->setValueTypes(VT);
1503 N->setOperands(Op1, Op2, Op3);
1505 ON = N; // Memoize the new node.
1506 return SDOperand(N, 0);
1509 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1510 MVT::ValueType VT, SDOperand Op1,
1511 SDOperand Op2, SDOperand Op3,
1513 // If an identical node already exists, use it.
1514 std::vector<SDOperand> OpList;
1515 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1516 OpList.push_back(Op4);
1517 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1518 std::make_pair(VT, OpList))];
1519 if (ON) return SDOperand(ON, 0);
1521 RemoveNodeFromCSEMaps(N);
1522 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1523 N->setValueTypes(VT);
1524 N->setOperands(Op1, Op2, Op3, Op4);
1526 ON = N; // Memoize the new node.
1527 return SDOperand(N, 0);
1530 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1531 MVT::ValueType VT, SDOperand Op1,
1532 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1534 // If an identical node already exists, use it.
1535 std::vector<SDOperand> OpList;
1536 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1537 OpList.push_back(Op4); OpList.push_back(Op5);
1538 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1539 std::make_pair(VT, OpList))];
1540 if (ON) return SDOperand(ON, 0);
1542 RemoveNodeFromCSEMaps(N);
1543 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1544 N->setValueTypes(VT);
1545 N->setOperands(Op1, Op2, Op3, Op4, Op5);
1547 ON = N; // Memoize the new node.
1548 return SDOperand(N, 0);
1551 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1552 MVT::ValueType VT, SDOperand Op1,
1553 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1554 SDOperand Op5, SDOperand Op6) {
1555 // If an identical node already exists, use it.
1556 std::vector<SDOperand> OpList;
1557 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1558 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1559 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1560 std::make_pair(VT, OpList))];
1561 if (ON) return SDOperand(ON, 0);
1563 RemoveNodeFromCSEMaps(N);
1564 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1565 N->setValueTypes(VT);
1566 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
1568 ON = N; // Memoize the new node.
1569 return SDOperand(N, 0);
1572 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1573 MVT::ValueType VT1, MVT::ValueType VT2,
1574 SDOperand Op1, SDOperand Op2) {
1575 // If an identical node already exists, use it.
1576 std::vector<SDOperand> OpList;
1577 OpList.push_back(Op1); OpList.push_back(Op2);
1578 std::vector<MVT::ValueType> VTList;
1579 VTList.push_back(VT1); VTList.push_back(VT2);
1580 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1581 std::make_pair(VTList, OpList))];
1582 if (ON) return SDOperand(ON, 0);
1584 RemoveNodeFromCSEMaps(N);
1585 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1586 setNodeValueTypes(N, VT1, VT2);
1587 N->setOperands(Op1, Op2);
1589 ON = N; // Memoize the new node.
1590 return SDOperand(N, 0);
1593 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1594 MVT::ValueType VT1, MVT::ValueType VT2,
1595 SDOperand Op1, SDOperand Op2,
1597 // If an identical node already exists, use it.
1598 std::vector<SDOperand> OpList;
1599 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1600 std::vector<MVT::ValueType> VTList;
1601 VTList.push_back(VT1); VTList.push_back(VT2);
1602 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1603 std::make_pair(VTList, OpList))];
1604 if (ON) return SDOperand(ON, 0);
1606 RemoveNodeFromCSEMaps(N);
1607 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1608 setNodeValueTypes(N, VT1, VT2);
1609 N->setOperands(Op1, Op2, Op3);
1611 ON = N; // Memoize the new node.
1612 return SDOperand(N, 0);
1615 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1616 MVT::ValueType VT1, MVT::ValueType VT2,
1617 SDOperand Op1, SDOperand Op2,
1618 SDOperand Op3, SDOperand Op4) {
1619 // If an identical node already exists, use it.
1620 std::vector<SDOperand> OpList;
1621 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1622 OpList.push_back(Op4);
1623 std::vector<MVT::ValueType> VTList;
1624 VTList.push_back(VT1); VTList.push_back(VT2);
1625 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1626 std::make_pair(VTList, OpList))];
1627 if (ON) return SDOperand(ON, 0);
1629 RemoveNodeFromCSEMaps(N);
1630 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1631 setNodeValueTypes(N, VT1, VT2);
1632 N->setOperands(Op1, Op2, Op3, Op4);
1634 ON = N; // Memoize the new node.
1635 return SDOperand(N, 0);
1638 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1639 MVT::ValueType VT1, MVT::ValueType VT2,
1640 SDOperand Op1, SDOperand Op2,
1641 SDOperand Op3, SDOperand Op4,
1643 // If an identical node already exists, use it.
1644 std::vector<SDOperand> OpList;
1645 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1646 OpList.push_back(Op4); OpList.push_back(Op5);
1647 std::vector<MVT::ValueType> VTList;
1648 VTList.push_back(VT1); VTList.push_back(VT2);
1649 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1650 std::make_pair(VTList, OpList))];
1651 if (ON) return SDOperand(ON, 0);
1653 RemoveNodeFromCSEMaps(N);
1654 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1655 setNodeValueTypes(N, VT1, VT2);
1656 N->setOperands(Op1, Op2, Op3, Op4, Op5);
1658 ON = N; // Memoize the new node.
1659 return SDOperand(N, 0);
1662 // ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1663 /// This can cause recursive merging of nodes in the DAG.
1665 /// This version assumes From/To have a single result value.
1667 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
1668 std::vector<SDNode*> *Deleted) {
1669 SDNode *From = FromN.Val, *To = ToN.Val;
1670 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
1671 "Cannot replace with this method!");
1672 assert(From != To && "Cannot replace uses of with self");
1674 while (!From->use_empty()) {
1675 // Process users until they are all gone.
1676 SDNode *U = *From->use_begin();
1678 // This node is about to morph, remove its old self from the CSE maps.
1679 RemoveNodeFromCSEMaps(U);
1681 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1683 if (I->Val == From) {
1684 From->removeUser(U);
1689 // Now that we have modified U, add it back to the CSE maps. If it already
1690 // exists there, recursively merge the results together.
1691 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1692 ReplaceAllUsesWith(U, Existing, Deleted);
1694 if (Deleted) Deleted->push_back(U);
1695 DeleteNodeNotInCSEMaps(U);
1700 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1701 /// This can cause recursive merging of nodes in the DAG.
1703 /// This version assumes From/To have matching types and numbers of result
1706 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
1707 std::vector<SDNode*> *Deleted) {
1708 assert(From != To && "Cannot replace uses of with self");
1709 assert(From->getNumValues() == To->getNumValues() &&
1710 "Cannot use this version of ReplaceAllUsesWith!");
1711 if (From->getNumValues() == 1) { // If possible, use the faster version.
1712 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
1716 while (!From->use_empty()) {
1717 // Process users until they are all gone.
1718 SDNode *U = *From->use_begin();
1720 // This node is about to morph, remove its old self from the CSE maps.
1721 RemoveNodeFromCSEMaps(U);
1723 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1725 if (I->Val == From) {
1726 From->removeUser(U);
1731 // Now that we have modified U, add it back to the CSE maps. If it already
1732 // exists there, recursively merge the results together.
1733 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1734 ReplaceAllUsesWith(U, Existing, Deleted);
1736 if (Deleted) Deleted->push_back(U);
1737 DeleteNodeNotInCSEMaps(U);
1742 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1743 /// This can cause recursive merging of nodes in the DAG.
1745 /// This version can replace From with any result values. To must match the
1746 /// number and types of values returned by From.
1747 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
1748 const std::vector<SDOperand> &To,
1749 std::vector<SDNode*> *Deleted) {
1750 assert(From->getNumValues() == To.size() &&
1751 "Incorrect number of values to replace with!");
1752 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
1753 // Degenerate case handled above.
1754 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
1758 while (!From->use_empty()) {
1759 // Process users until they are all gone.
1760 SDNode *U = *From->use_begin();
1762 // This node is about to morph, remove its old self from the CSE maps.
1763 RemoveNodeFromCSEMaps(U);
1765 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1767 if (I->Val == From) {
1768 const SDOperand &ToOp = To[I->ResNo];
1769 From->removeUser(U);
1771 ToOp.Val->addUser(U);
1774 // Now that we have modified U, add it back to the CSE maps. If it already
1775 // exists there, recursively merge the results together.
1776 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1777 ReplaceAllUsesWith(U, Existing, Deleted);
1779 if (Deleted) Deleted->push_back(U);
1780 DeleteNodeNotInCSEMaps(U);
1786 //===----------------------------------------------------------------------===//
1788 //===----------------------------------------------------------------------===//
1791 /// getValueTypeList - Return a pointer to the specified value type.
1793 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
1794 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
1799 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1800 /// indicated value. This method ignores uses of other values defined by this
1802 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1803 assert(Value < getNumValues() && "Bad value!");
1805 // If there is only one value, this is easy.
1806 if (getNumValues() == 1)
1807 return use_size() == NUses;
1808 if (Uses.size() < NUses) return false;
1810 SDOperand TheValue(this, Value);
1812 std::set<SDNode*> UsersHandled;
1814 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1817 if (User->getNumOperands() == 1 ||
1818 UsersHandled.insert(User).second) // First time we've seen this?
1819 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1820 if (User->getOperand(i) == TheValue) {
1822 return false; // too many uses
1827 // Found exactly the right number of uses?
1832 const char *SDNode::getOperationName(const SelectionDAG *G) const {
1833 switch (getOpcode()) {
1835 if (getOpcode() < ISD::BUILTIN_OP_END)
1836 return "<<Unknown DAG Node>>";
1839 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
1840 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
1841 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
1843 TargetLowering &TLI = G->getTargetLoweringInfo();
1845 TLI.getTargetNodeName(getOpcode());
1846 if (Name) return Name;
1849 return "<<Unknown Target Node>>";
1852 case ISD::PCMARKER: return "PCMarker";
1853 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
1854 case ISD::SRCVALUE: return "SrcValue";
1855 case ISD::VALUETYPE: return "ValueType";
1856 case ISD::STRING: return "String";
1857 case ISD::EntryToken: return "EntryToken";
1858 case ISD::TokenFactor: return "TokenFactor";
1859 case ISD::AssertSext: return "AssertSext";
1860 case ISD::AssertZext: return "AssertZext";
1861 case ISD::Constant: return "Constant";
1862 case ISD::TargetConstant: return "TargetConstant";
1863 case ISD::ConstantFP: return "ConstantFP";
1864 case ISD::ConstantVec: return "ConstantVec";
1865 case ISD::GlobalAddress: return "GlobalAddress";
1866 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
1867 case ISD::FrameIndex: return "FrameIndex";
1868 case ISD::TargetFrameIndex: return "TargetFrameIndex";
1869 case ISD::BasicBlock: return "BasicBlock";
1870 case ISD::Register: return "Register";
1871 case ISD::ExternalSymbol: return "ExternalSymbol";
1872 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
1873 case ISD::ConstantPool: return "ConstantPool";
1874 case ISD::TargetConstantPool: return "TargetConstantPool";
1875 case ISD::CopyToReg: return "CopyToReg";
1876 case ISD::CopyFromReg: return "CopyFromReg";
1877 case ISD::ImplicitDef: return "ImplicitDef";
1878 case ISD::UNDEF: return "undef";
1881 case ISD::FABS: return "fabs";
1882 case ISD::FNEG: return "fneg";
1883 case ISD::FSQRT: return "fsqrt";
1884 case ISD::FSIN: return "fsin";
1885 case ISD::FCOS: return "fcos";
1888 case ISD::ADD: return "add";
1889 case ISD::SUB: return "sub";
1890 case ISD::MUL: return "mul";
1891 case ISD::MULHU: return "mulhu";
1892 case ISD::MULHS: return "mulhs";
1893 case ISD::SDIV: return "sdiv";
1894 case ISD::UDIV: return "udiv";
1895 case ISD::SREM: return "srem";
1896 case ISD::UREM: return "urem";
1897 case ISD::AND: return "and";
1898 case ISD::OR: return "or";
1899 case ISD::XOR: return "xor";
1900 case ISD::SHL: return "shl";
1901 case ISD::SRA: return "sra";
1902 case ISD::SRL: return "srl";
1903 case ISD::FADD: return "fadd";
1904 case ISD::FSUB: return "fsub";
1905 case ISD::FMUL: return "fmul";
1906 case ISD::FDIV: return "fdiv";
1907 case ISD::FREM: return "frem";
1908 case ISD::VADD: return "vadd";
1909 case ISD::VSUB: return "vsub";
1910 case ISD::VMUL: return "vmul";
1912 case ISD::SETCC: return "setcc";
1913 case ISD::SELECT: return "select";
1914 case ISD::SELECT_CC: return "select_cc";
1915 case ISD::ADD_PARTS: return "add_parts";
1916 case ISD::SUB_PARTS: return "sub_parts";
1917 case ISD::SHL_PARTS: return "shl_parts";
1918 case ISD::SRA_PARTS: return "sra_parts";
1919 case ISD::SRL_PARTS: return "srl_parts";
1921 // Conversion operators.
1922 case ISD::SIGN_EXTEND: return "sign_extend";
1923 case ISD::ZERO_EXTEND: return "zero_extend";
1924 case ISD::ANY_EXTEND: return "any_extend";
1925 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
1926 case ISD::TRUNCATE: return "truncate";
1927 case ISD::FP_ROUND: return "fp_round";
1928 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
1929 case ISD::FP_EXTEND: return "fp_extend";
1931 case ISD::SINT_TO_FP: return "sint_to_fp";
1932 case ISD::UINT_TO_FP: return "uint_to_fp";
1933 case ISD::FP_TO_SINT: return "fp_to_sint";
1934 case ISD::FP_TO_UINT: return "fp_to_uint";
1936 // Control flow instructions
1937 case ISD::BR: return "br";
1938 case ISD::BRCOND: return "brcond";
1939 case ISD::BRCONDTWOWAY: return "brcondtwoway";
1940 case ISD::BR_CC: return "br_cc";
1941 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
1942 case ISD::RET: return "ret";
1943 case ISD::CALL: return "call";
1944 case ISD::TAILCALL:return "tailcall";
1945 case ISD::CALLSEQ_START: return "callseq_start";
1946 case ISD::CALLSEQ_END: return "callseq_end";
1949 case ISD::LOAD: return "load";
1950 case ISD::STORE: return "store";
1951 case ISD::VLOAD: return "vload";
1952 case ISD::EXTLOAD: return "extload";
1953 case ISD::SEXTLOAD: return "sextload";
1954 case ISD::ZEXTLOAD: return "zextload";
1955 case ISD::TRUNCSTORE: return "truncstore";
1957 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1958 case ISD::EXTRACT_ELEMENT: return "extract_element";
1959 case ISD::BUILD_PAIR: return "build_pair";
1960 case ISD::MEMSET: return "memset";
1961 case ISD::MEMCPY: return "memcpy";
1962 case ISD::MEMMOVE: return "memmove";
1965 case ISD::CTPOP: return "ctpop";
1966 case ISD::CTTZ: return "cttz";
1967 case ISD::CTLZ: return "ctlz";
1970 case ISD::READPORT: return "readport";
1971 case ISD::WRITEPORT: return "writeport";
1972 case ISD::READIO: return "readio";
1973 case ISD::WRITEIO: return "writeio";
1976 case ISD::LOCATION: return "location";
1977 case ISD::DEBUG_LOC: return "debug_loc";
1980 switch (cast<CondCodeSDNode>(this)->get()) {
1981 default: assert(0 && "Unknown setcc condition!");
1982 case ISD::SETOEQ: return "setoeq";
1983 case ISD::SETOGT: return "setogt";
1984 case ISD::SETOGE: return "setoge";
1985 case ISD::SETOLT: return "setolt";
1986 case ISD::SETOLE: return "setole";
1987 case ISD::SETONE: return "setone";
1989 case ISD::SETO: return "seto";
1990 case ISD::SETUO: return "setuo";
1991 case ISD::SETUEQ: return "setue";
1992 case ISD::SETUGT: return "setugt";
1993 case ISD::SETUGE: return "setuge";
1994 case ISD::SETULT: return "setult";
1995 case ISD::SETULE: return "setule";
1996 case ISD::SETUNE: return "setune";
1998 case ISD::SETEQ: return "seteq";
1999 case ISD::SETGT: return "setgt";
2000 case ISD::SETGE: return "setge";
2001 case ISD::SETLT: return "setlt";
2002 case ISD::SETLE: return "setle";
2003 case ISD::SETNE: return "setne";
2008 void SDNode::dump() const { dump(0); }
2009 void SDNode::dump(const SelectionDAG *G) const {
2010 std::cerr << (void*)this << ": ";
2012 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2013 if (i) std::cerr << ",";
2014 if (getValueType(i) == MVT::Other)
2017 std::cerr << MVT::getValueTypeString(getValueType(i));
2019 std::cerr << " = " << getOperationName(G);
2022 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2023 if (i) std::cerr << ", ";
2024 std::cerr << (void*)getOperand(i).Val;
2025 if (unsigned RN = getOperand(i).ResNo)
2026 std::cerr << ":" << RN;
2029 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2030 std::cerr << "<" << CSDN->getValue() << ">";
2031 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2032 std::cerr << "<" << CSDN->getValue() << ">";
2033 } else if (const GlobalAddressSDNode *GADN =
2034 dyn_cast<GlobalAddressSDNode>(this)) {
2035 int offset = GADN->getOffset();
2037 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
2039 std::cerr << " + " << offset;
2041 std::cerr << " " << offset;
2042 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
2043 std::cerr << "<" << FIDN->getIndex() << ">";
2044 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
2045 std::cerr << "<" << *CP->get() << ">";
2046 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
2048 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2050 std::cerr << LBB->getName() << " ";
2051 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
2052 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
2053 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2054 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2056 std::cerr << " #" << R->getReg();
2058 } else if (const ExternalSymbolSDNode *ES =
2059 dyn_cast<ExternalSymbolSDNode>(this)) {
2060 std::cerr << "'" << ES->getSymbol() << "'";
2061 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2063 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2065 std::cerr << "<null:" << M->getOffset() << ">";
2066 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2067 std::cerr << ":" << getValueTypeString(N->getVT());
2071 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
2072 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2073 if (N->getOperand(i).Val->hasOneUse())
2074 DumpNodes(N->getOperand(i).Val, indent+2, G);
2076 std::cerr << "\n" << std::string(indent+2, ' ')
2077 << (void*)N->getOperand(i).Val << ": <multiple use>";
2080 std::cerr << "\n" << std::string(indent, ' ');
2084 void SelectionDAG::dump() const {
2085 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
2086 std::vector<const SDNode*> Nodes;
2087 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2091 std::sort(Nodes.begin(), Nodes.end());
2093 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2094 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
2095 DumpNodes(Nodes[i], 2, this);
2098 DumpNodes(getRoot().Val, 2, this);
2100 std::cerr << "\n\n";