1 //===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements the SelectionDAG class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/SelectionDAG.h"
15 #include "llvm/Constants.h"
16 #include "llvm/GlobalValue.h"
17 #include "llvm/Intrinsics.h"
18 #include "llvm/Assembly/Writer.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/Support/MathExtras.h"
21 #include "llvm/Target/MRegisterInfo.h"
22 #include "llvm/Target/TargetLowering.h"
23 #include "llvm/Target/TargetInstrInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/ADT/SetVector.h"
26 #include "llvm/ADT/StringExtras.h"
33 static bool isCommutativeBinOp(unsigned Opcode) {
43 case ISD::XOR: return true;
44 default: return false; // FIXME: Need commutative info for user ops!
48 // isInvertibleForFree - Return true if there is no cost to emitting the logical
49 // inverse of this node.
50 static bool isInvertibleForFree(SDOperand N) {
51 if (isa<ConstantSDNode>(N.Val)) return true;
52 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
57 //===----------------------------------------------------------------------===//
58 // ConstantFPSDNode Class
59 //===----------------------------------------------------------------------===//
61 /// isExactlyValue - We don't rely on operator== working on double values, as
62 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
63 /// As such, this method can be used to do an exact bit-for-bit comparison of
64 /// two floating point values.
65 bool ConstantFPSDNode::isExactlyValue(double V) const {
66 return DoubleToBits(V) == DoubleToBits(Value);
69 //===----------------------------------------------------------------------===//
71 //===----------------------------------------------------------------------===//
73 /// isBuildVectorAllOnes - Return true if the specified node is a
74 /// BUILD_VECTOR where all of the elements are ~0 or undef.
75 bool ISD::isBuildVectorAllOnes(const SDNode *N) {
76 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
78 unsigned i = 0, e = N->getNumOperands();
80 // Skip over all of the undef values.
81 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
84 // Do not accept an all-undef vector.
85 if (i == e) return false;
87 // Do not accept build_vectors that aren't all constants or which have non-~0
89 SDOperand NotZero = N->getOperand(i);
90 if (isa<ConstantSDNode>(NotZero)) {
91 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
93 } else if (isa<ConstantFPSDNode>(NotZero)) {
94 MVT::ValueType VT = NotZero.getValueType();
96 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
100 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
107 // Okay, we have at least one ~0 value, check to see if the rest match or are
109 for (++i; i != e; ++i)
110 if (N->getOperand(i) != NotZero &&
111 N->getOperand(i).getOpcode() != ISD::UNDEF)
117 /// isBuildVectorAllZeros - Return true if the specified node is a
118 /// BUILD_VECTOR where all of the elements are 0 or undef.
119 bool ISD::isBuildVectorAllZeros(const SDNode *N) {
120 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
122 unsigned i = 0, e = N->getNumOperands();
124 // Skip over all of the undef values.
125 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
128 // Do not accept an all-undef vector.
129 if (i == e) return false;
131 // Do not accept build_vectors that aren't all constants or which have non-~0
133 SDOperand Zero = N->getOperand(i);
134 if (isa<ConstantSDNode>(Zero)) {
135 if (!cast<ConstantSDNode>(Zero)->isNullValue())
137 } else if (isa<ConstantFPSDNode>(Zero)) {
138 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
143 // Okay, we have at least one ~0 value, check to see if the rest match or are
145 for (++i; i != e; ++i)
146 if (N->getOperand(i) != Zero &&
147 N->getOperand(i).getOpcode() != ISD::UNDEF)
152 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
153 /// when given the operation for (X op Y).
154 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
155 // To perform this operation, we just need to swap the L and G bits of the
157 unsigned OldL = (Operation >> 2) & 1;
158 unsigned OldG = (Operation >> 1) & 1;
159 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
160 (OldL << 1) | // New G bit
161 (OldG << 2)); // New L bit.
164 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
165 /// 'op' is a valid SetCC operation.
166 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
167 unsigned Operation = Op;
169 Operation ^= 7; // Flip L, G, E bits, but not U.
171 Operation ^= 15; // Flip all of the condition bits.
172 if (Operation > ISD::SETTRUE2)
173 Operation &= ~8; // Don't let N and U bits get set.
174 return ISD::CondCode(Operation);
178 /// isSignedOp - For an integer comparison, return 1 if the comparison is a
179 /// signed operation and 2 if the result is an unsigned comparison. Return zero
180 /// if the operation does not depend on the sign of the input (setne and seteq).
181 static int isSignedOp(ISD::CondCode Opcode) {
183 default: assert(0 && "Illegal integer setcc operation!");
185 case ISD::SETNE: return 0;
189 case ISD::SETGE: return 1;
193 case ISD::SETUGE: return 2;
197 /// getSetCCOrOperation - Return the result of a logical OR between different
198 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
199 /// returns SETCC_INVALID if it is not possible to represent the resultant
201 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
203 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
204 // Cannot fold a signed integer setcc with an unsigned integer setcc.
205 return ISD::SETCC_INVALID;
207 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
209 // If the N and U bits get set then the resultant comparison DOES suddenly
210 // care about orderedness, and is true when ordered.
211 if (Op > ISD::SETTRUE2)
212 Op &= ~16; // Clear the N bit.
213 return ISD::CondCode(Op);
216 /// getSetCCAndOperation - Return the result of a logical AND between different
217 /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
218 /// function returns zero if it is not possible to represent the resultant
220 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
222 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
223 // Cannot fold a signed setcc with an unsigned setcc.
224 return ISD::SETCC_INVALID;
226 // Combine all of the condition bits.
227 return ISD::CondCode(Op1 & Op2);
230 const TargetMachine &SelectionDAG::getTarget() const {
231 return TLI.getTargetMachine();
234 //===----------------------------------------------------------------------===//
235 // SelectionDAG Class
236 //===----------------------------------------------------------------------===//
238 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
239 /// SelectionDAG, including nodes (like loads) that have uses of their token
240 /// chain but no other uses and no side effect. If a node is passed in as an
241 /// argument, it is used as the seed for node deletion.
242 void SelectionDAG::RemoveDeadNodes(SDNode *N) {
243 // Create a dummy node (which is not added to allnodes), that adds a reference
244 // to the root node, preventing it from being deleted.
245 HandleSDNode Dummy(getRoot());
247 bool MadeChange = false;
249 // If we have a hint to start from, use it.
250 if (N && N->use_empty()) {
255 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
256 if (I->use_empty() && I->getOpcode() != 65535) {
257 // Node is dead, recursively delete newly dead uses.
262 // Walk the nodes list, removing the nodes we've marked as dead.
264 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
271 // If the root changed (e.g. it was a dead load, update the root).
272 setRoot(Dummy.getValue());
275 /// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
276 /// graph. If it is the last user of any of its operands, recursively process
277 /// them the same way.
279 void SelectionDAG::DestroyDeadNode(SDNode *N) {
280 // Okay, we really are going to delete this node. First take this out of the
281 // appropriate CSE map.
282 RemoveNodeFromCSEMaps(N);
284 // Next, brutally remove the operand list. This is safe to do, as there are
285 // no cycles in the graph.
286 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
290 // Now that we removed this operand, see if there are no uses of it left.
294 delete[] N->OperandList;
298 // Mark the node as dead.
299 N->MorphNodeTo(65535);
302 void SelectionDAG::DeleteNode(SDNode *N) {
303 assert(N->use_empty() && "Cannot delete a node that is not dead!");
305 // First take this out of the appropriate CSE map.
306 RemoveNodeFromCSEMaps(N);
308 // Finally, remove uses due to operands of this node, remove from the
309 // AllNodes list, and delete the node.
310 DeleteNodeNotInCSEMaps(N);
313 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
315 // Remove it from the AllNodes list.
318 // Drop all of the operands and decrement used nodes use counts.
319 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
320 I->Val->removeUser(N);
321 delete[] N->OperandList;
328 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
329 /// correspond to it. This is useful when we're about to delete or repurpose
330 /// the node. We don't want future request for structurally identical nodes
331 /// to return N anymore.
332 void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
334 switch (N->getOpcode()) {
335 case ISD::HANDLENODE: return; // noop.
337 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
338 N->getValueType(0)));
340 case ISD::TargetConstant:
341 Erased = TargetConstants.erase(std::make_pair(
342 cast<ConstantSDNode>(N)->getValue(),
343 N->getValueType(0)));
345 case ISD::ConstantFP: {
346 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
347 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
350 case ISD::TargetConstantFP: {
351 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
352 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
356 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
359 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
360 "Cond code doesn't exist!");
361 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
362 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
364 case ISD::GlobalAddress: {
365 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
366 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
370 case ISD::TargetGlobalAddress: {
371 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
372 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
376 case ISD::FrameIndex:
377 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
379 case ISD::TargetFrameIndex:
380 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
382 case ISD::ConstantPool:
383 Erased = ConstantPoolIndices.
384 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
385 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
386 cast<ConstantPoolSDNode>(N)->getAlignment())));
388 case ISD::TargetConstantPool:
389 Erased = TargetConstantPoolIndices.
390 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
391 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
392 cast<ConstantPoolSDNode>(N)->getAlignment())));
394 case ISD::BasicBlock:
395 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
397 case ISD::ExternalSymbol:
398 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
400 case ISD::TargetExternalSymbol:
402 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
405 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
406 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
409 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
410 N->getValueType(0)));
412 case ISD::SRCVALUE: {
413 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
414 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
418 Erased = Loads.erase(std::make_pair(N->getOperand(1),
419 std::make_pair(N->getOperand(0),
420 N->getValueType(0))));
423 if (N->getNumValues() == 1) {
424 if (N->getNumOperands() == 0) {
425 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
426 N->getValueType(0)));
427 } else if (N->getNumOperands() == 1) {
429 UnaryOps.erase(std::make_pair(N->getOpcode(),
430 std::make_pair(N->getOperand(0),
431 N->getValueType(0))));
432 } else if (N->getNumOperands() == 2) {
434 BinaryOps.erase(std::make_pair(N->getOpcode(),
435 std::make_pair(N->getOperand(0),
438 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
440 OneResultNodes.erase(std::make_pair(N->getOpcode(),
441 std::make_pair(N->getValueType(0),
445 // Remove the node from the ArbitraryNodes map.
446 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
447 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
449 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
450 std::make_pair(RV, Ops)));
455 // Verify that the node was actually in one of the CSE maps, unless it has a
456 // flag result (which cannot be CSE'd) or is one of the special cases that are
457 // not subject to CSE.
458 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
459 !N->isTargetOpcode()) {
461 assert(0 && "Node is not in map!");
466 /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
467 /// has been taken out and modified in some way. If the specified node already
468 /// exists in the CSE maps, do not modify the maps, but return the existing node
469 /// instead. If it doesn't exist, add it and return null.
471 SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
472 assert(N->getNumOperands() && "This is a leaf node!");
473 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
474 return 0; // Never add these nodes.
476 // Check that remaining values produced are not flags.
477 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
478 if (N->getValueType(i) == MVT::Flag)
479 return 0; // Never CSE anything that produces a flag.
481 if (N->getNumValues() == 1) {
482 if (N->getNumOperands() == 1) {
483 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
484 std::make_pair(N->getOperand(0),
485 N->getValueType(0)))];
488 } else if (N->getNumOperands() == 2) {
489 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
490 std::make_pair(N->getOperand(0),
495 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
496 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
497 std::make_pair(N->getValueType(0), Ops))];
502 if (N->getOpcode() == ISD::LOAD) {
503 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
504 std::make_pair(N->getOperand(0),
505 N->getValueType(0)))];
509 // Remove the node from the ArbitraryNodes map.
510 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
511 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
512 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
513 std::make_pair(RV, Ops))];
521 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
522 /// were replaced with those specified. If this node is never memoized,
523 /// return null, otherwise return a pointer to the slot it would take. If a
524 /// node already exists with these operands, the slot will be non-null.
525 SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
526 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
527 return 0; // Never add these nodes.
529 // Check that remaining values produced are not flags.
530 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
531 if (N->getValueType(i) == MVT::Flag)
532 return 0; // Never CSE anything that produces a flag.
534 if (N->getNumValues() == 1) {
535 return &UnaryOps[std::make_pair(N->getOpcode(),
536 std::make_pair(Op, N->getValueType(0)))];
538 // Remove the node from the ArbitraryNodes map.
539 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
540 std::vector<SDOperand> Ops;
542 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
543 std::make_pair(RV, Ops))];
548 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
549 /// were replaced with those specified. If this node is never memoized,
550 /// return null, otherwise return a pointer to the slot it would take. If a
551 /// node already exists with these operands, the slot will be non-null.
552 SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
553 SDOperand Op1, SDOperand Op2) {
554 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
555 return 0; // Never add these nodes.
557 // Check that remaining values produced are not flags.
558 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
559 if (N->getValueType(i) == MVT::Flag)
560 return 0; // Never CSE anything that produces a flag.
562 if (N->getNumValues() == 1) {
563 return &BinaryOps[std::make_pair(N->getOpcode(),
564 std::make_pair(Op1, Op2))];
566 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
567 std::vector<SDOperand> Ops;
570 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
571 std::make_pair(RV, Ops))];
577 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
578 /// were replaced with those specified. If this node is never memoized,
579 /// return null, otherwise return a pointer to the slot it would take. If a
580 /// node already exists with these operands, the slot will be non-null.
581 SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
582 const std::vector<SDOperand> &Ops) {
583 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
584 return 0; // Never add these nodes.
586 // Check that remaining values produced are not flags.
587 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
588 if (N->getValueType(i) == MVT::Flag)
589 return 0; // Never CSE anything that produces a flag.
591 if (N->getNumValues() == 1) {
592 if (N->getNumOperands() == 1) {
593 return &UnaryOps[std::make_pair(N->getOpcode(),
594 std::make_pair(Ops[0],
595 N->getValueType(0)))];
596 } else if (N->getNumOperands() == 2) {
597 return &BinaryOps[std::make_pair(N->getOpcode(),
598 std::make_pair(Ops[0], Ops[1]))];
600 return &OneResultNodes[std::make_pair(N->getOpcode(),
601 std::make_pair(N->getValueType(0),
605 if (N->getOpcode() == ISD::LOAD) {
606 return &Loads[std::make_pair(Ops[1],
607 std::make_pair(Ops[0], N->getValueType(0)))];
609 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
610 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
611 std::make_pair(RV, Ops))];
618 SelectionDAG::~SelectionDAG() {
619 while (!AllNodes.empty()) {
620 SDNode *N = AllNodes.begin();
621 delete [] N->OperandList;
624 AllNodes.pop_front();
628 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
629 if (Op.getValueType() == VT) return Op;
630 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
631 return getNode(ISD::AND, Op.getValueType(), Op,
632 getConstant(Imm, Op.getValueType()));
635 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
636 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
637 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
639 // Mask out any bits that are not valid for this constant.
641 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
643 SDNode *&N = Constants[std::make_pair(Val, VT)];
644 if (N) return SDOperand(N, 0);
645 N = new ConstantSDNode(false, Val, VT);
646 AllNodes.push_back(N);
647 return SDOperand(N, 0);
650 SDOperand SelectionDAG::getString(const std::string &Val) {
651 StringSDNode *&N = StringNodes[Val];
653 N = new StringSDNode(Val);
654 AllNodes.push_back(N);
656 return SDOperand(N, 0);
659 SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
660 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
661 // Mask out any bits that are not valid for this constant.
663 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
665 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
666 if (N) return SDOperand(N, 0);
667 N = new ConstantSDNode(true, Val, VT);
668 AllNodes.push_back(N);
669 return SDOperand(N, 0);
672 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
673 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
675 Val = (float)Val; // Mask out extra precision.
677 // Do the map lookup using the actual bit pattern for the floating point
678 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
679 // we don't have issues with SNANs.
680 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
681 if (N) return SDOperand(N, 0);
682 N = new ConstantFPSDNode(false, Val, VT);
683 AllNodes.push_back(N);
684 return SDOperand(N, 0);
687 SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
688 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
690 Val = (float)Val; // Mask out extra precision.
692 // Do the map lookup using the actual bit pattern for the floating point
693 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
694 // we don't have issues with SNANs.
695 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
696 if (N) return SDOperand(N, 0);
697 N = new ConstantFPSDNode(true, Val, VT);
698 AllNodes.push_back(N);
699 return SDOperand(N, 0);
702 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
703 MVT::ValueType VT, int offset) {
704 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
705 if (N) return SDOperand(N, 0);
706 N = new GlobalAddressSDNode(false, GV, VT, offset);
707 AllNodes.push_back(N);
708 return SDOperand(N, 0);
711 SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
712 MVT::ValueType VT, int offset) {
713 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
714 if (N) return SDOperand(N, 0);
715 N = new GlobalAddressSDNode(true, GV, VT, offset);
716 AllNodes.push_back(N);
717 return SDOperand(N, 0);
720 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
721 SDNode *&N = FrameIndices[FI];
722 if (N) return SDOperand(N, 0);
723 N = new FrameIndexSDNode(FI, VT, false);
724 AllNodes.push_back(N);
725 return SDOperand(N, 0);
728 SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
729 SDNode *&N = TargetFrameIndices[FI];
730 if (N) return SDOperand(N, 0);
731 N = new FrameIndexSDNode(FI, VT, true);
732 AllNodes.push_back(N);
733 return SDOperand(N, 0);
736 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
737 unsigned Alignment, int Offset) {
738 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
739 std::make_pair(Offset, Alignment))];
740 if (N) return SDOperand(N, 0);
741 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
742 AllNodes.push_back(N);
743 return SDOperand(N, 0);
746 SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
747 unsigned Alignment, int Offset) {
748 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
749 std::make_pair(Offset, Alignment))];
750 if (N) return SDOperand(N, 0);
751 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
752 AllNodes.push_back(N);
753 return SDOperand(N, 0);
756 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
757 SDNode *&N = BBNodes[MBB];
758 if (N) return SDOperand(N, 0);
759 N = new BasicBlockSDNode(MBB);
760 AllNodes.push_back(N);
761 return SDOperand(N, 0);
764 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
765 if ((unsigned)VT >= ValueTypeNodes.size())
766 ValueTypeNodes.resize(VT+1);
767 if (ValueTypeNodes[VT] == 0) {
768 ValueTypeNodes[VT] = new VTSDNode(VT);
769 AllNodes.push_back(ValueTypeNodes[VT]);
772 return SDOperand(ValueTypeNodes[VT], 0);
775 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
776 SDNode *&N = ExternalSymbols[Sym];
777 if (N) return SDOperand(N, 0);
778 N = new ExternalSymbolSDNode(false, Sym, VT);
779 AllNodes.push_back(N);
780 return SDOperand(N, 0);
783 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
785 SDNode *&N = TargetExternalSymbols[Sym];
786 if (N) return SDOperand(N, 0);
787 N = new ExternalSymbolSDNode(true, Sym, VT);
788 AllNodes.push_back(N);
789 return SDOperand(N, 0);
792 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
793 if ((unsigned)Cond >= CondCodeNodes.size())
794 CondCodeNodes.resize(Cond+1);
796 if (CondCodeNodes[Cond] == 0) {
797 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
798 AllNodes.push_back(CondCodeNodes[Cond]);
800 return SDOperand(CondCodeNodes[Cond], 0);
803 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
804 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
806 Reg = new RegisterSDNode(RegNo, VT);
807 AllNodes.push_back(Reg);
809 return SDOperand(Reg, 0);
812 SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
813 SDOperand N2, ISD::CondCode Cond) {
814 // These setcc operations always fold.
818 case ISD::SETFALSE2: return getConstant(0, VT);
820 case ISD::SETTRUE2: return getConstant(1, VT);
823 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
824 uint64_t C2 = N2C->getValue();
825 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
826 uint64_t C1 = N1C->getValue();
828 // Sign extend the operands if required
829 if (ISD::isSignedIntSetCC(Cond)) {
830 C1 = N1C->getSignExtended();
831 C2 = N2C->getSignExtended();
835 default: assert(0 && "Unknown integer setcc!");
836 case ISD::SETEQ: return getConstant(C1 == C2, VT);
837 case ISD::SETNE: return getConstant(C1 != C2, VT);
838 case ISD::SETULT: return getConstant(C1 < C2, VT);
839 case ISD::SETUGT: return getConstant(C1 > C2, VT);
840 case ISD::SETULE: return getConstant(C1 <= C2, VT);
841 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
842 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
843 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
844 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
845 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
848 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
849 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
850 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
852 // If the comparison constant has bits in the upper part, the
853 // zero-extended value could never match.
854 if (C2 & (~0ULL << InSize)) {
855 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
859 case ISD::SETEQ: return getConstant(0, VT);
862 case ISD::SETNE: return getConstant(1, VT);
865 // True if the sign bit of C2 is set.
866 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
869 // True if the sign bit of C2 isn't set.
870 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
876 // Otherwise, we can perform the comparison with the low bits.
884 return getSetCC(VT, N1.getOperand(0),
885 getConstant(C2, N1.getOperand(0).getValueType()),
888 break; // todo, be more careful with signed comparisons
890 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
891 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
892 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
893 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
894 MVT::ValueType ExtDstTy = N1.getValueType();
895 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
897 // If the extended part has any inconsistent bits, it cannot ever
898 // compare equal. In other words, they have to be all ones or all
901 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
902 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
903 return getConstant(Cond == ISD::SETNE, VT);
905 // Otherwise, make this a use of a zext.
906 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
907 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
911 uint64_t MinVal, MaxVal;
912 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
913 if (ISD::isSignedIntSetCC(Cond)) {
914 MinVal = 1ULL << (OperandBitSize-1);
915 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
916 MaxVal = ~0ULL >> (65-OperandBitSize);
921 MaxVal = ~0ULL >> (64-OperandBitSize);
924 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
925 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
926 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
927 --C2; // X >= C1 --> X > (C1-1)
928 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
929 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
932 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
933 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
934 ++C2; // X <= C1 --> X < (C1+1)
935 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
936 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
939 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
940 return getConstant(0, VT); // X < MIN --> false
942 // Canonicalize setgt X, Min --> setne X, Min
943 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
944 return getSetCC(VT, N1, N2, ISD::SETNE);
946 // If we have setult X, 1, turn it into seteq X, 0
947 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
948 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
950 // If we have setugt X, Max-1, turn it into seteq X, Max
951 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
952 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
955 // If we have "setcc X, C1", check to see if we can shrink the immediate
958 // SETUGT X, SINTMAX -> SETLT X, 0
959 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
960 C2 == (~0ULL >> (65-OperandBitSize)))
961 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
963 // FIXME: Implement the rest of these.
966 // Fold bit comparisons when we can.
967 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
968 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
969 if (ConstantSDNode *AndRHS =
970 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
971 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
972 // Perform the xform if the AND RHS is a single bit.
973 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
974 return getNode(ISD::SRL, VT, N1,
975 getConstant(Log2_64(AndRHS->getValue()),
976 TLI.getShiftAmountTy()));
978 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
979 // (X & 8) == 8 --> (X & 8) >> 3
980 // Perform the xform if C2 is a single bit.
981 if ((C2 & (C2-1)) == 0) {
982 return getNode(ISD::SRL, VT, N1,
983 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
988 } else if (isa<ConstantSDNode>(N1.Val)) {
989 // Ensure that the constant occurs on the RHS.
990 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
993 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
994 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
995 double C1 = N1C->getValue(), C2 = N2C->getValue();
998 default: break; // FIXME: Implement the rest of these!
999 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1000 case ISD::SETNE: return getConstant(C1 != C2, VT);
1001 case ISD::SETLT: return getConstant(C1 < C2, VT);
1002 case ISD::SETGT: return getConstant(C1 > C2, VT);
1003 case ISD::SETLE: return getConstant(C1 <= C2, VT);
1004 case ISD::SETGE: return getConstant(C1 >= C2, VT);
1007 // Ensure that the constant occurs on the RHS.
1008 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
1011 // Could not fold it.
1015 /// getNode - Gets or creates the specified node.
1017 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
1018 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
1020 N = new SDNode(Opcode, VT);
1021 AllNodes.push_back(N);
1023 return SDOperand(N, 0);
1026 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1027 SDOperand Operand) {
1029 // Constant fold unary operations with an integer constant operand.
1030 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1031 uint64_t Val = C->getValue();
1034 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
1035 case ISD::ANY_EXTEND:
1036 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1037 case ISD::TRUNCATE: return getConstant(Val, VT);
1038 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1039 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
1040 case ISD::BIT_CONVERT:
1041 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
1042 return getConstantFP(BitsToFloat(Val), VT);
1043 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
1044 return getConstantFP(BitsToDouble(Val), VT);
1048 default: assert(0 && "Invalid bswap!"); break;
1049 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1050 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1051 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1056 default: assert(0 && "Invalid ctpop!"); break;
1057 case MVT::i1: return getConstant(Val != 0, VT);
1059 Tmp1 = (unsigned)Val & 0xFF;
1060 return getConstant(CountPopulation_32(Tmp1), VT);
1062 Tmp1 = (unsigned)Val & 0xFFFF;
1063 return getConstant(CountPopulation_32(Tmp1), VT);
1065 return getConstant(CountPopulation_32((unsigned)Val), VT);
1067 return getConstant(CountPopulation_64(Val), VT);
1071 default: assert(0 && "Invalid ctlz!"); break;
1072 case MVT::i1: return getConstant(Val == 0, VT);
1074 Tmp1 = (unsigned)Val & 0xFF;
1075 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1077 Tmp1 = (unsigned)Val & 0xFFFF;
1078 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1080 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1082 return getConstant(CountLeadingZeros_64(Val), VT);
1086 default: assert(0 && "Invalid cttz!"); break;
1087 case MVT::i1: return getConstant(Val == 0, VT);
1089 Tmp1 = (unsigned)Val | 0x100;
1090 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1092 Tmp1 = (unsigned)Val | 0x10000;
1093 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1095 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1097 return getConstant(CountTrailingZeros_64(Val), VT);
1102 // Constant fold unary operations with an floating point constant operand.
1103 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1106 return getConstantFP(-C->getValue(), VT);
1108 return getConstantFP(fabs(C->getValue()), VT);
1110 case ISD::FP_EXTEND:
1111 return getConstantFP(C->getValue(), VT);
1112 case ISD::FP_TO_SINT:
1113 return getConstant((int64_t)C->getValue(), VT);
1114 case ISD::FP_TO_UINT:
1115 return getConstant((uint64_t)C->getValue(), VT);
1116 case ISD::BIT_CONVERT:
1117 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1118 return getConstant(FloatToBits(C->getValue()), VT);
1119 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1120 return getConstant(DoubleToBits(C->getValue()), VT);
1124 unsigned OpOpcode = Operand.Val->getOpcode();
1126 case ISD::TokenFactor:
1127 return Operand; // Factor of one node? No factor.
1128 case ISD::SIGN_EXTEND:
1129 if (Operand.getValueType() == VT) return Operand; // noop extension
1130 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
1131 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1132 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1134 case ISD::ZERO_EXTEND:
1135 if (Operand.getValueType() == VT) return Operand; // noop extension
1136 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
1137 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1138 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1140 case ISD::ANY_EXTEND:
1141 if (Operand.getValueType() == VT) return Operand; // noop extension
1142 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
1143 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1144 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1145 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1148 if (Operand.getValueType() == VT) return Operand; // noop truncate
1149 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
1150 if (OpOpcode == ISD::TRUNCATE)
1151 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1152 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1153 OpOpcode == ISD::ANY_EXTEND) {
1154 // If the source is smaller than the dest, we still need an extend.
1155 if (Operand.Val->getOperand(0).getValueType() < VT)
1156 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1157 else if (Operand.Val->getOperand(0).getValueType() > VT)
1158 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1160 return Operand.Val->getOperand(0);
1163 case ISD::BIT_CONVERT:
1164 // Basic sanity checking.
1165 assert((Operand.getValueType() == MVT::Vector || // FIXME: This is a hack.
1166 MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType()))
1167 && "Cannot BIT_CONVERT between two different types!");
1168 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1169 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1170 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1172 case ISD::SCALAR_TO_VECTOR:
1173 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1174 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1175 "Illegal SCALAR_TO_VECTOR node!");
1178 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1179 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1180 Operand.Val->getOperand(0));
1181 if (OpOpcode == ISD::FNEG) // --X -> X
1182 return Operand.Val->getOperand(0);
1185 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1186 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1191 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1192 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
1193 if (E) return SDOperand(E, 0);
1194 E = N = new SDNode(Opcode, Operand);
1196 N = new SDNode(Opcode, Operand);
1198 N->setValueTypes(VT);
1199 AllNodes.push_back(N);
1200 return SDOperand(N, 0);
1205 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1206 SDOperand N1, SDOperand N2) {
1209 case ISD::TokenFactor:
1210 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1211 N2.getValueType() == MVT::Other && "Invalid token factor!");
1220 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1227 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1234 assert(N1.getValueType() == N2.getValueType() &&
1235 N1.getValueType() == VT && "Binary operator types must match!");
1237 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1238 assert(N1.getValueType() == VT &&
1239 MVT::isFloatingPoint(N1.getValueType()) &&
1240 MVT::isFloatingPoint(N2.getValueType()) &&
1241 "Invalid FCOPYSIGN!");
1248 assert(VT == N1.getValueType() &&
1249 "Shift operators return type must be the same as their first arg");
1250 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1251 VT != MVT::i1 && "Shifts only work on integers");
1253 case ISD::FP_ROUND_INREG: {
1254 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1255 assert(VT == N1.getValueType() && "Not an inreg round!");
1256 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1257 "Cannot FP_ROUND_INREG integer types");
1258 assert(EVT <= VT && "Not rounding down!");
1261 case ISD::AssertSext:
1262 case ISD::AssertZext:
1263 case ISD::SIGN_EXTEND_INREG: {
1264 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1265 assert(VT == N1.getValueType() && "Not an inreg extend!");
1266 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1267 "Cannot *_EXTEND_INREG FP types");
1268 assert(EVT <= VT && "Not extending!");
1275 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1276 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1279 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1281 case ISD::ADD: return getConstant(C1 + C2, VT);
1282 case ISD::SUB: return getConstant(C1 - C2, VT);
1283 case ISD::MUL: return getConstant(C1 * C2, VT);
1285 if (C2) return getConstant(C1 / C2, VT);
1288 if (C2) return getConstant(C1 % C2, VT);
1291 if (C2) return getConstant(N1C->getSignExtended() /
1292 N2C->getSignExtended(), VT);
1295 if (C2) return getConstant(N1C->getSignExtended() %
1296 N2C->getSignExtended(), VT);
1298 case ISD::AND : return getConstant(C1 & C2, VT);
1299 case ISD::OR : return getConstant(C1 | C2, VT);
1300 case ISD::XOR : return getConstant(C1 ^ C2, VT);
1301 case ISD::SHL : return getConstant(C1 << C2, VT);
1302 case ISD::SRL : return getConstant(C1 >> C2, VT);
1303 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
1305 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1308 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1312 } else { // Cannonicalize constant to RHS if commutative
1313 if (isCommutativeBinOp(Opcode)) {
1314 std::swap(N1C, N2C);
1320 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1321 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1324 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1326 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1327 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1328 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1330 if (C2) return getConstantFP(C1 / C2, VT);
1333 if (C2) return getConstantFP(fmod(C1, C2), VT);
1335 case ISD::FCOPYSIGN: {
1346 if (u2.I < 0) // Sign bit of RHS set?
1347 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1349 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1350 return getConstantFP(u1.F, VT);
1354 } else { // Cannonicalize constant to RHS if commutative
1355 if (isCommutativeBinOp(Opcode)) {
1356 std::swap(N1CFP, N2CFP);
1362 // Finally, fold operations that do not require constants.
1364 case ISD::FP_ROUND_INREG:
1365 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1367 case ISD::SIGN_EXTEND_INREG: {
1368 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1369 if (EVT == VT) return N1; // Not actually extending
1373 // FIXME: figure out how to safely handle things like
1374 // int foo(int x) { return 1 << (x & 255); }
1375 // int bar() { return foo(256); }
1380 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1381 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
1382 return getNode(Opcode, VT, N1, N2.getOperand(0));
1383 else if (N2.getOpcode() == ISD::AND)
1384 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1385 // If the and is only masking out bits that cannot effect the shift,
1386 // eliminate the and.
1387 unsigned NumBits = MVT::getSizeInBits(VT);
1388 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1389 return getNode(Opcode, VT, N1, N2.getOperand(0));
1395 // Memoize this node if possible.
1397 if (VT != MVT::Flag) {
1398 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1399 if (BON) return SDOperand(BON, 0);
1401 BON = N = new SDNode(Opcode, N1, N2);
1403 N = new SDNode(Opcode, N1, N2);
1406 N->setValueTypes(VT);
1407 AllNodes.push_back(N);
1408 return SDOperand(N, 0);
1411 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1412 SDOperand N1, SDOperand N2, SDOperand N3) {
1413 // Perform various simplifications.
1414 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1415 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1416 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1419 // Use SimplifySetCC to simplify SETCC's.
1420 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
1421 if (Simp.Val) return Simp;
1426 if (N1C->getValue())
1427 return N2; // select true, X, Y -> X
1429 return N3; // select false, X, Y -> Y
1431 if (N2 == N3) return N2; // select C, X, X -> X
1435 if (N2C->getValue()) // Unconditional branch
1436 return getNode(ISD::BR, MVT::Other, N1, N3);
1438 return N1; // Never-taken branch
1440 case ISD::VECTOR_SHUFFLE:
1441 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1442 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1443 N3.getOpcode() == ISD::BUILD_VECTOR &&
1444 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1445 "Illegal VECTOR_SHUFFLE node!");
1449 std::vector<SDOperand> Ops;
1455 // Memoize node if it doesn't produce a flag.
1457 if (VT != MVT::Flag) {
1458 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1459 if (E) return SDOperand(E, 0);
1460 E = N = new SDNode(Opcode, N1, N2, N3);
1462 N = new SDNode(Opcode, N1, N2, N3);
1464 N->setValueTypes(VT);
1465 AllNodes.push_back(N);
1466 return SDOperand(N, 0);
1469 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1470 SDOperand N1, SDOperand N2, SDOperand N3,
1472 std::vector<SDOperand> Ops;
1478 return getNode(Opcode, VT, Ops);
1481 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1482 SDOperand N1, SDOperand N2, SDOperand N3,
1483 SDOperand N4, SDOperand N5) {
1484 std::vector<SDOperand> Ops;
1491 return getNode(Opcode, VT, Ops);
1494 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1495 SDOperand Chain, SDOperand Ptr,
1497 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1498 if (N) return SDOperand(N, 0);
1499 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1501 // Loads have a token chain.
1502 setNodeValueTypes(N, VT, MVT::Other);
1503 AllNodes.push_back(N);
1504 return SDOperand(N, 0);
1507 SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1508 SDOperand Chain, SDOperand Ptr,
1510 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1511 if (N) return SDOperand(N, 0);
1512 std::vector<SDOperand> Ops;
1514 Ops.push_back(Chain);
1517 Ops.push_back(getConstant(Count, MVT::i32));
1518 Ops.push_back(getValueType(EVT));
1519 std::vector<MVT::ValueType> VTs;
1521 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1522 return getNode(ISD::VLOAD, VTs, Ops);
1525 SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1526 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1527 MVT::ValueType EVT) {
1528 std::vector<SDOperand> Ops;
1530 Ops.push_back(Chain);
1533 Ops.push_back(getValueType(EVT));
1534 std::vector<MVT::ValueType> VTs;
1536 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1537 return getNode(Opcode, VTs, Ops);
1540 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
1541 assert((!V || isa<PointerType>(V->getType())) &&
1542 "SrcValue is not a pointer?");
1543 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1544 if (N) return SDOperand(N, 0);
1546 N = new SrcValueSDNode(V, Offset);
1547 AllNodes.push_back(N);
1548 return SDOperand(N, 0);
1551 SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1552 SDOperand Chain, SDOperand Ptr,
1554 std::vector<SDOperand> Ops;
1556 Ops.push_back(Chain);
1559 std::vector<MVT::ValueType> VTs;
1561 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1562 return getNode(ISD::VAARG, VTs, Ops);
1565 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1566 std::vector<SDOperand> &Ops) {
1567 switch (Ops.size()) {
1568 case 0: return getNode(Opcode, VT);
1569 case 1: return getNode(Opcode, VT, Ops[0]);
1570 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1571 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
1575 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
1578 case ISD::TRUNCSTORE: {
1579 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1580 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1581 #if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1582 // If this is a truncating store of a constant, convert to the desired type
1583 // and store it instead.
1584 if (isa<Constant>(Ops[0])) {
1585 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1586 if (isa<Constant>(Op))
1589 // Also for ConstantFP?
1591 if (Ops[0].getValueType() == EVT) // Normal store?
1592 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1593 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1594 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1595 "Can't do FP-INT conversion!");
1598 case ISD::SELECT_CC: {
1599 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
1600 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1601 "LHS and RHS of condition must have same type!");
1602 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1603 "True and False arms of SelectCC must have same type!");
1604 assert(Ops[2].getValueType() == VT &&
1605 "select_cc node must be of same type as true and false value!");
1609 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
1610 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1611 "LHS/RHS of comparison should match types!");
1618 if (VT != MVT::Flag) {
1620 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1621 if (E) return SDOperand(E, 0);
1622 E = N = new SDNode(Opcode, Ops);
1624 N = new SDNode(Opcode, Ops);
1626 N->setValueTypes(VT);
1627 AllNodes.push_back(N);
1628 return SDOperand(N, 0);
1631 SDOperand SelectionDAG::getNode(unsigned Opcode,
1632 std::vector<MVT::ValueType> &ResultTys,
1633 std::vector<SDOperand> &Ops) {
1634 if (ResultTys.size() == 1)
1635 return getNode(Opcode, ResultTys[0], Ops);
1640 case ISD::ZEXTLOAD: {
1641 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1642 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1643 // If they are asking for an extending load from/to the same thing, return a
1645 if (ResultTys[0] == EVT)
1646 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1647 if (MVT::isVector(ResultTys[0])) {
1648 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1649 "Invalid vector extload!");
1651 assert(EVT < ResultTys[0] &&
1652 "Should only be an extending load, not truncating!");
1654 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1655 "Cannot sign/zero extend a FP/Vector load!");
1656 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1657 "Cannot convert from FP to Int or Int -> FP!");
1661 // FIXME: figure out how to safely handle things like
1662 // int foo(int x) { return 1 << (x & 255); }
1663 // int bar() { return foo(256); }
1665 case ISD::SRA_PARTS:
1666 case ISD::SRL_PARTS:
1667 case ISD::SHL_PARTS:
1668 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1669 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
1670 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1671 else if (N3.getOpcode() == ISD::AND)
1672 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1673 // If the and is only masking out bits that cannot effect the shift,
1674 // eliminate the and.
1675 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1676 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1677 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1683 // Memoize the node unless it returns a flag.
1685 if (ResultTys.back() != MVT::Flag) {
1687 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1688 if (E) return SDOperand(E, 0);
1689 E = N = new SDNode(Opcode, Ops);
1691 N = new SDNode(Opcode, Ops);
1693 setNodeValueTypes(N, ResultTys);
1694 AllNodes.push_back(N);
1695 return SDOperand(N, 0);
1698 void SelectionDAG::setNodeValueTypes(SDNode *N,
1699 std::vector<MVT::ValueType> &RetVals) {
1700 switch (RetVals.size()) {
1702 case 1: N->setValueTypes(RetVals[0]); return;
1703 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1707 std::list<std::vector<MVT::ValueType> >::iterator I =
1708 std::find(VTList.begin(), VTList.end(), RetVals);
1709 if (I == VTList.end()) {
1710 VTList.push_front(RetVals);
1714 N->setValueTypes(&(*I)[0], I->size());
1717 void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1718 MVT::ValueType VT2) {
1719 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1720 E = VTList.end(); I != E; ++I) {
1721 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1722 N->setValueTypes(&(*I)[0], 2);
1726 std::vector<MVT::ValueType> V;
1729 VTList.push_front(V);
1730 N->setValueTypes(&(*VTList.begin())[0], 2);
1733 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1734 /// specified operands. If the resultant node already exists in the DAG,
1735 /// this does not modify the specified node, instead it returns the node that
1736 /// already exists. If the resultant node does not exist in the DAG, the
1737 /// input node is returned. As a degenerate case, if you specify the same
1738 /// input operands as the node already has, the input node is returned.
1739 SDOperand SelectionDAG::
1740 UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1741 SDNode *N = InN.Val;
1742 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1744 // Check to see if there is no change.
1745 if (Op == N->getOperand(0)) return InN;
1747 // See if the modified node already exists.
1748 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1749 if (NewSlot && *NewSlot)
1750 return SDOperand(*NewSlot, InN.ResNo);
1752 // Nope it doesn't. Remove the node from it's current place in the maps.
1754 RemoveNodeFromCSEMaps(N);
1756 // Now we update the operands.
1757 N->OperandList[0].Val->removeUser(N);
1759 N->OperandList[0] = Op;
1761 // If this gets put into a CSE map, add it.
1762 if (NewSlot) *NewSlot = N;
1766 SDOperand SelectionDAG::
1767 UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1768 SDNode *N = InN.Val;
1769 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1771 // Check to see if there is no change.
1772 bool AnyChange = false;
1773 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1774 return InN; // No operands changed, just return the input node.
1776 // See if the modified node already exists.
1777 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1778 if (NewSlot && *NewSlot)
1779 return SDOperand(*NewSlot, InN.ResNo);
1781 // Nope it doesn't. Remove the node from it's current place in the maps.
1783 RemoveNodeFromCSEMaps(N);
1785 // Now we update the operands.
1786 if (N->OperandList[0] != Op1) {
1787 N->OperandList[0].Val->removeUser(N);
1788 Op1.Val->addUser(N);
1789 N->OperandList[0] = Op1;
1791 if (N->OperandList[1] != Op2) {
1792 N->OperandList[1].Val->removeUser(N);
1793 Op2.Val->addUser(N);
1794 N->OperandList[1] = Op2;
1797 // If this gets put into a CSE map, add it.
1798 if (NewSlot) *NewSlot = N;
1802 SDOperand SelectionDAG::
1803 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1804 std::vector<SDOperand> Ops;
1808 return UpdateNodeOperands(N, Ops);
1811 SDOperand SelectionDAG::
1812 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1813 SDOperand Op3, SDOperand Op4) {
1814 std::vector<SDOperand> Ops;
1819 return UpdateNodeOperands(N, Ops);
1822 SDOperand SelectionDAG::
1823 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1824 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1825 std::vector<SDOperand> Ops;
1831 return UpdateNodeOperands(N, Ops);
1835 SDOperand SelectionDAG::
1836 UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1837 SDNode *N = InN.Val;
1838 assert(N->getNumOperands() == Ops.size() &&
1839 "Update with wrong number of operands");
1841 // Check to see if there is no change.
1842 unsigned NumOps = Ops.size();
1843 bool AnyChange = false;
1844 for (unsigned i = 0; i != NumOps; ++i) {
1845 if (Ops[i] != N->getOperand(i)) {
1851 // No operands changed, just return the input node.
1852 if (!AnyChange) return InN;
1854 // See if the modified node already exists.
1855 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1856 if (NewSlot && *NewSlot)
1857 return SDOperand(*NewSlot, InN.ResNo);
1859 // Nope it doesn't. Remove the node from it's current place in the maps.
1861 RemoveNodeFromCSEMaps(N);
1863 // Now we update the operands.
1864 for (unsigned i = 0; i != NumOps; ++i) {
1865 if (N->OperandList[i] != Ops[i]) {
1866 N->OperandList[i].Val->removeUser(N);
1867 Ops[i].Val->addUser(N);
1868 N->OperandList[i] = Ops[i];
1872 // If this gets put into a CSE map, add it.
1873 if (NewSlot) *NewSlot = N;
1880 /// SelectNodeTo - These are used for target selectors to *mutate* the
1881 /// specified node to have the specified return type, Target opcode, and
1882 /// operands. Note that target opcodes are stored as
1883 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1885 /// Note that SelectNodeTo returns the resultant node. If there is already a
1886 /// node of the specified opcode and operands, it returns that node instead of
1887 /// the current one.
1888 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1889 MVT::ValueType VT) {
1890 // If an identical node already exists, use it.
1891 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1892 if (ON) return SDOperand(ON, 0);
1894 RemoveNodeFromCSEMaps(N);
1896 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1897 N->setValueTypes(VT);
1899 ON = N; // Memoize the new node.
1900 return SDOperand(N, 0);
1903 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1904 MVT::ValueType VT, SDOperand Op1) {
1905 // If an identical node already exists, use it.
1906 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1907 std::make_pair(Op1, VT))];
1908 if (ON) return SDOperand(ON, 0);
1910 RemoveNodeFromCSEMaps(N);
1911 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1912 N->setValueTypes(VT);
1913 N->setOperands(Op1);
1915 ON = N; // Memoize the new node.
1916 return SDOperand(N, 0);
1919 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1920 MVT::ValueType VT, SDOperand Op1,
1922 // If an identical node already exists, use it.
1923 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1924 std::make_pair(Op1, Op2))];
1925 if (ON) return SDOperand(ON, 0);
1927 RemoveNodeFromCSEMaps(N);
1928 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1929 N->setValueTypes(VT);
1930 N->setOperands(Op1, Op2);
1932 ON = N; // Memoize the new node.
1933 return SDOperand(N, 0);
1936 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1937 MVT::ValueType VT, SDOperand Op1,
1938 SDOperand Op2, SDOperand Op3) {
1939 // If an identical node already exists, use it.
1940 std::vector<SDOperand> OpList;
1941 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1942 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1943 std::make_pair(VT, OpList))];
1944 if (ON) return SDOperand(ON, 0);
1946 RemoveNodeFromCSEMaps(N);
1947 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1948 N->setValueTypes(VT);
1949 N->setOperands(Op1, Op2, Op3);
1951 ON = N; // Memoize the new node.
1952 return SDOperand(N, 0);
1955 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1956 MVT::ValueType VT, SDOperand Op1,
1957 SDOperand Op2, SDOperand Op3,
1959 // If an identical node already exists, use it.
1960 std::vector<SDOperand> OpList;
1961 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1962 OpList.push_back(Op4);
1963 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1964 std::make_pair(VT, OpList))];
1965 if (ON) return SDOperand(ON, 0);
1967 RemoveNodeFromCSEMaps(N);
1968 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1969 N->setValueTypes(VT);
1970 N->setOperands(Op1, Op2, Op3, Op4);
1972 ON = N; // Memoize the new node.
1973 return SDOperand(N, 0);
1976 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1977 MVT::ValueType VT, SDOperand Op1,
1978 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1980 // If an identical node already exists, use it.
1981 std::vector<SDOperand> OpList;
1982 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1983 OpList.push_back(Op4); OpList.push_back(Op5);
1984 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1985 std::make_pair(VT, OpList))];
1986 if (ON) return SDOperand(ON, 0);
1988 RemoveNodeFromCSEMaps(N);
1989 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1990 N->setValueTypes(VT);
1991 N->setOperands(Op1, Op2, Op3, Op4, Op5);
1993 ON = N; // Memoize the new node.
1994 return SDOperand(N, 0);
1997 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1998 MVT::ValueType VT, SDOperand Op1,
1999 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2000 SDOperand Op5, SDOperand Op6) {
2001 // If an identical node already exists, use it.
2002 std::vector<SDOperand> OpList;
2003 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2004 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2005 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2006 std::make_pair(VT, OpList))];
2007 if (ON) return SDOperand(ON, 0);
2009 RemoveNodeFromCSEMaps(N);
2010 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2011 N->setValueTypes(VT);
2012 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
2014 ON = N; // Memoize the new node.
2015 return SDOperand(N, 0);
2018 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2019 MVT::ValueType VT, SDOperand Op1,
2020 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2021 SDOperand Op5, SDOperand Op6,
2023 // If an identical node already exists, use it.
2024 std::vector<SDOperand> OpList;
2025 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2026 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2027 OpList.push_back(Op7);
2028 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2029 std::make_pair(VT, OpList))];
2030 if (ON) return SDOperand(ON, 0);
2032 RemoveNodeFromCSEMaps(N);
2033 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2034 N->setValueTypes(VT);
2035 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
2037 ON = N; // Memoize the new node.
2038 return SDOperand(N, 0);
2040 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2041 MVT::ValueType VT, SDOperand Op1,
2042 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2043 SDOperand Op5, SDOperand Op6,
2044 SDOperand Op7, SDOperand Op8) {
2045 // If an identical node already exists, use it.
2046 std::vector<SDOperand> OpList;
2047 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2048 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2049 OpList.push_back(Op7); OpList.push_back(Op8);
2050 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2051 std::make_pair(VT, OpList))];
2052 if (ON) return SDOperand(ON, 0);
2054 RemoveNodeFromCSEMaps(N);
2055 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2056 N->setValueTypes(VT);
2057 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2059 ON = N; // Memoize the new node.
2060 return SDOperand(N, 0);
2063 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2064 MVT::ValueType VT1, MVT::ValueType VT2,
2065 SDOperand Op1, SDOperand Op2) {
2066 // If an identical node already exists, use it.
2067 std::vector<SDOperand> OpList;
2068 OpList.push_back(Op1); OpList.push_back(Op2);
2069 std::vector<MVT::ValueType> VTList;
2070 VTList.push_back(VT1); VTList.push_back(VT2);
2071 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2072 std::make_pair(VTList, OpList))];
2073 if (ON) return SDOperand(ON, 0);
2075 RemoveNodeFromCSEMaps(N);
2076 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2077 setNodeValueTypes(N, VT1, VT2);
2078 N->setOperands(Op1, Op2);
2080 ON = N; // Memoize the new node.
2081 return SDOperand(N, 0);
2084 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2085 MVT::ValueType VT1, MVT::ValueType VT2,
2086 SDOperand Op1, SDOperand Op2,
2088 // If an identical node already exists, use it.
2089 std::vector<SDOperand> OpList;
2090 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2091 std::vector<MVT::ValueType> VTList;
2092 VTList.push_back(VT1); VTList.push_back(VT2);
2093 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2094 std::make_pair(VTList, OpList))];
2095 if (ON) return SDOperand(ON, 0);
2097 RemoveNodeFromCSEMaps(N);
2098 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2099 setNodeValueTypes(N, VT1, VT2);
2100 N->setOperands(Op1, Op2, Op3);
2102 ON = N; // Memoize the new node.
2103 return SDOperand(N, 0);
2106 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2107 MVT::ValueType VT1, MVT::ValueType VT2,
2108 SDOperand Op1, SDOperand Op2,
2109 SDOperand Op3, SDOperand Op4) {
2110 // If an identical node already exists, use it.
2111 std::vector<SDOperand> OpList;
2112 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2113 OpList.push_back(Op4);
2114 std::vector<MVT::ValueType> VTList;
2115 VTList.push_back(VT1); VTList.push_back(VT2);
2116 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2117 std::make_pair(VTList, OpList))];
2118 if (ON) return SDOperand(ON, 0);
2120 RemoveNodeFromCSEMaps(N);
2121 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2122 setNodeValueTypes(N, VT1, VT2);
2123 N->setOperands(Op1, Op2, Op3, Op4);
2125 ON = N; // Memoize the new node.
2126 return SDOperand(N, 0);
2129 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2130 MVT::ValueType VT1, MVT::ValueType VT2,
2131 SDOperand Op1, SDOperand Op2,
2132 SDOperand Op3, SDOperand Op4,
2134 // If an identical node already exists, use it.
2135 std::vector<SDOperand> OpList;
2136 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2137 OpList.push_back(Op4); OpList.push_back(Op5);
2138 std::vector<MVT::ValueType> VTList;
2139 VTList.push_back(VT1); VTList.push_back(VT2);
2140 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2141 std::make_pair(VTList, OpList))];
2142 if (ON) return SDOperand(ON, 0);
2144 RemoveNodeFromCSEMaps(N);
2145 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2146 setNodeValueTypes(N, VT1, VT2);
2147 N->setOperands(Op1, Op2, Op3, Op4, Op5);
2149 ON = N; // Memoize the new node.
2150 return SDOperand(N, 0);
2153 /// getTargetNode - These are used for target selectors to create a new node
2154 /// with specified return type(s), target opcode, and operands.
2156 /// Note that getTargetNode returns the resultant node. If there is already a
2157 /// node of the specified opcode and operands, it returns that node instead of
2158 /// the current one.
2159 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2160 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2162 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2164 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2166 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2167 SDOperand Op1, SDOperand Op2) {
2168 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2170 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2171 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2172 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2174 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2175 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2177 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2179 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2180 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2181 SDOperand Op4, SDOperand Op5) {
2182 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2184 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2185 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2186 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
2187 std::vector<SDOperand> Ops;
2195 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2197 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2198 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2199 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2201 std::vector<SDOperand> Ops;
2210 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2212 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2213 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2214 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2215 SDOperand Op7, SDOperand Op8) {
2216 std::vector<SDOperand> Ops;
2226 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2228 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2229 std::vector<SDOperand> &Ops) {
2230 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2232 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2233 MVT::ValueType VT2, SDOperand Op1) {
2234 std::vector<MVT::ValueType> ResultTys;
2235 ResultTys.push_back(VT1);
2236 ResultTys.push_back(VT2);
2237 std::vector<SDOperand> Ops;
2239 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2241 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2242 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
2243 std::vector<MVT::ValueType> ResultTys;
2244 ResultTys.push_back(VT1);
2245 ResultTys.push_back(VT2);
2246 std::vector<SDOperand> Ops;
2249 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2251 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2252 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2254 std::vector<MVT::ValueType> ResultTys;
2255 ResultTys.push_back(VT1);
2256 ResultTys.push_back(VT2);
2257 std::vector<SDOperand> Ops;
2261 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2263 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2264 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2265 SDOperand Op3, SDOperand Op4) {
2266 std::vector<MVT::ValueType> ResultTys;
2267 ResultTys.push_back(VT1);
2268 ResultTys.push_back(VT2);
2269 std::vector<SDOperand> Ops;
2274 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2276 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2277 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2278 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2279 std::vector<MVT::ValueType> ResultTys;
2280 ResultTys.push_back(VT1);
2281 ResultTys.push_back(VT2);
2282 std::vector<SDOperand> Ops;
2288 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2290 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2291 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2292 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2294 std::vector<MVT::ValueType> ResultTys;
2295 ResultTys.push_back(VT1);
2296 ResultTys.push_back(VT2);
2297 std::vector<SDOperand> Ops;
2304 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2306 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2307 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2308 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2309 SDOperand Op6, SDOperand Op7) {
2310 std::vector<MVT::ValueType> ResultTys;
2311 ResultTys.push_back(VT1);
2312 ResultTys.push_back(VT2);
2313 std::vector<SDOperand> Ops;
2321 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2323 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2324 MVT::ValueType VT2, MVT::ValueType VT3,
2325 SDOperand Op1, SDOperand Op2) {
2326 std::vector<MVT::ValueType> ResultTys;
2327 ResultTys.push_back(VT1);
2328 ResultTys.push_back(VT2);
2329 ResultTys.push_back(VT3);
2330 std::vector<SDOperand> Ops;
2333 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2335 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2336 MVT::ValueType VT2, MVT::ValueType VT3,
2337 SDOperand Op1, SDOperand Op2,
2338 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2339 std::vector<MVT::ValueType> ResultTys;
2340 ResultTys.push_back(VT1);
2341 ResultTys.push_back(VT2);
2342 ResultTys.push_back(VT3);
2343 std::vector<SDOperand> Ops;
2349 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2351 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2352 MVT::ValueType VT2, MVT::ValueType VT3,
2353 SDOperand Op1, SDOperand Op2,
2354 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2356 std::vector<MVT::ValueType> ResultTys;
2357 ResultTys.push_back(VT1);
2358 ResultTys.push_back(VT2);
2359 ResultTys.push_back(VT3);
2360 std::vector<SDOperand> Ops;
2367 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2369 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2370 MVT::ValueType VT2, MVT::ValueType VT3,
2371 SDOperand Op1, SDOperand Op2,
2372 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2373 SDOperand Op6, SDOperand Op7) {
2374 std::vector<MVT::ValueType> ResultTys;
2375 ResultTys.push_back(VT1);
2376 ResultTys.push_back(VT2);
2377 ResultTys.push_back(VT3);
2378 std::vector<SDOperand> Ops;
2386 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2388 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2389 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
2390 std::vector<MVT::ValueType> ResultTys;
2391 ResultTys.push_back(VT1);
2392 ResultTys.push_back(VT2);
2393 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2396 // ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2397 /// This can cause recursive merging of nodes in the DAG.
2399 /// This version assumes From/To have a single result value.
2401 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2402 std::vector<SDNode*> *Deleted) {
2403 SDNode *From = FromN.Val, *To = ToN.Val;
2404 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2405 "Cannot replace with this method!");
2406 assert(From != To && "Cannot replace uses of with self");
2408 while (!From->use_empty()) {
2409 // Process users until they are all gone.
2410 SDNode *U = *From->use_begin();
2412 // This node is about to morph, remove its old self from the CSE maps.
2413 RemoveNodeFromCSEMaps(U);
2415 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2417 if (I->Val == From) {
2418 From->removeUser(U);
2423 // Now that we have modified U, add it back to the CSE maps. If it already
2424 // exists there, recursively merge the results together.
2425 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2426 ReplaceAllUsesWith(U, Existing, Deleted);
2428 if (Deleted) Deleted->push_back(U);
2429 DeleteNodeNotInCSEMaps(U);
2434 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2435 /// This can cause recursive merging of nodes in the DAG.
2437 /// This version assumes From/To have matching types and numbers of result
2440 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2441 std::vector<SDNode*> *Deleted) {
2442 assert(From != To && "Cannot replace uses of with self");
2443 assert(From->getNumValues() == To->getNumValues() &&
2444 "Cannot use this version of ReplaceAllUsesWith!");
2445 if (From->getNumValues() == 1) { // If possible, use the faster version.
2446 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
2450 while (!From->use_empty()) {
2451 // Process users until they are all gone.
2452 SDNode *U = *From->use_begin();
2454 // This node is about to morph, remove its old self from the CSE maps.
2455 RemoveNodeFromCSEMaps(U);
2457 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2459 if (I->Val == From) {
2460 From->removeUser(U);
2465 // Now that we have modified U, add it back to the CSE maps. If it already
2466 // exists there, recursively merge the results together.
2467 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2468 ReplaceAllUsesWith(U, Existing, Deleted);
2470 if (Deleted) Deleted->push_back(U);
2471 DeleteNodeNotInCSEMaps(U);
2476 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2477 /// This can cause recursive merging of nodes in the DAG.
2479 /// This version can replace From with any result values. To must match the
2480 /// number and types of values returned by From.
2481 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
2482 const std::vector<SDOperand> &To,
2483 std::vector<SDNode*> *Deleted) {
2484 assert(From->getNumValues() == To.size() &&
2485 "Incorrect number of values to replace with!");
2486 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
2487 // Degenerate case handled above.
2488 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
2492 while (!From->use_empty()) {
2493 // Process users until they are all gone.
2494 SDNode *U = *From->use_begin();
2496 // This node is about to morph, remove its old self from the CSE maps.
2497 RemoveNodeFromCSEMaps(U);
2499 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2501 if (I->Val == From) {
2502 const SDOperand &ToOp = To[I->ResNo];
2503 From->removeUser(U);
2505 ToOp.Val->addUser(U);
2508 // Now that we have modified U, add it back to the CSE maps. If it already
2509 // exists there, recursively merge the results together.
2510 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2511 ReplaceAllUsesWith(U, Existing, Deleted);
2513 if (Deleted) Deleted->push_back(U);
2514 DeleteNodeNotInCSEMaps(U);
2519 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2520 /// uses of other values produced by From.Val alone. The Deleted vector is
2521 /// handled the same was as for ReplaceAllUsesWith.
2522 void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2523 std::vector<SDNode*> &Deleted) {
2524 assert(From != To && "Cannot replace a value with itself");
2525 // Handle the simple, trivial, case efficiently.
2526 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2527 ReplaceAllUsesWith(From, To, &Deleted);
2531 // Get all of the users in a nice, deterministically ordered, uniqued set.
2532 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2534 while (!Users.empty()) {
2535 // We know that this user uses some value of From. If it is the right
2536 // value, update it.
2537 SDNode *User = Users.back();
2540 for (SDOperand *Op = User->OperandList,
2541 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2543 // Okay, we know this user needs to be updated. Remove its old self
2544 // from the CSE maps.
2545 RemoveNodeFromCSEMaps(User);
2547 // Update all operands that match "From".
2548 for (; Op != E; ++Op) {
2550 From.Val->removeUser(User);
2552 To.Val->addUser(User);
2556 // Now that we have modified User, add it back to the CSE maps. If it
2557 // already exists there, recursively merge the results together.
2558 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2559 unsigned NumDeleted = Deleted.size();
2560 ReplaceAllUsesWith(User, Existing, &Deleted);
2562 // User is now dead.
2563 Deleted.push_back(User);
2564 DeleteNodeNotInCSEMaps(User);
2566 // We have to be careful here, because ReplaceAllUsesWith could have
2567 // deleted a user of From, which means there may be dangling pointers
2568 // in the "Users" setvector. Scan over the deleted node pointers and
2569 // remove them from the setvector.
2570 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2571 Users.remove(Deleted[i]);
2573 break; // Exit the operand scanning loop.
2580 //===----------------------------------------------------------------------===//
2582 //===----------------------------------------------------------------------===//
2585 /// getValueTypeList - Return a pointer to the specified value type.
2587 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2588 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2593 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2594 /// indicated value. This method ignores uses of other values defined by this
2596 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
2597 assert(Value < getNumValues() && "Bad value!");
2599 // If there is only one value, this is easy.
2600 if (getNumValues() == 1)
2601 return use_size() == NUses;
2602 if (Uses.size() < NUses) return false;
2604 SDOperand TheValue(const_cast<SDNode *>(this), Value);
2606 std::set<SDNode*> UsersHandled;
2608 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
2611 if (User->getNumOperands() == 1 ||
2612 UsersHandled.insert(User).second) // First time we've seen this?
2613 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2614 if (User->getOperand(i) == TheValue) {
2616 return false; // too many uses
2621 // Found exactly the right number of uses?
2626 // isOnlyUse - Return true if this node is the only use of N.
2627 bool SDNode::isOnlyUse(SDNode *N) const {
2629 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2640 // isOperand - Return true if this node is an operand of N.
2641 bool SDOperand::isOperand(SDNode *N) const {
2642 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2643 if (*this == N->getOperand(i))
2648 bool SDNode::isOperand(SDNode *N) const {
2649 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2650 if (this == N->OperandList[i].Val)
2655 const char *SDNode::getOperationName(const SelectionDAG *G) const {
2656 switch (getOpcode()) {
2658 if (getOpcode() < ISD::BUILTIN_OP_END)
2659 return "<<Unknown DAG Node>>";
2662 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
2663 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2664 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
2666 TargetLowering &TLI = G->getTargetLoweringInfo();
2668 TLI.getTargetNodeName(getOpcode());
2669 if (Name) return Name;
2672 return "<<Unknown Target Node>>";
2675 case ISD::PCMARKER: return "PCMarker";
2676 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
2677 case ISD::SRCVALUE: return "SrcValue";
2678 case ISD::EntryToken: return "EntryToken";
2679 case ISD::TokenFactor: return "TokenFactor";
2680 case ISD::AssertSext: return "AssertSext";
2681 case ISD::AssertZext: return "AssertZext";
2683 case ISD::STRING: return "String";
2684 case ISD::BasicBlock: return "BasicBlock";
2685 case ISD::VALUETYPE: return "ValueType";
2686 case ISD::Register: return "Register";
2688 case ISD::Constant: return "Constant";
2689 case ISD::ConstantFP: return "ConstantFP";
2690 case ISD::GlobalAddress: return "GlobalAddress";
2691 case ISD::FrameIndex: return "FrameIndex";
2692 case ISD::ConstantPool: return "ConstantPool";
2693 case ISD::ExternalSymbol: return "ExternalSymbol";
2694 case ISD::INTRINSIC_WO_CHAIN: {
2695 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2696 return Intrinsic::getName((Intrinsic::ID)IID);
2698 case ISD::INTRINSIC_VOID:
2699 case ISD::INTRINSIC_W_CHAIN: {
2700 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
2701 return Intrinsic::getName((Intrinsic::ID)IID);
2704 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
2705 case ISD::TargetConstant: return "TargetConstant";
2706 case ISD::TargetConstantFP:return "TargetConstantFP";
2707 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2708 case ISD::TargetFrameIndex: return "TargetFrameIndex";
2709 case ISD::TargetConstantPool: return "TargetConstantPool";
2710 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
2712 case ISD::CopyToReg: return "CopyToReg";
2713 case ISD::CopyFromReg: return "CopyFromReg";
2714 case ISD::UNDEF: return "undef";
2715 case ISD::MERGE_VALUES: return "mergevalues";
2716 case ISD::INLINEASM: return "inlineasm";
2717 case ISD::HANDLENODE: return "handlenode";
2720 case ISD::FABS: return "fabs";
2721 case ISD::FNEG: return "fneg";
2722 case ISD::FSQRT: return "fsqrt";
2723 case ISD::FSIN: return "fsin";
2724 case ISD::FCOS: return "fcos";
2727 case ISD::ADD: return "add";
2728 case ISD::SUB: return "sub";
2729 case ISD::MUL: return "mul";
2730 case ISD::MULHU: return "mulhu";
2731 case ISD::MULHS: return "mulhs";
2732 case ISD::SDIV: return "sdiv";
2733 case ISD::UDIV: return "udiv";
2734 case ISD::SREM: return "srem";
2735 case ISD::UREM: return "urem";
2736 case ISD::AND: return "and";
2737 case ISD::OR: return "or";
2738 case ISD::XOR: return "xor";
2739 case ISD::SHL: return "shl";
2740 case ISD::SRA: return "sra";
2741 case ISD::SRL: return "srl";
2742 case ISD::ROTL: return "rotl";
2743 case ISD::ROTR: return "rotr";
2744 case ISD::FADD: return "fadd";
2745 case ISD::FSUB: return "fsub";
2746 case ISD::FMUL: return "fmul";
2747 case ISD::FDIV: return "fdiv";
2748 case ISD::FREM: return "frem";
2749 case ISD::FCOPYSIGN: return "fcopysign";
2750 case ISD::VADD: return "vadd";
2751 case ISD::VSUB: return "vsub";
2752 case ISD::VMUL: return "vmul";
2754 case ISD::SETCC: return "setcc";
2755 case ISD::SELECT: return "select";
2756 case ISD::SELECT_CC: return "select_cc";
2757 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2758 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2759 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
2760 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
2761 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2762 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2763 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2764 case ISD::VBIT_CONVERT: return "vbit_convert";
2765 case ISD::ADDC: return "addc";
2766 case ISD::ADDE: return "adde";
2767 case ISD::SUBC: return "subc";
2768 case ISD::SUBE: return "sube";
2769 case ISD::SHL_PARTS: return "shl_parts";
2770 case ISD::SRA_PARTS: return "sra_parts";
2771 case ISD::SRL_PARTS: return "srl_parts";
2773 // Conversion operators.
2774 case ISD::SIGN_EXTEND: return "sign_extend";
2775 case ISD::ZERO_EXTEND: return "zero_extend";
2776 case ISD::ANY_EXTEND: return "any_extend";
2777 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
2778 case ISD::TRUNCATE: return "truncate";
2779 case ISD::FP_ROUND: return "fp_round";
2780 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
2781 case ISD::FP_EXTEND: return "fp_extend";
2783 case ISD::SINT_TO_FP: return "sint_to_fp";
2784 case ISD::UINT_TO_FP: return "uint_to_fp";
2785 case ISD::FP_TO_SINT: return "fp_to_sint";
2786 case ISD::FP_TO_UINT: return "fp_to_uint";
2787 case ISD::BIT_CONVERT: return "bit_convert";
2789 // Control flow instructions
2790 case ISD::BR: return "br";
2791 case ISD::BRCOND: return "brcond";
2792 case ISD::BR_CC: return "br_cc";
2793 case ISD::RET: return "ret";
2794 case ISD::CALLSEQ_START: return "callseq_start";
2795 case ISD::CALLSEQ_END: return "callseq_end";
2798 case ISD::LOAD: return "load";
2799 case ISD::STORE: return "store";
2800 case ISD::VLOAD: return "vload";
2801 case ISD::EXTLOAD: return "extload";
2802 case ISD::SEXTLOAD: return "sextload";
2803 case ISD::ZEXTLOAD: return "zextload";
2804 case ISD::TRUNCSTORE: return "truncstore";
2805 case ISD::VAARG: return "vaarg";
2806 case ISD::VACOPY: return "vacopy";
2807 case ISD::VAEND: return "vaend";
2808 case ISD::VASTART: return "vastart";
2809 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2810 case ISD::EXTRACT_ELEMENT: return "extract_element";
2811 case ISD::BUILD_PAIR: return "build_pair";
2812 case ISD::STACKSAVE: return "stacksave";
2813 case ISD::STACKRESTORE: return "stackrestore";
2815 // Block memory operations.
2816 case ISD::MEMSET: return "memset";
2817 case ISD::MEMCPY: return "memcpy";
2818 case ISD::MEMMOVE: return "memmove";
2821 case ISD::BSWAP: return "bswap";
2822 case ISD::CTPOP: return "ctpop";
2823 case ISD::CTTZ: return "cttz";
2824 case ISD::CTLZ: return "ctlz";
2827 case ISD::LOCATION: return "location";
2828 case ISD::DEBUG_LOC: return "debug_loc";
2829 case ISD::DEBUG_LABEL: return "debug_label";
2832 switch (cast<CondCodeSDNode>(this)->get()) {
2833 default: assert(0 && "Unknown setcc condition!");
2834 case ISD::SETOEQ: return "setoeq";
2835 case ISD::SETOGT: return "setogt";
2836 case ISD::SETOGE: return "setoge";
2837 case ISD::SETOLT: return "setolt";
2838 case ISD::SETOLE: return "setole";
2839 case ISD::SETONE: return "setone";
2841 case ISD::SETO: return "seto";
2842 case ISD::SETUO: return "setuo";
2843 case ISD::SETUEQ: return "setue";
2844 case ISD::SETUGT: return "setugt";
2845 case ISD::SETUGE: return "setuge";
2846 case ISD::SETULT: return "setult";
2847 case ISD::SETULE: return "setule";
2848 case ISD::SETUNE: return "setune";
2850 case ISD::SETEQ: return "seteq";
2851 case ISD::SETGT: return "setgt";
2852 case ISD::SETGE: return "setge";
2853 case ISD::SETLT: return "setlt";
2854 case ISD::SETLE: return "setle";
2855 case ISD::SETNE: return "setne";
2860 void SDNode::dump() const { dump(0); }
2861 void SDNode::dump(const SelectionDAG *G) const {
2862 std::cerr << (void*)this << ": ";
2864 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2865 if (i) std::cerr << ",";
2866 if (getValueType(i) == MVT::Other)
2869 std::cerr << MVT::getValueTypeString(getValueType(i));
2871 std::cerr << " = " << getOperationName(G);
2874 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2875 if (i) std::cerr << ", ";
2876 std::cerr << (void*)getOperand(i).Val;
2877 if (unsigned RN = getOperand(i).ResNo)
2878 std::cerr << ":" << RN;
2881 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2882 std::cerr << "<" << CSDN->getValue() << ">";
2883 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2884 std::cerr << "<" << CSDN->getValue() << ">";
2885 } else if (const GlobalAddressSDNode *GADN =
2886 dyn_cast<GlobalAddressSDNode>(this)) {
2887 int offset = GADN->getOffset();
2889 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
2891 std::cerr << " + " << offset;
2893 std::cerr << " " << offset;
2894 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
2895 std::cerr << "<" << FIDN->getIndex() << ">";
2896 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
2897 int offset = CP->getOffset();
2898 std::cerr << "<" << *CP->get() << ">";
2900 std::cerr << " + " << offset;
2902 std::cerr << " " << offset;
2903 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
2905 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2907 std::cerr << LBB->getName() << " ";
2908 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
2909 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
2910 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2911 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2913 std::cerr << " #" << R->getReg();
2915 } else if (const ExternalSymbolSDNode *ES =
2916 dyn_cast<ExternalSymbolSDNode>(this)) {
2917 std::cerr << "'" << ES->getSymbol() << "'";
2918 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2920 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2922 std::cerr << "<null:" << M->getOffset() << ">";
2923 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2924 std::cerr << ":" << getValueTypeString(N->getVT());
2928 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
2929 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2930 if (N->getOperand(i).Val->hasOneUse())
2931 DumpNodes(N->getOperand(i).Val, indent+2, G);
2933 std::cerr << "\n" << std::string(indent+2, ' ')
2934 << (void*)N->getOperand(i).Val << ": <multiple use>";
2937 std::cerr << "\n" << std::string(indent, ' ');
2941 void SelectionDAG::dump() const {
2942 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
2943 std::vector<const SDNode*> Nodes;
2944 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2948 std::sort(Nodes.begin(), Nodes.end());
2950 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2951 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
2952 DumpNodes(Nodes[i], 2, this);
2955 DumpNodes(getRoot().Val, 2, this);
2957 std::cerr << "\n\n";
2960 /// InsertISelMapEntry - A helper function to insert a key / element pair
2961 /// into a SDOperand to SDOperand map. This is added to avoid the map
2962 /// insertion operator from being inlined.
2963 void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
2964 SDNode *Key, unsigned KeyResNo,
2965 SDNode *Element, unsigned ElementResNo) {
2966 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
2967 SDOperand(Element, ElementResNo)));