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 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) ==
100 // Okay, we have at least one ~0 value, check to see if the rest match or are
102 for (++i; i != e; ++i)
103 if (N->getOperand(i) != NotZero &&
104 N->getOperand(i).getOpcode() != ISD::UNDEF)
110 /// isBuildVectorAllZeros - Return true if the specified node is a
111 /// BUILD_VECTOR where all of the elements are 0 or undef.
112 bool ISD::isBuildVectorAllZeros(const SDNode *N) {
113 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
115 unsigned i = 0, e = N->getNumOperands();
117 // Skip over all of the undef values.
118 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
121 // Do not accept an all-undef vector.
122 if (i == e) return false;
124 // Do not accept build_vectors that aren't all constants or which have non-~0
126 SDOperand Zero = N->getOperand(i);
127 if (isa<ConstantSDNode>(Zero)) {
128 if (!cast<ConstantSDNode>(Zero)->isNullValue())
130 } else if (isa<ConstantFPSDNode>(Zero)) {
131 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
136 // Okay, we have at least one ~0 value, check to see if the rest match or are
138 for (++i; i != e; ++i)
139 if (N->getOperand(i) != Zero &&
140 N->getOperand(i).getOpcode() != ISD::UNDEF)
145 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
146 /// when given the operation for (X op Y).
147 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
148 // To perform this operation, we just need to swap the L and G bits of the
150 unsigned OldL = (Operation >> 2) & 1;
151 unsigned OldG = (Operation >> 1) & 1;
152 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
153 (OldL << 1) | // New G bit
154 (OldG << 2)); // New L bit.
157 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
158 /// 'op' is a valid SetCC operation.
159 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
160 unsigned Operation = Op;
162 Operation ^= 7; // Flip L, G, E bits, but not U.
164 Operation ^= 15; // Flip all of the condition bits.
165 if (Operation > ISD::SETTRUE2)
166 Operation &= ~8; // Don't let N and U bits get set.
167 return ISD::CondCode(Operation);
171 /// isSignedOp - For an integer comparison, return 1 if the comparison is a
172 /// signed operation and 2 if the result is an unsigned comparison. Return zero
173 /// if the operation does not depend on the sign of the input (setne and seteq).
174 static int isSignedOp(ISD::CondCode Opcode) {
176 default: assert(0 && "Illegal integer setcc operation!");
178 case ISD::SETNE: return 0;
182 case ISD::SETGE: return 1;
186 case ISD::SETUGE: return 2;
190 /// getSetCCOrOperation - Return the result of a logical OR between different
191 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
192 /// returns SETCC_INVALID if it is not possible to represent the resultant
194 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
196 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
197 // Cannot fold a signed integer setcc with an unsigned integer setcc.
198 return ISD::SETCC_INVALID;
200 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
202 // If the N and U bits get set then the resultant comparison DOES suddenly
203 // care about orderedness, and is true when ordered.
204 if (Op > ISD::SETTRUE2)
205 Op &= ~16; // Clear the N bit.
206 return ISD::CondCode(Op);
209 /// getSetCCAndOperation - Return the result of a logical AND between different
210 /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
211 /// function returns zero if it is not possible to represent the resultant
213 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
215 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
216 // Cannot fold a signed setcc with an unsigned setcc.
217 return ISD::SETCC_INVALID;
219 // Combine all of the condition bits.
220 return ISD::CondCode(Op1 & Op2);
223 const TargetMachine &SelectionDAG::getTarget() const {
224 return TLI.getTargetMachine();
227 //===----------------------------------------------------------------------===//
228 // SelectionDAG Class
229 //===----------------------------------------------------------------------===//
231 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
232 /// SelectionDAG, including nodes (like loads) that have uses of their token
233 /// chain but no other uses and no side effect. If a node is passed in as an
234 /// argument, it is used as the seed for node deletion.
235 void SelectionDAG::RemoveDeadNodes(SDNode *N) {
236 // Create a dummy node (which is not added to allnodes), that adds a reference
237 // to the root node, preventing it from being deleted.
238 HandleSDNode Dummy(getRoot());
240 bool MadeChange = false;
242 // If we have a hint to start from, use it.
243 if (N && N->use_empty()) {
248 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
249 if (I->use_empty() && I->getOpcode() != 65535) {
250 // Node is dead, recursively delete newly dead uses.
255 // Walk the nodes list, removing the nodes we've marked as dead.
257 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
264 // If the root changed (e.g. it was a dead load, update the root).
265 setRoot(Dummy.getValue());
268 /// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
269 /// graph. If it is the last user of any of its operands, recursively process
270 /// them the same way.
272 void SelectionDAG::DestroyDeadNode(SDNode *N) {
273 // Okay, we really are going to delete this node. First take this out of the
274 // appropriate CSE map.
275 RemoveNodeFromCSEMaps(N);
277 // Next, brutally remove the operand list. This is safe to do, as there are
278 // no cycles in the graph.
279 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
283 // Now that we removed this operand, see if there are no uses of it left.
287 delete[] N->OperandList;
291 // Mark the node as dead.
292 N->MorphNodeTo(65535);
295 void SelectionDAG::DeleteNode(SDNode *N) {
296 assert(N->use_empty() && "Cannot delete a node that is not dead!");
298 // First take this out of the appropriate CSE map.
299 RemoveNodeFromCSEMaps(N);
301 // Finally, remove uses due to operands of this node, remove from the
302 // AllNodes list, and delete the node.
303 DeleteNodeNotInCSEMaps(N);
306 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
308 // Remove it from the AllNodes list.
311 // Drop all of the operands and decrement used nodes use counts.
312 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
313 I->Val->removeUser(N);
314 delete[] N->OperandList;
321 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
322 /// correspond to it. This is useful when we're about to delete or repurpose
323 /// the node. We don't want future request for structurally identical nodes
324 /// to return N anymore.
325 void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
327 switch (N->getOpcode()) {
328 case ISD::HANDLENODE: return; // noop.
330 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
331 N->getValueType(0)));
333 case ISD::TargetConstant:
334 Erased = TargetConstants.erase(std::make_pair(
335 cast<ConstantSDNode>(N)->getValue(),
336 N->getValueType(0)));
338 case ISD::ConstantFP: {
339 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
340 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
343 case ISD::TargetConstantFP: {
344 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
345 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
349 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
352 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
353 "Cond code doesn't exist!");
354 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
355 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
357 case ISD::GlobalAddress: {
358 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
359 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
363 case ISD::TargetGlobalAddress: {
364 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
365 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
369 case ISD::FrameIndex:
370 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
372 case ISD::TargetFrameIndex:
373 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
375 case ISD::ConstantPool:
376 Erased = ConstantPoolIndices.
377 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
378 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
379 cast<ConstantPoolSDNode>(N)->getAlignment())));
381 case ISD::TargetConstantPool:
382 Erased = TargetConstantPoolIndices.
383 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
384 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
385 cast<ConstantPoolSDNode>(N)->getAlignment())));
387 case ISD::BasicBlock:
388 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
390 case ISD::ExternalSymbol:
391 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
393 case ISD::TargetExternalSymbol:
395 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
398 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
399 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
402 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
403 N->getValueType(0)));
405 case ISD::SRCVALUE: {
406 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
407 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
411 Erased = Loads.erase(std::make_pair(N->getOperand(1),
412 std::make_pair(N->getOperand(0),
413 N->getValueType(0))));
416 if (N->getNumValues() == 1) {
417 if (N->getNumOperands() == 0) {
418 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
419 N->getValueType(0)));
420 } else if (N->getNumOperands() == 1) {
422 UnaryOps.erase(std::make_pair(N->getOpcode(),
423 std::make_pair(N->getOperand(0),
424 N->getValueType(0))));
425 } else if (N->getNumOperands() == 2) {
427 BinaryOps.erase(std::make_pair(N->getOpcode(),
428 std::make_pair(N->getOperand(0),
431 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
433 OneResultNodes.erase(std::make_pair(N->getOpcode(),
434 std::make_pair(N->getValueType(0),
438 // Remove the node from the ArbitraryNodes map.
439 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
440 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
442 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
443 std::make_pair(RV, Ops)));
448 // Verify that the node was actually in one of the CSE maps, unless it has a
449 // flag result (which cannot be CSE'd) or is one of the special cases that are
450 // not subject to CSE.
451 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
452 !N->isTargetOpcode()) {
454 assert(0 && "Node is not in map!");
459 /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
460 /// has been taken out and modified in some way. If the specified node already
461 /// exists in the CSE maps, do not modify the maps, but return the existing node
462 /// instead. If it doesn't exist, add it and return null.
464 SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
465 assert(N->getNumOperands() && "This is a leaf node!");
466 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
467 return 0; // Never add these nodes.
469 // Check that remaining values produced are not flags.
470 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
471 if (N->getValueType(i) == MVT::Flag)
472 return 0; // Never CSE anything that produces a flag.
474 if (N->getNumValues() == 1) {
475 if (N->getNumOperands() == 1) {
476 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
477 std::make_pair(N->getOperand(0),
478 N->getValueType(0)))];
481 } else if (N->getNumOperands() == 2) {
482 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
483 std::make_pair(N->getOperand(0),
488 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
489 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
490 std::make_pair(N->getValueType(0), Ops))];
495 if (N->getOpcode() == ISD::LOAD) {
496 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
497 std::make_pair(N->getOperand(0),
498 N->getValueType(0)))];
502 // Remove the node from the ArbitraryNodes map.
503 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
504 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
505 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
506 std::make_pair(RV, Ops))];
514 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
515 /// were replaced with those specified. If this node is never memoized,
516 /// return null, otherwise return a pointer to the slot it would take. If a
517 /// node already exists with these operands, the slot will be non-null.
518 SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
519 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
520 return 0; // Never add these nodes.
522 // Check that remaining values produced are not flags.
523 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
524 if (N->getValueType(i) == MVT::Flag)
525 return 0; // Never CSE anything that produces a flag.
527 if (N->getNumValues() == 1) {
528 return &UnaryOps[std::make_pair(N->getOpcode(),
529 std::make_pair(Op, N->getValueType(0)))];
531 // Remove the node from the ArbitraryNodes map.
532 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
533 std::vector<SDOperand> Ops;
535 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
536 std::make_pair(RV, Ops))];
541 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
542 /// were replaced with those specified. If this node is never memoized,
543 /// return null, otherwise return a pointer to the slot it would take. If a
544 /// node already exists with these operands, the slot will be non-null.
545 SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
546 SDOperand Op1, SDOperand Op2) {
547 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
548 return 0; // Never add these nodes.
550 // Check that remaining values produced are not flags.
551 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
552 if (N->getValueType(i) == MVT::Flag)
553 return 0; // Never CSE anything that produces a flag.
555 if (N->getNumValues() == 1) {
556 return &BinaryOps[std::make_pair(N->getOpcode(),
557 std::make_pair(Op1, Op2))];
559 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
560 std::vector<SDOperand> Ops;
563 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
564 std::make_pair(RV, Ops))];
570 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
571 /// were replaced with those specified. If this node is never memoized,
572 /// return null, otherwise return a pointer to the slot it would take. If a
573 /// node already exists with these operands, the slot will be non-null.
574 SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
575 const std::vector<SDOperand> &Ops) {
576 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
577 return 0; // Never add these nodes.
579 // Check that remaining values produced are not flags.
580 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
581 if (N->getValueType(i) == MVT::Flag)
582 return 0; // Never CSE anything that produces a flag.
584 if (N->getNumValues() == 1) {
585 if (N->getNumOperands() == 1) {
586 return &UnaryOps[std::make_pair(N->getOpcode(),
587 std::make_pair(Ops[0],
588 N->getValueType(0)))];
589 } else if (N->getNumOperands() == 2) {
590 return &BinaryOps[std::make_pair(N->getOpcode(),
591 std::make_pair(Ops[0], Ops[1]))];
593 return &OneResultNodes[std::make_pair(N->getOpcode(),
594 std::make_pair(N->getValueType(0),
598 if (N->getOpcode() == ISD::LOAD) {
599 return &Loads[std::make_pair(Ops[1],
600 std::make_pair(Ops[0], N->getValueType(0)))];
602 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
603 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
604 std::make_pair(RV, Ops))];
611 SelectionDAG::~SelectionDAG() {
612 while (!AllNodes.empty()) {
613 SDNode *N = AllNodes.begin();
614 delete [] N->OperandList;
617 AllNodes.pop_front();
621 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
622 if (Op.getValueType() == VT) return Op;
623 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
624 return getNode(ISD::AND, Op.getValueType(), Op,
625 getConstant(Imm, Op.getValueType()));
628 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
629 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
630 // Mask out any bits that are not valid for this constant.
632 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
634 SDNode *&N = Constants[std::make_pair(Val, VT)];
635 if (N) return SDOperand(N, 0);
636 N = new ConstantSDNode(false, Val, VT);
637 AllNodes.push_back(N);
638 return SDOperand(N, 0);
641 SDOperand SelectionDAG::getString(const std::string &Val) {
642 StringSDNode *&N = StringNodes[Val];
644 N = new StringSDNode(Val);
645 AllNodes.push_back(N);
647 return SDOperand(N, 0);
650 SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
651 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
652 // Mask out any bits that are not valid for this constant.
654 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
656 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
657 if (N) return SDOperand(N, 0);
658 N = new ConstantSDNode(true, Val, VT);
659 AllNodes.push_back(N);
660 return SDOperand(N, 0);
663 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
664 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
666 Val = (float)Val; // Mask out extra precision.
668 // Do the map lookup using the actual bit pattern for the floating point
669 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
670 // we don't have issues with SNANs.
671 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
672 if (N) return SDOperand(N, 0);
673 N = new ConstantFPSDNode(false, Val, VT);
674 AllNodes.push_back(N);
675 return SDOperand(N, 0);
678 SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
679 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
681 Val = (float)Val; // Mask out extra precision.
683 // Do the map lookup using the actual bit pattern for the floating point
684 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
685 // we don't have issues with SNANs.
686 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
687 if (N) return SDOperand(N, 0);
688 N = new ConstantFPSDNode(true, Val, VT);
689 AllNodes.push_back(N);
690 return SDOperand(N, 0);
693 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
694 MVT::ValueType VT, int offset) {
695 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
696 if (N) return SDOperand(N, 0);
697 N = new GlobalAddressSDNode(false, GV, VT, offset);
698 AllNodes.push_back(N);
699 return SDOperand(N, 0);
702 SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
703 MVT::ValueType VT, int offset) {
704 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
705 if (N) return SDOperand(N, 0);
706 N = new GlobalAddressSDNode(true, GV, VT, offset);
707 AllNodes.push_back(N);
708 return SDOperand(N, 0);
711 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
712 SDNode *&N = FrameIndices[FI];
713 if (N) return SDOperand(N, 0);
714 N = new FrameIndexSDNode(FI, VT, false);
715 AllNodes.push_back(N);
716 return SDOperand(N, 0);
719 SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
720 SDNode *&N = TargetFrameIndices[FI];
721 if (N) return SDOperand(N, 0);
722 N = new FrameIndexSDNode(FI, VT, true);
723 AllNodes.push_back(N);
724 return SDOperand(N, 0);
727 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
728 unsigned Alignment, int Offset) {
729 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
730 std::make_pair(Offset, Alignment))];
731 if (N) return SDOperand(N, 0);
732 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
733 AllNodes.push_back(N);
734 return SDOperand(N, 0);
737 SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
738 unsigned Alignment, int Offset) {
739 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
740 std::make_pair(Offset, Alignment))];
741 if (N) return SDOperand(N, 0);
742 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
743 AllNodes.push_back(N);
744 return SDOperand(N, 0);
747 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
748 SDNode *&N = BBNodes[MBB];
749 if (N) return SDOperand(N, 0);
750 N = new BasicBlockSDNode(MBB);
751 AllNodes.push_back(N);
752 return SDOperand(N, 0);
755 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
756 if ((unsigned)VT >= ValueTypeNodes.size())
757 ValueTypeNodes.resize(VT+1);
758 if (ValueTypeNodes[VT] == 0) {
759 ValueTypeNodes[VT] = new VTSDNode(VT);
760 AllNodes.push_back(ValueTypeNodes[VT]);
763 return SDOperand(ValueTypeNodes[VT], 0);
766 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
767 SDNode *&N = ExternalSymbols[Sym];
768 if (N) return SDOperand(N, 0);
769 N = new ExternalSymbolSDNode(false, Sym, VT);
770 AllNodes.push_back(N);
771 return SDOperand(N, 0);
774 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
776 SDNode *&N = TargetExternalSymbols[Sym];
777 if (N) return SDOperand(N, 0);
778 N = new ExternalSymbolSDNode(true, Sym, VT);
779 AllNodes.push_back(N);
780 return SDOperand(N, 0);
783 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
784 if ((unsigned)Cond >= CondCodeNodes.size())
785 CondCodeNodes.resize(Cond+1);
787 if (CondCodeNodes[Cond] == 0) {
788 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
789 AllNodes.push_back(CondCodeNodes[Cond]);
791 return SDOperand(CondCodeNodes[Cond], 0);
794 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
795 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
797 Reg = new RegisterSDNode(RegNo, VT);
798 AllNodes.push_back(Reg);
800 return SDOperand(Reg, 0);
803 SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
804 SDOperand N2, ISD::CondCode Cond) {
805 // These setcc operations always fold.
809 case ISD::SETFALSE2: return getConstant(0, VT);
811 case ISD::SETTRUE2: return getConstant(1, VT);
814 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
815 uint64_t C2 = N2C->getValue();
816 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
817 uint64_t C1 = N1C->getValue();
819 // Sign extend the operands if required
820 if (ISD::isSignedIntSetCC(Cond)) {
821 C1 = N1C->getSignExtended();
822 C2 = N2C->getSignExtended();
826 default: assert(0 && "Unknown integer setcc!");
827 case ISD::SETEQ: return getConstant(C1 == C2, VT);
828 case ISD::SETNE: return getConstant(C1 != C2, VT);
829 case ISD::SETULT: return getConstant(C1 < C2, VT);
830 case ISD::SETUGT: return getConstant(C1 > C2, VT);
831 case ISD::SETULE: return getConstant(C1 <= C2, VT);
832 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
833 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
834 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
835 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
836 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
839 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
840 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
841 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
843 // If the comparison constant has bits in the upper part, the
844 // zero-extended value could never match.
845 if (C2 & (~0ULL << InSize)) {
846 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
850 case ISD::SETEQ: return getConstant(0, VT);
853 case ISD::SETNE: return getConstant(1, VT);
856 // True if the sign bit of C2 is set.
857 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
860 // True if the sign bit of C2 isn't set.
861 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
867 // Otherwise, we can perform the comparison with the low bits.
875 return getSetCC(VT, N1.getOperand(0),
876 getConstant(C2, N1.getOperand(0).getValueType()),
879 break; // todo, be more careful with signed comparisons
881 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
882 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
883 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
884 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
885 MVT::ValueType ExtDstTy = N1.getValueType();
886 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
888 // If the extended part has any inconsistent bits, it cannot ever
889 // compare equal. In other words, they have to be all ones or all
892 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
893 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
894 return getConstant(Cond == ISD::SETNE, VT);
896 // Otherwise, make this a use of a zext.
897 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
898 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
902 uint64_t MinVal, MaxVal;
903 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
904 if (ISD::isSignedIntSetCC(Cond)) {
905 MinVal = 1ULL << (OperandBitSize-1);
906 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
907 MaxVal = ~0ULL >> (65-OperandBitSize);
912 MaxVal = ~0ULL >> (64-OperandBitSize);
915 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
916 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
917 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
918 --C2; // X >= C1 --> X > (C1-1)
919 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
920 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
923 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
924 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
925 ++C2; // X <= C1 --> X < (C1+1)
926 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
927 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
930 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
931 return getConstant(0, VT); // X < MIN --> false
933 // Canonicalize setgt X, Min --> setne X, Min
934 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
935 return getSetCC(VT, N1, N2, ISD::SETNE);
937 // If we have setult X, 1, turn it into seteq X, 0
938 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
939 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
941 // If we have setugt X, Max-1, turn it into seteq X, Max
942 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
943 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
946 // If we have "setcc X, C1", check to see if we can shrink the immediate
949 // SETUGT X, SINTMAX -> SETLT X, 0
950 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
951 C2 == (~0ULL >> (65-OperandBitSize)))
952 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
954 // FIXME: Implement the rest of these.
957 // Fold bit comparisons when we can.
958 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
959 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
960 if (ConstantSDNode *AndRHS =
961 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
962 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
963 // Perform the xform if the AND RHS is a single bit.
964 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
965 return getNode(ISD::SRL, VT, N1,
966 getConstant(Log2_64(AndRHS->getValue()),
967 TLI.getShiftAmountTy()));
969 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
970 // (X & 8) == 8 --> (X & 8) >> 3
971 // Perform the xform if C2 is a single bit.
972 if ((C2 & (C2-1)) == 0) {
973 return getNode(ISD::SRL, VT, N1,
974 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
979 } else if (isa<ConstantSDNode>(N1.Val)) {
980 // Ensure that the constant occurs on the RHS.
981 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
984 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
985 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
986 double C1 = N1C->getValue(), C2 = N2C->getValue();
989 default: break; // FIXME: Implement the rest of these!
990 case ISD::SETEQ: return getConstant(C1 == C2, VT);
991 case ISD::SETNE: return getConstant(C1 != C2, VT);
992 case ISD::SETLT: return getConstant(C1 < C2, VT);
993 case ISD::SETGT: return getConstant(C1 > C2, VT);
994 case ISD::SETLE: return getConstant(C1 <= C2, VT);
995 case ISD::SETGE: return getConstant(C1 >= C2, VT);
998 // Ensure that the constant occurs on the RHS.
999 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
1002 // Could not fold it.
1006 /// getNode - Gets or creates the specified node.
1008 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
1009 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
1011 N = new SDNode(Opcode, VT);
1012 AllNodes.push_back(N);
1014 return SDOperand(N, 0);
1017 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1018 SDOperand Operand) {
1020 // Constant fold unary operations with an integer constant operand.
1021 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1022 uint64_t Val = C->getValue();
1025 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
1026 case ISD::ANY_EXTEND:
1027 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1028 case ISD::TRUNCATE: return getConstant(Val, VT);
1029 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1030 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
1031 case ISD::BIT_CONVERT:
1032 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
1033 return getConstantFP(BitsToFloat(Val), VT);
1034 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
1035 return getConstantFP(BitsToDouble(Val), VT);
1039 default: assert(0 && "Invalid bswap!"); break;
1040 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1041 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1042 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1047 default: assert(0 && "Invalid ctpop!"); break;
1048 case MVT::i1: return getConstant(Val != 0, VT);
1050 Tmp1 = (unsigned)Val & 0xFF;
1051 return getConstant(CountPopulation_32(Tmp1), VT);
1053 Tmp1 = (unsigned)Val & 0xFFFF;
1054 return getConstant(CountPopulation_32(Tmp1), VT);
1056 return getConstant(CountPopulation_32((unsigned)Val), VT);
1058 return getConstant(CountPopulation_64(Val), VT);
1062 default: assert(0 && "Invalid ctlz!"); break;
1063 case MVT::i1: return getConstant(Val == 0, VT);
1065 Tmp1 = (unsigned)Val & 0xFF;
1066 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1068 Tmp1 = (unsigned)Val & 0xFFFF;
1069 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1071 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1073 return getConstant(CountLeadingZeros_64(Val), VT);
1077 default: assert(0 && "Invalid cttz!"); break;
1078 case MVT::i1: return getConstant(Val == 0, VT);
1080 Tmp1 = (unsigned)Val | 0x100;
1081 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1083 Tmp1 = (unsigned)Val | 0x10000;
1084 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1086 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1088 return getConstant(CountTrailingZeros_64(Val), VT);
1093 // Constant fold unary operations with an floating point constant operand.
1094 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1097 return getConstantFP(-C->getValue(), VT);
1099 return getConstantFP(fabs(C->getValue()), VT);
1101 case ISD::FP_EXTEND:
1102 return getConstantFP(C->getValue(), VT);
1103 case ISD::FP_TO_SINT:
1104 return getConstant((int64_t)C->getValue(), VT);
1105 case ISD::FP_TO_UINT:
1106 return getConstant((uint64_t)C->getValue(), VT);
1107 case ISD::BIT_CONVERT:
1108 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1109 return getConstant(FloatToBits(C->getValue()), VT);
1110 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1111 return getConstant(DoubleToBits(C->getValue()), VT);
1115 unsigned OpOpcode = Operand.Val->getOpcode();
1117 case ISD::TokenFactor:
1118 return Operand; // Factor of one node? No factor.
1119 case ISD::SIGN_EXTEND:
1120 if (Operand.getValueType() == VT) return Operand; // noop extension
1121 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
1122 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1123 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1125 case ISD::ZERO_EXTEND:
1126 if (Operand.getValueType() == VT) return Operand; // noop extension
1127 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
1128 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1129 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1131 case ISD::ANY_EXTEND:
1132 if (Operand.getValueType() == VT) return Operand; // noop extension
1133 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
1134 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1135 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1136 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1139 if (Operand.getValueType() == VT) return Operand; // noop truncate
1140 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
1141 if (OpOpcode == ISD::TRUNCATE)
1142 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1143 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1144 OpOpcode == ISD::ANY_EXTEND) {
1145 // If the source is smaller than the dest, we still need an extend.
1146 if (Operand.Val->getOperand(0).getValueType() < VT)
1147 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1148 else if (Operand.Val->getOperand(0).getValueType() > VT)
1149 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1151 return Operand.Val->getOperand(0);
1154 case ISD::BIT_CONVERT:
1155 // Basic sanity checking.
1156 assert((Operand.getValueType() == MVT::Vector || // FIXME: This is a hack.
1157 MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType()))
1158 && "Cannot BIT_CONVERT between two different types!");
1159 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1160 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1161 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1163 case ISD::SCALAR_TO_VECTOR:
1164 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1165 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1166 "Illegal SCALAR_TO_VECTOR node!");
1169 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1170 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1171 Operand.Val->getOperand(0));
1172 if (OpOpcode == ISD::FNEG) // --X -> X
1173 return Operand.Val->getOperand(0);
1176 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1177 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1182 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1183 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
1184 if (E) return SDOperand(E, 0);
1185 E = N = new SDNode(Opcode, Operand);
1187 N = new SDNode(Opcode, Operand);
1189 N->setValueTypes(VT);
1190 AllNodes.push_back(N);
1191 return SDOperand(N, 0);
1196 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1197 SDOperand N1, SDOperand N2) {
1200 case ISD::TokenFactor:
1201 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1202 N2.getValueType() == MVT::Other && "Invalid token factor!");
1211 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1218 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1225 assert(N1.getValueType() == N2.getValueType() &&
1226 N1.getValueType() == VT && "Binary operator types must match!");
1228 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1229 assert(N1.getValueType() == VT &&
1230 MVT::isFloatingPoint(N1.getValueType()) &&
1231 MVT::isFloatingPoint(N2.getValueType()) &&
1232 "Invalid FCOPYSIGN!");
1239 assert(VT == N1.getValueType() &&
1240 "Shift operators return type must be the same as their first arg");
1241 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1242 VT != MVT::i1 && "Shifts only work on integers");
1244 case ISD::FP_ROUND_INREG: {
1245 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1246 assert(VT == N1.getValueType() && "Not an inreg round!");
1247 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1248 "Cannot FP_ROUND_INREG integer types");
1249 assert(EVT <= VT && "Not rounding down!");
1252 case ISD::AssertSext:
1253 case ISD::AssertZext:
1254 case ISD::SIGN_EXTEND_INREG: {
1255 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1256 assert(VT == N1.getValueType() && "Not an inreg extend!");
1257 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1258 "Cannot *_EXTEND_INREG FP types");
1259 assert(EVT <= VT && "Not extending!");
1266 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1267 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1270 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1272 case ISD::ADD: return getConstant(C1 + C2, VT);
1273 case ISD::SUB: return getConstant(C1 - C2, VT);
1274 case ISD::MUL: return getConstant(C1 * C2, VT);
1276 if (C2) return getConstant(C1 / C2, VT);
1279 if (C2) return getConstant(C1 % C2, VT);
1282 if (C2) return getConstant(N1C->getSignExtended() /
1283 N2C->getSignExtended(), VT);
1286 if (C2) return getConstant(N1C->getSignExtended() %
1287 N2C->getSignExtended(), VT);
1289 case ISD::AND : return getConstant(C1 & C2, VT);
1290 case ISD::OR : return getConstant(C1 | C2, VT);
1291 case ISD::XOR : return getConstant(C1 ^ C2, VT);
1292 case ISD::SHL : return getConstant(C1 << C2, VT);
1293 case ISD::SRL : return getConstant(C1 >> C2, VT);
1294 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
1296 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1299 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1303 } else { // Cannonicalize constant to RHS if commutative
1304 if (isCommutativeBinOp(Opcode)) {
1305 std::swap(N1C, N2C);
1311 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1312 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1315 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1317 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1318 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1319 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1321 if (C2) return getConstantFP(C1 / C2, VT);
1324 if (C2) return getConstantFP(fmod(C1, C2), VT);
1326 case ISD::FCOPYSIGN: {
1337 if (u2.I < 0) // Sign bit of RHS set?
1338 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1340 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1341 return getConstantFP(u1.F, VT);
1345 } else { // Cannonicalize constant to RHS if commutative
1346 if (isCommutativeBinOp(Opcode)) {
1347 std::swap(N1CFP, N2CFP);
1353 // Finally, fold operations that do not require constants.
1355 case ISD::FP_ROUND_INREG:
1356 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1358 case ISD::SIGN_EXTEND_INREG: {
1359 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1360 if (EVT == VT) return N1; // Not actually extending
1364 // FIXME: figure out how to safely handle things like
1365 // int foo(int x) { return 1 << (x & 255); }
1366 // int bar() { return foo(256); }
1371 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1372 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
1373 return getNode(Opcode, VT, N1, N2.getOperand(0));
1374 else if (N2.getOpcode() == ISD::AND)
1375 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1376 // If the and is only masking out bits that cannot effect the shift,
1377 // eliminate the and.
1378 unsigned NumBits = MVT::getSizeInBits(VT);
1379 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1380 return getNode(Opcode, VT, N1, N2.getOperand(0));
1386 // Memoize this node if possible.
1388 if (VT != MVT::Flag) {
1389 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1390 if (BON) return SDOperand(BON, 0);
1392 BON = N = new SDNode(Opcode, N1, N2);
1394 N = new SDNode(Opcode, N1, N2);
1397 N->setValueTypes(VT);
1398 AllNodes.push_back(N);
1399 return SDOperand(N, 0);
1402 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1403 SDOperand N1, SDOperand N2, SDOperand N3) {
1404 // Perform various simplifications.
1405 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1406 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1407 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1410 // Use SimplifySetCC to simplify SETCC's.
1411 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
1412 if (Simp.Val) return Simp;
1417 if (N1C->getValue())
1418 return N2; // select true, X, Y -> X
1420 return N3; // select false, X, Y -> Y
1422 if (N2 == N3) return N2; // select C, X, X -> X
1426 if (N2C->getValue()) // Unconditional branch
1427 return getNode(ISD::BR, MVT::Other, N1, N3);
1429 return N1; // Never-taken branch
1431 case ISD::VECTOR_SHUFFLE:
1432 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1433 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1434 N3.getOpcode() == ISD::BUILD_VECTOR &&
1435 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1436 "Illegal VECTOR_SHUFFLE node!");
1440 std::vector<SDOperand> Ops;
1446 // Memoize node if it doesn't produce a flag.
1448 if (VT != MVT::Flag) {
1449 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1450 if (E) return SDOperand(E, 0);
1451 E = N = new SDNode(Opcode, N1, N2, N3);
1453 N = new SDNode(Opcode, N1, N2, N3);
1455 N->setValueTypes(VT);
1456 AllNodes.push_back(N);
1457 return SDOperand(N, 0);
1460 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1461 SDOperand N1, SDOperand N2, SDOperand N3,
1463 std::vector<SDOperand> Ops;
1469 return getNode(Opcode, VT, Ops);
1472 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1473 SDOperand N1, SDOperand N2, SDOperand N3,
1474 SDOperand N4, SDOperand N5) {
1475 std::vector<SDOperand> Ops;
1482 return getNode(Opcode, VT, Ops);
1485 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1486 SDOperand Chain, SDOperand Ptr,
1488 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1489 if (N) return SDOperand(N, 0);
1490 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1492 // Loads have a token chain.
1493 setNodeValueTypes(N, VT, MVT::Other);
1494 AllNodes.push_back(N);
1495 return SDOperand(N, 0);
1498 SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1499 SDOperand Chain, SDOperand Ptr,
1501 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1502 if (N) return SDOperand(N, 0);
1503 std::vector<SDOperand> Ops;
1505 Ops.push_back(Chain);
1508 Ops.push_back(getConstant(Count, MVT::i32));
1509 Ops.push_back(getValueType(EVT));
1510 std::vector<MVT::ValueType> VTs;
1512 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1513 return getNode(ISD::VLOAD, VTs, Ops);
1516 SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1517 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1518 MVT::ValueType EVT) {
1519 std::vector<SDOperand> Ops;
1521 Ops.push_back(Chain);
1524 Ops.push_back(getValueType(EVT));
1525 std::vector<MVT::ValueType> VTs;
1527 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1528 return getNode(Opcode, VTs, Ops);
1531 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
1532 assert((!V || isa<PointerType>(V->getType())) &&
1533 "SrcValue is not a pointer?");
1534 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1535 if (N) return SDOperand(N, 0);
1537 N = new SrcValueSDNode(V, Offset);
1538 AllNodes.push_back(N);
1539 return SDOperand(N, 0);
1542 SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1543 SDOperand Chain, SDOperand Ptr,
1545 std::vector<SDOperand> Ops;
1547 Ops.push_back(Chain);
1550 std::vector<MVT::ValueType> VTs;
1552 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1553 return getNode(ISD::VAARG, VTs, Ops);
1556 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1557 std::vector<SDOperand> &Ops) {
1558 switch (Ops.size()) {
1559 case 0: return getNode(Opcode, VT);
1560 case 1: return getNode(Opcode, VT, Ops[0]);
1561 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1562 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
1566 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
1569 case ISD::TRUNCSTORE: {
1570 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1571 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1572 #if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1573 // If this is a truncating store of a constant, convert to the desired type
1574 // and store it instead.
1575 if (isa<Constant>(Ops[0])) {
1576 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1577 if (isa<Constant>(Op))
1580 // Also for ConstantFP?
1582 if (Ops[0].getValueType() == EVT) // Normal store?
1583 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1584 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1585 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1586 "Can't do FP-INT conversion!");
1589 case ISD::SELECT_CC: {
1590 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
1591 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1592 "LHS and RHS of condition must have same type!");
1593 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1594 "True and False arms of SelectCC must have same type!");
1595 assert(Ops[2].getValueType() == VT &&
1596 "select_cc node must be of same type as true and false value!");
1600 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
1601 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1602 "LHS/RHS of comparison should match types!");
1609 if (VT != MVT::Flag) {
1611 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1612 if (E) return SDOperand(E, 0);
1613 E = N = new SDNode(Opcode, Ops);
1615 N = new SDNode(Opcode, Ops);
1617 N->setValueTypes(VT);
1618 AllNodes.push_back(N);
1619 return SDOperand(N, 0);
1622 SDOperand SelectionDAG::getNode(unsigned Opcode,
1623 std::vector<MVT::ValueType> &ResultTys,
1624 std::vector<SDOperand> &Ops) {
1625 if (ResultTys.size() == 1)
1626 return getNode(Opcode, ResultTys[0], Ops);
1631 case ISD::ZEXTLOAD: {
1632 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1633 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1634 // If they are asking for an extending load from/to the same thing, return a
1636 if (ResultTys[0] == EVT)
1637 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1638 if (MVT::isVector(ResultTys[0])) {
1639 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1640 "Invalid vector extload!");
1642 assert(EVT < ResultTys[0] &&
1643 "Should only be an extending load, not truncating!");
1645 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1646 "Cannot sign/zero extend a FP/Vector load!");
1647 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1648 "Cannot convert from FP to Int or Int -> FP!");
1652 // FIXME: figure out how to safely handle things like
1653 // int foo(int x) { return 1 << (x & 255); }
1654 // int bar() { return foo(256); }
1656 case ISD::SRA_PARTS:
1657 case ISD::SRL_PARTS:
1658 case ISD::SHL_PARTS:
1659 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1660 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
1661 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1662 else if (N3.getOpcode() == ISD::AND)
1663 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1664 // If the and is only masking out bits that cannot effect the shift,
1665 // eliminate the and.
1666 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1667 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1668 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1674 // Memoize the node unless it returns a flag.
1676 if (ResultTys.back() != MVT::Flag) {
1678 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1679 if (E) return SDOperand(E, 0);
1680 E = N = new SDNode(Opcode, Ops);
1682 N = new SDNode(Opcode, Ops);
1684 setNodeValueTypes(N, ResultTys);
1685 AllNodes.push_back(N);
1686 return SDOperand(N, 0);
1689 void SelectionDAG::setNodeValueTypes(SDNode *N,
1690 std::vector<MVT::ValueType> &RetVals) {
1691 switch (RetVals.size()) {
1693 case 1: N->setValueTypes(RetVals[0]); return;
1694 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1698 std::list<std::vector<MVT::ValueType> >::iterator I =
1699 std::find(VTList.begin(), VTList.end(), RetVals);
1700 if (I == VTList.end()) {
1701 VTList.push_front(RetVals);
1705 N->setValueTypes(&(*I)[0], I->size());
1708 void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1709 MVT::ValueType VT2) {
1710 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1711 E = VTList.end(); I != E; ++I) {
1712 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1713 N->setValueTypes(&(*I)[0], 2);
1717 std::vector<MVT::ValueType> V;
1720 VTList.push_front(V);
1721 N->setValueTypes(&(*VTList.begin())[0], 2);
1724 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1725 /// specified operands. If the resultant node already exists in the DAG,
1726 /// this does not modify the specified node, instead it returns the node that
1727 /// already exists. If the resultant node does not exist in the DAG, the
1728 /// input node is returned. As a degenerate case, if you specify the same
1729 /// input operands as the node already has, the input node is returned.
1730 SDOperand SelectionDAG::
1731 UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1732 SDNode *N = InN.Val;
1733 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1735 // Check to see if there is no change.
1736 if (Op == N->getOperand(0)) return InN;
1738 // See if the modified node already exists.
1739 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1740 if (NewSlot && *NewSlot)
1741 return SDOperand(*NewSlot, InN.ResNo);
1743 // Nope it doesn't. Remove the node from it's current place in the maps.
1745 RemoveNodeFromCSEMaps(N);
1747 // Now we update the operands.
1748 N->OperandList[0].Val->removeUser(N);
1750 N->OperandList[0] = Op;
1752 // If this gets put into a CSE map, add it.
1753 if (NewSlot) *NewSlot = N;
1757 SDOperand SelectionDAG::
1758 UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1759 SDNode *N = InN.Val;
1760 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1762 // Check to see if there is no change.
1763 bool AnyChange = false;
1764 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1765 return InN; // No operands changed, just return the input node.
1767 // See if the modified node already exists.
1768 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1769 if (NewSlot && *NewSlot)
1770 return SDOperand(*NewSlot, InN.ResNo);
1772 // Nope it doesn't. Remove the node from it's current place in the maps.
1774 RemoveNodeFromCSEMaps(N);
1776 // Now we update the operands.
1777 if (N->OperandList[0] != Op1) {
1778 N->OperandList[0].Val->removeUser(N);
1779 Op1.Val->addUser(N);
1780 N->OperandList[0] = Op1;
1782 if (N->OperandList[1] != Op2) {
1783 N->OperandList[1].Val->removeUser(N);
1784 Op2.Val->addUser(N);
1785 N->OperandList[1] = Op2;
1788 // If this gets put into a CSE map, add it.
1789 if (NewSlot) *NewSlot = N;
1793 SDOperand SelectionDAG::
1794 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1795 std::vector<SDOperand> Ops;
1799 return UpdateNodeOperands(N, Ops);
1802 SDOperand SelectionDAG::
1803 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1804 SDOperand Op3, SDOperand Op4) {
1805 std::vector<SDOperand> Ops;
1810 return UpdateNodeOperands(N, Ops);
1813 SDOperand SelectionDAG::
1814 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1815 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1816 std::vector<SDOperand> Ops;
1822 return UpdateNodeOperands(N, Ops);
1826 SDOperand SelectionDAG::
1827 UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1828 SDNode *N = InN.Val;
1829 assert(N->getNumOperands() == Ops.size() &&
1830 "Update with wrong number of operands");
1832 // Check to see if there is no change.
1833 unsigned NumOps = Ops.size();
1834 bool AnyChange = false;
1835 for (unsigned i = 0; i != NumOps; ++i) {
1836 if (Ops[i] != N->getOperand(i)) {
1842 // No operands changed, just return the input node.
1843 if (!AnyChange) return InN;
1845 // See if the modified node already exists.
1846 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1847 if (NewSlot && *NewSlot)
1848 return SDOperand(*NewSlot, InN.ResNo);
1850 // Nope it doesn't. Remove the node from it's current place in the maps.
1852 RemoveNodeFromCSEMaps(N);
1854 // Now we update the operands.
1855 for (unsigned i = 0; i != NumOps; ++i) {
1856 if (N->OperandList[i] != Ops[i]) {
1857 N->OperandList[i].Val->removeUser(N);
1858 Ops[i].Val->addUser(N);
1859 N->OperandList[i] = Ops[i];
1863 // If this gets put into a CSE map, add it.
1864 if (NewSlot) *NewSlot = N;
1871 /// SelectNodeTo - These are used for target selectors to *mutate* the
1872 /// specified node to have the specified return type, Target opcode, and
1873 /// operands. Note that target opcodes are stored as
1874 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1876 /// Note that SelectNodeTo returns the resultant node. If there is already a
1877 /// node of the specified opcode and operands, it returns that node instead of
1878 /// the current one.
1879 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1880 MVT::ValueType VT) {
1881 // If an identical node already exists, use it.
1882 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1883 if (ON) return SDOperand(ON, 0);
1885 RemoveNodeFromCSEMaps(N);
1887 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1888 N->setValueTypes(VT);
1890 ON = N; // Memoize the new node.
1891 return SDOperand(N, 0);
1894 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1895 MVT::ValueType VT, SDOperand Op1) {
1896 // If an identical node already exists, use it.
1897 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1898 std::make_pair(Op1, VT))];
1899 if (ON) return SDOperand(ON, 0);
1901 RemoveNodeFromCSEMaps(N);
1902 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1903 N->setValueTypes(VT);
1904 N->setOperands(Op1);
1906 ON = N; // Memoize the new node.
1907 return SDOperand(N, 0);
1910 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1911 MVT::ValueType VT, SDOperand Op1,
1913 // If an identical node already exists, use it.
1914 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1915 std::make_pair(Op1, Op2))];
1916 if (ON) return SDOperand(ON, 0);
1918 RemoveNodeFromCSEMaps(N);
1919 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1920 N->setValueTypes(VT);
1921 N->setOperands(Op1, Op2);
1923 ON = N; // Memoize the new node.
1924 return SDOperand(N, 0);
1927 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1928 MVT::ValueType VT, SDOperand Op1,
1929 SDOperand Op2, SDOperand Op3) {
1930 // If an identical node already exists, use it.
1931 std::vector<SDOperand> OpList;
1932 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1933 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1934 std::make_pair(VT, OpList))];
1935 if (ON) return SDOperand(ON, 0);
1937 RemoveNodeFromCSEMaps(N);
1938 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1939 N->setValueTypes(VT);
1940 N->setOperands(Op1, Op2, Op3);
1942 ON = N; // Memoize the new node.
1943 return SDOperand(N, 0);
1946 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1947 MVT::ValueType VT, SDOperand Op1,
1948 SDOperand Op2, SDOperand Op3,
1950 // If an identical node already exists, use it.
1951 std::vector<SDOperand> OpList;
1952 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1953 OpList.push_back(Op4);
1954 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1955 std::make_pair(VT, OpList))];
1956 if (ON) return SDOperand(ON, 0);
1958 RemoveNodeFromCSEMaps(N);
1959 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1960 N->setValueTypes(VT);
1961 N->setOperands(Op1, Op2, Op3, Op4);
1963 ON = N; // Memoize the new node.
1964 return SDOperand(N, 0);
1967 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1968 MVT::ValueType VT, SDOperand Op1,
1969 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1971 // If an identical node already exists, use it.
1972 std::vector<SDOperand> OpList;
1973 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1974 OpList.push_back(Op4); OpList.push_back(Op5);
1975 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1976 std::make_pair(VT, OpList))];
1977 if (ON) return SDOperand(ON, 0);
1979 RemoveNodeFromCSEMaps(N);
1980 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1981 N->setValueTypes(VT);
1982 N->setOperands(Op1, Op2, Op3, Op4, Op5);
1984 ON = N; // Memoize the new node.
1985 return SDOperand(N, 0);
1988 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1989 MVT::ValueType VT, SDOperand Op1,
1990 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1991 SDOperand Op5, SDOperand Op6) {
1992 // If an identical node already exists, use it.
1993 std::vector<SDOperand> OpList;
1994 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1995 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1996 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1997 std::make_pair(VT, OpList))];
1998 if (ON) return SDOperand(ON, 0);
2000 RemoveNodeFromCSEMaps(N);
2001 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2002 N->setValueTypes(VT);
2003 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
2005 ON = N; // Memoize the new node.
2006 return SDOperand(N, 0);
2009 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2010 MVT::ValueType VT, SDOperand Op1,
2011 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2012 SDOperand Op5, SDOperand Op6,
2014 // If an identical node already exists, use it.
2015 std::vector<SDOperand> OpList;
2016 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2017 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2018 OpList.push_back(Op7);
2019 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2020 std::make_pair(VT, OpList))];
2021 if (ON) return SDOperand(ON, 0);
2023 RemoveNodeFromCSEMaps(N);
2024 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2025 N->setValueTypes(VT);
2026 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
2028 ON = N; // Memoize the new node.
2029 return SDOperand(N, 0);
2031 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2032 MVT::ValueType VT, SDOperand Op1,
2033 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2034 SDOperand Op5, SDOperand Op6,
2035 SDOperand Op7, SDOperand Op8) {
2036 // If an identical node already exists, use it.
2037 std::vector<SDOperand> OpList;
2038 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2039 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2040 OpList.push_back(Op7); OpList.push_back(Op8);
2041 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2042 std::make_pair(VT, OpList))];
2043 if (ON) return SDOperand(ON, 0);
2045 RemoveNodeFromCSEMaps(N);
2046 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2047 N->setValueTypes(VT);
2048 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2050 ON = N; // Memoize the new node.
2051 return SDOperand(N, 0);
2054 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2055 MVT::ValueType VT1, MVT::ValueType VT2,
2056 SDOperand Op1, SDOperand Op2) {
2057 // If an identical node already exists, use it.
2058 std::vector<SDOperand> OpList;
2059 OpList.push_back(Op1); OpList.push_back(Op2);
2060 std::vector<MVT::ValueType> VTList;
2061 VTList.push_back(VT1); VTList.push_back(VT2);
2062 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2063 std::make_pair(VTList, OpList))];
2064 if (ON) return SDOperand(ON, 0);
2066 RemoveNodeFromCSEMaps(N);
2067 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2068 setNodeValueTypes(N, VT1, VT2);
2069 N->setOperands(Op1, Op2);
2071 ON = N; // Memoize the new node.
2072 return SDOperand(N, 0);
2075 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2076 MVT::ValueType VT1, MVT::ValueType VT2,
2077 SDOperand Op1, SDOperand Op2,
2079 // If an identical node already exists, use it.
2080 std::vector<SDOperand> OpList;
2081 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2082 std::vector<MVT::ValueType> VTList;
2083 VTList.push_back(VT1); VTList.push_back(VT2);
2084 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2085 std::make_pair(VTList, OpList))];
2086 if (ON) return SDOperand(ON, 0);
2088 RemoveNodeFromCSEMaps(N);
2089 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2090 setNodeValueTypes(N, VT1, VT2);
2091 N->setOperands(Op1, Op2, Op3);
2093 ON = N; // Memoize the new node.
2094 return SDOperand(N, 0);
2097 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2098 MVT::ValueType VT1, MVT::ValueType VT2,
2099 SDOperand Op1, SDOperand Op2,
2100 SDOperand Op3, SDOperand Op4) {
2101 // If an identical node already exists, use it.
2102 std::vector<SDOperand> OpList;
2103 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2104 OpList.push_back(Op4);
2105 std::vector<MVT::ValueType> VTList;
2106 VTList.push_back(VT1); VTList.push_back(VT2);
2107 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2108 std::make_pair(VTList, OpList))];
2109 if (ON) return SDOperand(ON, 0);
2111 RemoveNodeFromCSEMaps(N);
2112 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2113 setNodeValueTypes(N, VT1, VT2);
2114 N->setOperands(Op1, Op2, Op3, Op4);
2116 ON = N; // Memoize the new node.
2117 return SDOperand(N, 0);
2120 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2121 MVT::ValueType VT1, MVT::ValueType VT2,
2122 SDOperand Op1, SDOperand Op2,
2123 SDOperand Op3, SDOperand Op4,
2125 // If an identical node already exists, use it.
2126 std::vector<SDOperand> OpList;
2127 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2128 OpList.push_back(Op4); OpList.push_back(Op5);
2129 std::vector<MVT::ValueType> VTList;
2130 VTList.push_back(VT1); VTList.push_back(VT2);
2131 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2132 std::make_pair(VTList, OpList))];
2133 if (ON) return SDOperand(ON, 0);
2135 RemoveNodeFromCSEMaps(N);
2136 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2137 setNodeValueTypes(N, VT1, VT2);
2138 N->setOperands(Op1, Op2, Op3, Op4, Op5);
2140 ON = N; // Memoize the new node.
2141 return SDOperand(N, 0);
2144 /// getTargetNode - These are used for target selectors to create a new node
2145 /// with specified return type(s), target opcode, and operands.
2147 /// Note that getTargetNode returns the resultant node. If there is already a
2148 /// node of the specified opcode and operands, it returns that node instead of
2149 /// the current one.
2150 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2151 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2153 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2155 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2157 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2158 SDOperand Op1, SDOperand Op2) {
2159 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2161 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2162 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2163 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2165 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2166 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2168 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2170 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2171 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2172 SDOperand Op4, SDOperand Op5) {
2173 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2175 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2176 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2177 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
2178 std::vector<SDOperand> Ops;
2186 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2188 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2189 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2190 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2192 std::vector<SDOperand> Ops;
2201 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2203 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2204 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2205 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2206 SDOperand Op7, SDOperand Op8) {
2207 std::vector<SDOperand> Ops;
2217 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2219 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2220 std::vector<SDOperand> &Ops) {
2221 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2223 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2224 MVT::ValueType VT2, SDOperand Op1) {
2225 std::vector<MVT::ValueType> ResultTys;
2226 ResultTys.push_back(VT1);
2227 ResultTys.push_back(VT2);
2228 std::vector<SDOperand> Ops;
2230 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2232 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2233 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
2234 std::vector<MVT::ValueType> ResultTys;
2235 ResultTys.push_back(VT1);
2236 ResultTys.push_back(VT2);
2237 std::vector<SDOperand> Ops;
2240 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2242 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2243 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2245 std::vector<MVT::ValueType> ResultTys;
2246 ResultTys.push_back(VT1);
2247 ResultTys.push_back(VT2);
2248 std::vector<SDOperand> Ops;
2252 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2254 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2255 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2256 SDOperand Op3, SDOperand Op4) {
2257 std::vector<MVT::ValueType> ResultTys;
2258 ResultTys.push_back(VT1);
2259 ResultTys.push_back(VT2);
2260 std::vector<SDOperand> Ops;
2265 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2267 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2268 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2269 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2270 std::vector<MVT::ValueType> ResultTys;
2271 ResultTys.push_back(VT1);
2272 ResultTys.push_back(VT2);
2273 std::vector<SDOperand> Ops;
2279 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2281 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2282 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2283 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2285 std::vector<MVT::ValueType> ResultTys;
2286 ResultTys.push_back(VT1);
2287 ResultTys.push_back(VT2);
2288 std::vector<SDOperand> Ops;
2295 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2297 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2298 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2299 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2300 SDOperand Op6, SDOperand Op7) {
2301 std::vector<MVT::ValueType> ResultTys;
2302 ResultTys.push_back(VT1);
2303 ResultTys.push_back(VT2);
2304 std::vector<SDOperand> Ops;
2312 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2314 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2315 MVT::ValueType VT2, MVT::ValueType VT3,
2316 SDOperand Op1, SDOperand Op2) {
2317 std::vector<MVT::ValueType> ResultTys;
2318 ResultTys.push_back(VT1);
2319 ResultTys.push_back(VT2);
2320 ResultTys.push_back(VT3);
2321 std::vector<SDOperand> Ops;
2324 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2326 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2327 MVT::ValueType VT2, MVT::ValueType VT3,
2328 SDOperand Op1, SDOperand Op2,
2329 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2330 std::vector<MVT::ValueType> ResultTys;
2331 ResultTys.push_back(VT1);
2332 ResultTys.push_back(VT2);
2333 ResultTys.push_back(VT3);
2334 std::vector<SDOperand> Ops;
2340 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2342 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2343 MVT::ValueType VT2, MVT::ValueType VT3,
2344 SDOperand Op1, SDOperand Op2,
2345 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2347 std::vector<MVT::ValueType> ResultTys;
2348 ResultTys.push_back(VT1);
2349 ResultTys.push_back(VT2);
2350 ResultTys.push_back(VT3);
2351 std::vector<SDOperand> Ops;
2358 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2360 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2361 MVT::ValueType VT2, MVT::ValueType VT3,
2362 SDOperand Op1, SDOperand Op2,
2363 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2364 SDOperand Op6, SDOperand Op7) {
2365 std::vector<MVT::ValueType> ResultTys;
2366 ResultTys.push_back(VT1);
2367 ResultTys.push_back(VT2);
2368 ResultTys.push_back(VT3);
2369 std::vector<SDOperand> Ops;
2377 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2379 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2380 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
2381 std::vector<MVT::ValueType> ResultTys;
2382 ResultTys.push_back(VT1);
2383 ResultTys.push_back(VT2);
2384 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2387 // ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2388 /// This can cause recursive merging of nodes in the DAG.
2390 /// This version assumes From/To have a single result value.
2392 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2393 std::vector<SDNode*> *Deleted) {
2394 SDNode *From = FromN.Val, *To = ToN.Val;
2395 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2396 "Cannot replace with this method!");
2397 assert(From != To && "Cannot replace uses of with self");
2399 while (!From->use_empty()) {
2400 // Process users until they are all gone.
2401 SDNode *U = *From->use_begin();
2403 // This node is about to morph, remove its old self from the CSE maps.
2404 RemoveNodeFromCSEMaps(U);
2406 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2408 if (I->Val == From) {
2409 From->removeUser(U);
2414 // Now that we have modified U, add it back to the CSE maps. If it already
2415 // exists there, recursively merge the results together.
2416 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2417 ReplaceAllUsesWith(U, Existing, Deleted);
2419 if (Deleted) Deleted->push_back(U);
2420 DeleteNodeNotInCSEMaps(U);
2425 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2426 /// This can cause recursive merging of nodes in the DAG.
2428 /// This version assumes From/To have matching types and numbers of result
2431 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2432 std::vector<SDNode*> *Deleted) {
2433 assert(From != To && "Cannot replace uses of with self");
2434 assert(From->getNumValues() == To->getNumValues() &&
2435 "Cannot use this version of ReplaceAllUsesWith!");
2436 if (From->getNumValues() == 1) { // If possible, use the faster version.
2437 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
2441 while (!From->use_empty()) {
2442 // Process users until they are all gone.
2443 SDNode *U = *From->use_begin();
2445 // This node is about to morph, remove its old self from the CSE maps.
2446 RemoveNodeFromCSEMaps(U);
2448 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2450 if (I->Val == From) {
2451 From->removeUser(U);
2456 // Now that we have modified U, add it back to the CSE maps. If it already
2457 // exists there, recursively merge the results together.
2458 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2459 ReplaceAllUsesWith(U, Existing, Deleted);
2461 if (Deleted) Deleted->push_back(U);
2462 DeleteNodeNotInCSEMaps(U);
2467 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2468 /// This can cause recursive merging of nodes in the DAG.
2470 /// This version can replace From with any result values. To must match the
2471 /// number and types of values returned by From.
2472 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
2473 const std::vector<SDOperand> &To,
2474 std::vector<SDNode*> *Deleted) {
2475 assert(From->getNumValues() == To.size() &&
2476 "Incorrect number of values to replace with!");
2477 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
2478 // Degenerate case handled above.
2479 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
2483 while (!From->use_empty()) {
2484 // Process users until they are all gone.
2485 SDNode *U = *From->use_begin();
2487 // This node is about to morph, remove its old self from the CSE maps.
2488 RemoveNodeFromCSEMaps(U);
2490 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2492 if (I->Val == From) {
2493 const SDOperand &ToOp = To[I->ResNo];
2494 From->removeUser(U);
2496 ToOp.Val->addUser(U);
2499 // Now that we have modified U, add it back to the CSE maps. If it already
2500 // exists there, recursively merge the results together.
2501 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2502 ReplaceAllUsesWith(U, Existing, Deleted);
2504 if (Deleted) Deleted->push_back(U);
2505 DeleteNodeNotInCSEMaps(U);
2510 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2511 /// uses of other values produced by From.Val alone. The Deleted vector is
2512 /// handled the same was as for ReplaceAllUsesWith.
2513 void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2514 std::vector<SDNode*> &Deleted) {
2515 assert(From != To && "Cannot replace a value with itself");
2516 // Handle the simple, trivial, case efficiently.
2517 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2518 ReplaceAllUsesWith(From, To, &Deleted);
2522 // Get all of the users in a nice, deterministically ordered, uniqued set.
2523 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2525 while (!Users.empty()) {
2526 // We know that this user uses some value of From. If it is the right
2527 // value, update it.
2528 SDNode *User = Users.back();
2531 for (SDOperand *Op = User->OperandList,
2532 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2534 // Okay, we know this user needs to be updated. Remove its old self
2535 // from the CSE maps.
2536 RemoveNodeFromCSEMaps(User);
2538 // Update all operands that match "From".
2539 for (; Op != E; ++Op) {
2541 From.Val->removeUser(User);
2543 To.Val->addUser(User);
2547 // Now that we have modified User, add it back to the CSE maps. If it
2548 // already exists there, recursively merge the results together.
2549 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2550 unsigned NumDeleted = Deleted.size();
2551 ReplaceAllUsesWith(User, Existing, &Deleted);
2553 // User is now dead.
2554 Deleted.push_back(User);
2555 DeleteNodeNotInCSEMaps(User);
2557 // We have to be careful here, because ReplaceAllUsesWith could have
2558 // deleted a user of From, which means there may be dangling pointers
2559 // in the "Users" setvector. Scan over the deleted node pointers and
2560 // remove them from the setvector.
2561 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2562 Users.remove(Deleted[i]);
2564 break; // Exit the operand scanning loop.
2571 //===----------------------------------------------------------------------===//
2573 //===----------------------------------------------------------------------===//
2576 /// getValueTypeList - Return a pointer to the specified value type.
2578 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2579 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2584 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2585 /// indicated value. This method ignores uses of other values defined by this
2587 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
2588 assert(Value < getNumValues() && "Bad value!");
2590 // If there is only one value, this is easy.
2591 if (getNumValues() == 1)
2592 return use_size() == NUses;
2593 if (Uses.size() < NUses) return false;
2595 SDOperand TheValue(const_cast<SDNode *>(this), Value);
2597 std::set<SDNode*> UsersHandled;
2599 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
2602 if (User->getNumOperands() == 1 ||
2603 UsersHandled.insert(User).second) // First time we've seen this?
2604 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2605 if (User->getOperand(i) == TheValue) {
2607 return false; // too many uses
2612 // Found exactly the right number of uses?
2617 // isOnlyUse - Return true if this node is the only use of N.
2618 bool SDNode::isOnlyUse(SDNode *N) const {
2620 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2631 // isOperand - Return true if this node is an operand of N.
2632 bool SDOperand::isOperand(SDNode *N) const {
2633 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2634 if (*this == N->getOperand(i))
2639 bool SDNode::isOperand(SDNode *N) const {
2640 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2641 if (this == N->OperandList[i].Val)
2646 const char *SDNode::getOperationName(const SelectionDAG *G) const {
2647 switch (getOpcode()) {
2649 if (getOpcode() < ISD::BUILTIN_OP_END)
2650 return "<<Unknown DAG Node>>";
2653 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
2654 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2655 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
2657 TargetLowering &TLI = G->getTargetLoweringInfo();
2659 TLI.getTargetNodeName(getOpcode());
2660 if (Name) return Name;
2663 return "<<Unknown Target Node>>";
2666 case ISD::PCMARKER: return "PCMarker";
2667 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
2668 case ISD::SRCVALUE: return "SrcValue";
2669 case ISD::EntryToken: return "EntryToken";
2670 case ISD::TokenFactor: return "TokenFactor";
2671 case ISD::AssertSext: return "AssertSext";
2672 case ISD::AssertZext: return "AssertZext";
2674 case ISD::STRING: return "String";
2675 case ISD::BasicBlock: return "BasicBlock";
2676 case ISD::VALUETYPE: return "ValueType";
2677 case ISD::Register: return "Register";
2679 case ISD::Constant: return "Constant";
2680 case ISD::ConstantFP: return "ConstantFP";
2681 case ISD::GlobalAddress: return "GlobalAddress";
2682 case ISD::FrameIndex: return "FrameIndex";
2683 case ISD::ConstantPool: return "ConstantPool";
2684 case ISD::ExternalSymbol: return "ExternalSymbol";
2685 case ISD::INTRINSIC:
2686 bool hasChain = getOperand(0).getValueType() == MVT::Other;
2687 unsigned IID = cast<ConstantSDNode>(getOperand(hasChain))->getValue();
2688 return Intrinsic::getName((Intrinsic::ID)IID);
2690 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
2691 case ISD::TargetConstant: return "TargetConstant";
2692 case ISD::TargetConstantFP:return "TargetConstantFP";
2693 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2694 case ISD::TargetFrameIndex: return "TargetFrameIndex";
2695 case ISD::TargetConstantPool: return "TargetConstantPool";
2696 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
2698 case ISD::CopyToReg: return "CopyToReg";
2699 case ISD::CopyFromReg: return "CopyFromReg";
2700 case ISD::UNDEF: return "undef";
2701 case ISD::MERGE_VALUES: return "mergevalues";
2702 case ISD::INLINEASM: return "inlineasm";
2703 case ISD::HANDLENODE: return "handlenode";
2706 case ISD::FABS: return "fabs";
2707 case ISD::FNEG: return "fneg";
2708 case ISD::FSQRT: return "fsqrt";
2709 case ISD::FSIN: return "fsin";
2710 case ISD::FCOS: return "fcos";
2713 case ISD::ADD: return "add";
2714 case ISD::SUB: return "sub";
2715 case ISD::MUL: return "mul";
2716 case ISD::MULHU: return "mulhu";
2717 case ISD::MULHS: return "mulhs";
2718 case ISD::SDIV: return "sdiv";
2719 case ISD::UDIV: return "udiv";
2720 case ISD::SREM: return "srem";
2721 case ISD::UREM: return "urem";
2722 case ISD::AND: return "and";
2723 case ISD::OR: return "or";
2724 case ISD::XOR: return "xor";
2725 case ISD::SHL: return "shl";
2726 case ISD::SRA: return "sra";
2727 case ISD::SRL: return "srl";
2728 case ISD::ROTL: return "rotl";
2729 case ISD::ROTR: return "rotr";
2730 case ISD::FADD: return "fadd";
2731 case ISD::FSUB: return "fsub";
2732 case ISD::FMUL: return "fmul";
2733 case ISD::FDIV: return "fdiv";
2734 case ISD::FREM: return "frem";
2735 case ISD::FCOPYSIGN: return "fcopysign";
2736 case ISD::VADD: return "vadd";
2737 case ISD::VSUB: return "vsub";
2738 case ISD::VMUL: return "vmul";
2740 case ISD::SETCC: return "setcc";
2741 case ISD::SELECT: return "select";
2742 case ISD::SELECT_CC: return "select_cc";
2743 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2744 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2745 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
2746 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
2747 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2748 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2749 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2750 case ISD::VBIT_CONVERT: return "vbit_convert";
2751 case ISD::ADDC: return "addc";
2752 case ISD::ADDE: return "adde";
2753 case ISD::SUBC: return "subc";
2754 case ISD::SUBE: return "sube";
2755 case ISD::SHL_PARTS: return "shl_parts";
2756 case ISD::SRA_PARTS: return "sra_parts";
2757 case ISD::SRL_PARTS: return "srl_parts";
2759 // Conversion operators.
2760 case ISD::SIGN_EXTEND: return "sign_extend";
2761 case ISD::ZERO_EXTEND: return "zero_extend";
2762 case ISD::ANY_EXTEND: return "any_extend";
2763 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
2764 case ISD::TRUNCATE: return "truncate";
2765 case ISD::FP_ROUND: return "fp_round";
2766 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
2767 case ISD::FP_EXTEND: return "fp_extend";
2769 case ISD::SINT_TO_FP: return "sint_to_fp";
2770 case ISD::UINT_TO_FP: return "uint_to_fp";
2771 case ISD::FP_TO_SINT: return "fp_to_sint";
2772 case ISD::FP_TO_UINT: return "fp_to_uint";
2773 case ISD::BIT_CONVERT: return "bit_convert";
2775 // Control flow instructions
2776 case ISD::BR: return "br";
2777 case ISD::BRCOND: return "brcond";
2778 case ISD::BR_CC: return "br_cc";
2779 case ISD::RET: return "ret";
2780 case ISD::CALLSEQ_START: return "callseq_start";
2781 case ISD::CALLSEQ_END: return "callseq_end";
2784 case ISD::LOAD: return "load";
2785 case ISD::STORE: return "store";
2786 case ISD::VLOAD: return "vload";
2787 case ISD::EXTLOAD: return "extload";
2788 case ISD::SEXTLOAD: return "sextload";
2789 case ISD::ZEXTLOAD: return "zextload";
2790 case ISD::TRUNCSTORE: return "truncstore";
2791 case ISD::VAARG: return "vaarg";
2792 case ISD::VACOPY: return "vacopy";
2793 case ISD::VAEND: return "vaend";
2794 case ISD::VASTART: return "vastart";
2795 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2796 case ISD::EXTRACT_ELEMENT: return "extract_element";
2797 case ISD::BUILD_PAIR: return "build_pair";
2798 case ISD::STACKSAVE: return "stacksave";
2799 case ISD::STACKRESTORE: return "stackrestore";
2801 // Block memory operations.
2802 case ISD::MEMSET: return "memset";
2803 case ISD::MEMCPY: return "memcpy";
2804 case ISD::MEMMOVE: return "memmove";
2807 case ISD::BSWAP: return "bswap";
2808 case ISD::CTPOP: return "ctpop";
2809 case ISD::CTTZ: return "cttz";
2810 case ISD::CTLZ: return "ctlz";
2813 case ISD::LOCATION: return "location";
2814 case ISD::DEBUG_LOC: return "debug_loc";
2815 case ISD::DEBUG_LABEL: return "debug_label";
2818 switch (cast<CondCodeSDNode>(this)->get()) {
2819 default: assert(0 && "Unknown setcc condition!");
2820 case ISD::SETOEQ: return "setoeq";
2821 case ISD::SETOGT: return "setogt";
2822 case ISD::SETOGE: return "setoge";
2823 case ISD::SETOLT: return "setolt";
2824 case ISD::SETOLE: return "setole";
2825 case ISD::SETONE: return "setone";
2827 case ISD::SETO: return "seto";
2828 case ISD::SETUO: return "setuo";
2829 case ISD::SETUEQ: return "setue";
2830 case ISD::SETUGT: return "setugt";
2831 case ISD::SETUGE: return "setuge";
2832 case ISD::SETULT: return "setult";
2833 case ISD::SETULE: return "setule";
2834 case ISD::SETUNE: return "setune";
2836 case ISD::SETEQ: return "seteq";
2837 case ISD::SETGT: return "setgt";
2838 case ISD::SETGE: return "setge";
2839 case ISD::SETLT: return "setlt";
2840 case ISD::SETLE: return "setle";
2841 case ISD::SETNE: return "setne";
2846 void SDNode::dump() const { dump(0); }
2847 void SDNode::dump(const SelectionDAG *G) const {
2848 std::cerr << (void*)this << ": ";
2850 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2851 if (i) std::cerr << ",";
2852 if (getValueType(i) == MVT::Other)
2855 std::cerr << MVT::getValueTypeString(getValueType(i));
2857 std::cerr << " = " << getOperationName(G);
2860 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2861 if (i) std::cerr << ", ";
2862 std::cerr << (void*)getOperand(i).Val;
2863 if (unsigned RN = getOperand(i).ResNo)
2864 std::cerr << ":" << RN;
2867 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2868 std::cerr << "<" << CSDN->getValue() << ">";
2869 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2870 std::cerr << "<" << CSDN->getValue() << ">";
2871 } else if (const GlobalAddressSDNode *GADN =
2872 dyn_cast<GlobalAddressSDNode>(this)) {
2873 int offset = GADN->getOffset();
2875 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
2877 std::cerr << " + " << offset;
2879 std::cerr << " " << offset;
2880 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
2881 std::cerr << "<" << FIDN->getIndex() << ">";
2882 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
2883 int offset = CP->getOffset();
2884 std::cerr << "<" << *CP->get() << ">";
2886 std::cerr << " + " << offset;
2888 std::cerr << " " << offset;
2889 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
2891 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2893 std::cerr << LBB->getName() << " ";
2894 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
2895 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
2896 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2897 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2899 std::cerr << " #" << R->getReg();
2901 } else if (const ExternalSymbolSDNode *ES =
2902 dyn_cast<ExternalSymbolSDNode>(this)) {
2903 std::cerr << "'" << ES->getSymbol() << "'";
2904 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2906 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2908 std::cerr << "<null:" << M->getOffset() << ">";
2909 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2910 std::cerr << ":" << getValueTypeString(N->getVT());
2914 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
2915 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2916 if (N->getOperand(i).Val->hasOneUse())
2917 DumpNodes(N->getOperand(i).Val, indent+2, G);
2919 std::cerr << "\n" << std::string(indent+2, ' ')
2920 << (void*)N->getOperand(i).Val << ": <multiple use>";
2923 std::cerr << "\n" << std::string(indent, ' ');
2927 void SelectionDAG::dump() const {
2928 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
2929 std::vector<const SDNode*> Nodes;
2930 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2934 std::sort(Nodes.begin(), Nodes.end());
2936 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2937 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
2938 DumpNodes(Nodes[i], 2, this);
2941 DumpNodes(getRoot().Val, 2, this);
2943 std::cerr << "\n\n";
2946 /// InsertISelMapEntry - A helper function to insert a key / element pair
2947 /// into a SDOperand to SDOperand map. This is added to avoid the map
2948 /// insertion operator from being inlined.
2949 void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
2950 SDNode *Key, unsigned KeyResNo,
2951 SDNode *Element, unsigned ElementResNo) {
2952 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
2953 SDOperand(Element, ElementResNo)));