1 //===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements the SelectionDAG class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/SelectionDAG.h"
15 #include "llvm/Constants.h"
16 #include "llvm/GlobalValue.h"
17 #include "llvm/Assembly/Writer.h"
18 #include "llvm/CodeGen/MachineBasicBlock.h"
19 #include "llvm/Support/MathExtras.h"
20 #include "llvm/Target/MRegisterInfo.h"
21 #include "llvm/Target/TargetLowering.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/ADT/SetVector.h"
25 #include "llvm/ADT/StringExtras.h"
32 static bool isCommutativeBinOp(unsigned Opcode) {
42 case ISD::XOR: return true;
43 default: return false; // FIXME: Need commutative info for user ops!
47 // isInvertibleForFree - Return true if there is no cost to emitting the logical
48 // inverse of this node.
49 static bool isInvertibleForFree(SDOperand N) {
50 if (isa<ConstantSDNode>(N.Val)) return true;
51 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
56 //===----------------------------------------------------------------------===//
57 // ConstantFPSDNode Class
58 //===----------------------------------------------------------------------===//
60 /// isExactlyValue - We don't rely on operator== working on double values, as
61 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
62 /// As such, this method can be used to do an exact bit-for-bit comparison of
63 /// two floating point values.
64 bool ConstantFPSDNode::isExactlyValue(double V) const {
65 return DoubleToBits(V) == DoubleToBits(Value);
68 //===----------------------------------------------------------------------===//
70 //===----------------------------------------------------------------------===//
72 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
73 /// when given the operation for (X op Y).
74 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
75 // To perform this operation, we just need to swap the L and G bits of the
77 unsigned OldL = (Operation >> 2) & 1;
78 unsigned OldG = (Operation >> 1) & 1;
79 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
80 (OldL << 1) | // New G bit
81 (OldG << 2)); // New L bit.
84 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
85 /// 'op' is a valid SetCC operation.
86 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
87 unsigned Operation = Op;
89 Operation ^= 7; // Flip L, G, E bits, but not U.
91 Operation ^= 15; // Flip all of the condition bits.
92 if (Operation > ISD::SETTRUE2)
93 Operation &= ~8; // Don't let N and U bits get set.
94 return ISD::CondCode(Operation);
98 /// isSignedOp - For an integer comparison, return 1 if the comparison is a
99 /// signed operation and 2 if the result is an unsigned comparison. Return zero
100 /// if the operation does not depend on the sign of the input (setne and seteq).
101 static int isSignedOp(ISD::CondCode Opcode) {
103 default: assert(0 && "Illegal integer setcc operation!");
105 case ISD::SETNE: return 0;
109 case ISD::SETGE: return 1;
113 case ISD::SETUGE: return 2;
117 /// getSetCCOrOperation - Return the result of a logical OR between different
118 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
119 /// returns SETCC_INVALID if it is not possible to represent the resultant
121 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
123 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
124 // Cannot fold a signed integer setcc with an unsigned integer setcc.
125 return ISD::SETCC_INVALID;
127 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
129 // If the N and U bits get set then the resultant comparison DOES suddenly
130 // care about orderedness, and is true when ordered.
131 if (Op > ISD::SETTRUE2)
132 Op &= ~16; // Clear the N bit.
133 return ISD::CondCode(Op);
136 /// getSetCCAndOperation - Return the result of a logical AND between different
137 /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
138 /// function returns zero if it is not possible to represent the resultant
140 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
142 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
143 // Cannot fold a signed setcc with an unsigned setcc.
144 return ISD::SETCC_INVALID;
146 // Combine all of the condition bits.
147 return ISD::CondCode(Op1 & Op2);
150 const TargetMachine &SelectionDAG::getTarget() const {
151 return TLI.getTargetMachine();
154 //===----------------------------------------------------------------------===//
155 // SelectionDAG Class
156 //===----------------------------------------------------------------------===//
158 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
159 /// SelectionDAG, including nodes (like loads) that have uses of their token
160 /// chain but no other uses and no side effect. If a node is passed in as an
161 /// argument, it is used as the seed for node deletion.
162 void SelectionDAG::RemoveDeadNodes(SDNode *N) {
163 // Create a dummy node (which is not added to allnodes), that adds a reference
164 // to the root node, preventing it from being deleted.
165 HandleSDNode Dummy(getRoot());
167 bool MadeChange = false;
169 // If we have a hint to start from, use it.
170 if (N && N->use_empty()) {
175 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
176 if (I->use_empty() && I->getOpcode() != 65535) {
177 // Node is dead, recursively delete newly dead uses.
182 // Walk the nodes list, removing the nodes we've marked as dead.
184 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
191 // If the root changed (e.g. it was a dead load, update the root).
192 setRoot(Dummy.getValue());
195 /// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
196 /// graph. If it is the last user of any of its operands, recursively process
197 /// them the same way.
199 void SelectionDAG::DestroyDeadNode(SDNode *N) {
200 // Okay, we really are going to delete this node. First take this out of the
201 // appropriate CSE map.
202 RemoveNodeFromCSEMaps(N);
204 // Next, brutally remove the operand list. This is safe to do, as there are
205 // no cycles in the graph.
206 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
210 // Now that we removed this operand, see if there are no uses of it left.
214 delete[] N->OperandList;
218 // Mark the node as dead.
219 N->MorphNodeTo(65535);
222 void SelectionDAG::DeleteNode(SDNode *N) {
223 assert(N->use_empty() && "Cannot delete a node that is not dead!");
225 // First take this out of the appropriate CSE map.
226 RemoveNodeFromCSEMaps(N);
228 // Finally, remove uses due to operands of this node, remove from the
229 // AllNodes list, and delete the node.
230 DeleteNodeNotInCSEMaps(N);
233 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
235 // Remove it from the AllNodes list.
238 // Drop all of the operands and decrement used nodes use counts.
239 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
240 I->Val->removeUser(N);
241 delete[] N->OperandList;
248 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
249 /// correspond to it. This is useful when we're about to delete or repurpose
250 /// the node. We don't want future request for structurally identical nodes
251 /// to return N anymore.
252 void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
254 switch (N->getOpcode()) {
255 case ISD::HANDLENODE: return; // noop.
257 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
258 N->getValueType(0)));
260 case ISD::TargetConstant:
261 Erased = TargetConstants.erase(std::make_pair(
262 cast<ConstantSDNode>(N)->getValue(),
263 N->getValueType(0)));
265 case ISD::ConstantFP: {
266 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
267 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
270 case ISD::TargetConstantFP: {
271 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
272 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
276 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
279 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
280 "Cond code doesn't exist!");
281 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
282 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
284 case ISD::GlobalAddress: {
285 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
286 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
290 case ISD::TargetGlobalAddress: {
291 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
292 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
296 case ISD::FrameIndex:
297 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
299 case ISD::TargetFrameIndex:
300 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
302 case ISD::ConstantPool:
303 Erased = ConstantPoolIndices.
304 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
305 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
306 cast<ConstantPoolSDNode>(N)->getAlignment())));
308 case ISD::TargetConstantPool:
309 Erased = TargetConstantPoolIndices.
310 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
311 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
312 cast<ConstantPoolSDNode>(N)->getAlignment())));
314 case ISD::BasicBlock:
315 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
317 case ISD::ExternalSymbol:
318 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
320 case ISD::TargetExternalSymbol:
322 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
325 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
326 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
329 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
330 N->getValueType(0)));
332 case ISD::SRCVALUE: {
333 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
334 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
338 Erased = Loads.erase(std::make_pair(N->getOperand(1),
339 std::make_pair(N->getOperand(0),
340 N->getValueType(0))));
343 if (N->getNumValues() == 1) {
344 if (N->getNumOperands() == 0) {
345 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
346 N->getValueType(0)));
347 } else if (N->getNumOperands() == 1) {
349 UnaryOps.erase(std::make_pair(N->getOpcode(),
350 std::make_pair(N->getOperand(0),
351 N->getValueType(0))));
352 } else if (N->getNumOperands() == 2) {
354 BinaryOps.erase(std::make_pair(N->getOpcode(),
355 std::make_pair(N->getOperand(0),
358 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
360 OneResultNodes.erase(std::make_pair(N->getOpcode(),
361 std::make_pair(N->getValueType(0),
365 // Remove the node from the ArbitraryNodes map.
366 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
367 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
369 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
370 std::make_pair(RV, Ops)));
375 // Verify that the node was actually in one of the CSE maps, unless it has a
376 // flag result (which cannot be CSE'd) or is one of the special cases that are
377 // not subject to CSE.
378 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
379 !N->isTargetOpcode()) {
381 assert(0 && "Node is not in map!");
386 /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
387 /// has been taken out and modified in some way. If the specified node already
388 /// exists in the CSE maps, do not modify the maps, but return the existing node
389 /// instead. If it doesn't exist, add it and return null.
391 SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
392 assert(N->getNumOperands() && "This is a leaf node!");
393 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
394 return 0; // Never add these nodes.
396 // Check that remaining values produced are not flags.
397 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
398 if (N->getValueType(i) == MVT::Flag)
399 return 0; // Never CSE anything that produces a flag.
401 if (N->getNumValues() == 1) {
402 if (N->getNumOperands() == 1) {
403 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
404 std::make_pair(N->getOperand(0),
405 N->getValueType(0)))];
408 } else if (N->getNumOperands() == 2) {
409 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
410 std::make_pair(N->getOperand(0),
415 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
416 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
417 std::make_pair(N->getValueType(0), Ops))];
422 if (N->getOpcode() == ISD::LOAD) {
423 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
424 std::make_pair(N->getOperand(0),
425 N->getValueType(0)))];
429 // Remove the node from the ArbitraryNodes map.
430 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
431 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
432 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
433 std::make_pair(RV, Ops))];
441 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
442 /// were replaced with those specified. If this node is never memoized,
443 /// return null, otherwise return a pointer to the slot it would take. If a
444 /// node already exists with these operands, the slot will be non-null.
445 SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
446 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
447 return 0; // Never add these nodes.
449 // Check that remaining values produced are not flags.
450 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
451 if (N->getValueType(i) == MVT::Flag)
452 return 0; // Never CSE anything that produces a flag.
454 if (N->getNumValues() == 1) {
455 return &UnaryOps[std::make_pair(N->getOpcode(),
456 std::make_pair(Op, N->getValueType(0)))];
458 // Remove the node from the ArbitraryNodes map.
459 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
460 std::vector<SDOperand> Ops;
462 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
463 std::make_pair(RV, Ops))];
468 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
469 /// were replaced with those specified. If this node is never memoized,
470 /// return null, otherwise return a pointer to the slot it would take. If a
471 /// node already exists with these operands, the slot will be non-null.
472 SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
473 SDOperand Op1, SDOperand Op2) {
474 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
475 return 0; // Never add these nodes.
477 // Check that remaining values produced are not flags.
478 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
479 if (N->getValueType(i) == MVT::Flag)
480 return 0; // Never CSE anything that produces a flag.
482 if (N->getNumValues() == 1) {
483 return &BinaryOps[std::make_pair(N->getOpcode(),
484 std::make_pair(Op1, Op2))];
486 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
487 std::vector<SDOperand> Ops;
490 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
491 std::make_pair(RV, Ops))];
497 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
498 /// were replaced with those specified. If this node is never memoized,
499 /// return null, otherwise return a pointer to the slot it would take. If a
500 /// node already exists with these operands, the slot will be non-null.
501 SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
502 const std::vector<SDOperand> &Ops) {
503 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
504 return 0; // Never add these nodes.
506 // Check that remaining values produced are not flags.
507 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
508 if (N->getValueType(i) == MVT::Flag)
509 return 0; // Never CSE anything that produces a flag.
511 if (N->getNumValues() == 1) {
512 if (N->getNumOperands() == 1) {
513 return &UnaryOps[std::make_pair(N->getOpcode(),
514 std::make_pair(Ops[0],
515 N->getValueType(0)))];
516 } else if (N->getNumOperands() == 2) {
517 return &BinaryOps[std::make_pair(N->getOpcode(),
518 std::make_pair(Ops[0], Ops[1]))];
520 return &OneResultNodes[std::make_pair(N->getOpcode(),
521 std::make_pair(N->getValueType(0),
525 if (N->getOpcode() == ISD::LOAD) {
526 return &Loads[std::make_pair(Ops[1],
527 std::make_pair(Ops[0], N->getValueType(0)))];
529 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
530 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
531 std::make_pair(RV, Ops))];
538 SelectionDAG::~SelectionDAG() {
539 while (!AllNodes.empty()) {
540 SDNode *N = AllNodes.begin();
541 delete [] N->OperandList;
544 AllNodes.pop_front();
548 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
549 if (Op.getValueType() == VT) return Op;
550 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
551 return getNode(ISD::AND, Op.getValueType(), Op,
552 getConstant(Imm, Op.getValueType()));
555 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
556 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
557 // Mask out any bits that are not valid for this constant.
559 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
561 SDNode *&N = Constants[std::make_pair(Val, VT)];
562 if (N) return SDOperand(N, 0);
563 N = new ConstantSDNode(false, Val, VT);
564 AllNodes.push_back(N);
565 return SDOperand(N, 0);
568 SDOperand SelectionDAG::getString(const std::string &Val) {
569 StringSDNode *&N = StringNodes[Val];
571 N = new StringSDNode(Val);
572 AllNodes.push_back(N);
574 return SDOperand(N, 0);
577 SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
578 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
579 // Mask out any bits that are not valid for this constant.
581 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
583 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
584 if (N) return SDOperand(N, 0);
585 N = new ConstantSDNode(true, Val, VT);
586 AllNodes.push_back(N);
587 return SDOperand(N, 0);
590 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
591 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
593 Val = (float)Val; // Mask out extra precision.
595 // Do the map lookup using the actual bit pattern for the floating point
596 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
597 // we don't have issues with SNANs.
598 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
599 if (N) return SDOperand(N, 0);
600 N = new ConstantFPSDNode(false, Val, VT);
601 AllNodes.push_back(N);
602 return SDOperand(N, 0);
605 SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
606 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
608 Val = (float)Val; // Mask out extra precision.
610 // Do the map lookup using the actual bit pattern for the floating point
611 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
612 // we don't have issues with SNANs.
613 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
614 if (N) return SDOperand(N, 0);
615 N = new ConstantFPSDNode(true, Val, VT);
616 AllNodes.push_back(N);
617 return SDOperand(N, 0);
620 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
621 MVT::ValueType VT, int offset) {
622 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
623 if (N) return SDOperand(N, 0);
624 N = new GlobalAddressSDNode(false, GV, VT, offset);
625 AllNodes.push_back(N);
626 return SDOperand(N, 0);
629 SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
630 MVT::ValueType VT, int offset) {
631 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
632 if (N) return SDOperand(N, 0);
633 N = new GlobalAddressSDNode(true, GV, VT, offset);
634 AllNodes.push_back(N);
635 return SDOperand(N, 0);
638 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
639 SDNode *&N = FrameIndices[FI];
640 if (N) return SDOperand(N, 0);
641 N = new FrameIndexSDNode(FI, VT, false);
642 AllNodes.push_back(N);
643 return SDOperand(N, 0);
646 SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
647 SDNode *&N = TargetFrameIndices[FI];
648 if (N) return SDOperand(N, 0);
649 N = new FrameIndexSDNode(FI, VT, true);
650 AllNodes.push_back(N);
651 return SDOperand(N, 0);
654 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
655 unsigned Alignment, int Offset) {
656 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
657 std::make_pair(Offset, Alignment))];
658 if (N) return SDOperand(N, 0);
659 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
660 AllNodes.push_back(N);
661 return SDOperand(N, 0);
664 SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
665 unsigned Alignment, int Offset) {
666 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
667 std::make_pair(Offset, Alignment))];
668 if (N) return SDOperand(N, 0);
669 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
670 AllNodes.push_back(N);
671 return SDOperand(N, 0);
674 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
675 SDNode *&N = BBNodes[MBB];
676 if (N) return SDOperand(N, 0);
677 N = new BasicBlockSDNode(MBB);
678 AllNodes.push_back(N);
679 return SDOperand(N, 0);
682 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
683 if ((unsigned)VT >= ValueTypeNodes.size())
684 ValueTypeNodes.resize(VT+1);
685 if (ValueTypeNodes[VT] == 0) {
686 ValueTypeNodes[VT] = new VTSDNode(VT);
687 AllNodes.push_back(ValueTypeNodes[VT]);
690 return SDOperand(ValueTypeNodes[VT], 0);
693 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
694 SDNode *&N = ExternalSymbols[Sym];
695 if (N) return SDOperand(N, 0);
696 N = new ExternalSymbolSDNode(false, Sym, VT);
697 AllNodes.push_back(N);
698 return SDOperand(N, 0);
701 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
703 SDNode *&N = TargetExternalSymbols[Sym];
704 if (N) return SDOperand(N, 0);
705 N = new ExternalSymbolSDNode(true, Sym, VT);
706 AllNodes.push_back(N);
707 return SDOperand(N, 0);
710 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
711 if ((unsigned)Cond >= CondCodeNodes.size())
712 CondCodeNodes.resize(Cond+1);
714 if (CondCodeNodes[Cond] == 0) {
715 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
716 AllNodes.push_back(CondCodeNodes[Cond]);
718 return SDOperand(CondCodeNodes[Cond], 0);
721 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
722 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
724 Reg = new RegisterSDNode(RegNo, VT);
725 AllNodes.push_back(Reg);
727 return SDOperand(Reg, 0);
730 SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
731 SDOperand N2, ISD::CondCode Cond) {
732 // These setcc operations always fold.
736 case ISD::SETFALSE2: return getConstant(0, VT);
738 case ISD::SETTRUE2: return getConstant(1, VT);
741 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
742 uint64_t C2 = N2C->getValue();
743 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
744 uint64_t C1 = N1C->getValue();
746 // Sign extend the operands if required
747 if (ISD::isSignedIntSetCC(Cond)) {
748 C1 = N1C->getSignExtended();
749 C2 = N2C->getSignExtended();
753 default: assert(0 && "Unknown integer setcc!");
754 case ISD::SETEQ: return getConstant(C1 == C2, VT);
755 case ISD::SETNE: return getConstant(C1 != C2, VT);
756 case ISD::SETULT: return getConstant(C1 < C2, VT);
757 case ISD::SETUGT: return getConstant(C1 > C2, VT);
758 case ISD::SETULE: return getConstant(C1 <= C2, VT);
759 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
760 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
761 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
762 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
763 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
766 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
767 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
768 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
770 // If the comparison constant has bits in the upper part, the
771 // zero-extended value could never match.
772 if (C2 & (~0ULL << InSize)) {
773 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
777 case ISD::SETEQ: return getConstant(0, VT);
780 case ISD::SETNE: return getConstant(1, VT);
783 // True if the sign bit of C2 is set.
784 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
787 // True if the sign bit of C2 isn't set.
788 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
794 // Otherwise, we can perform the comparison with the low bits.
802 return getSetCC(VT, N1.getOperand(0),
803 getConstant(C2, N1.getOperand(0).getValueType()),
806 break; // todo, be more careful with signed comparisons
808 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
809 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
810 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
811 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
812 MVT::ValueType ExtDstTy = N1.getValueType();
813 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
815 // If the extended part has any inconsistent bits, it cannot ever
816 // compare equal. In other words, they have to be all ones or all
819 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
820 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
821 return getConstant(Cond == ISD::SETNE, VT);
823 // Otherwise, make this a use of a zext.
824 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
825 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
829 uint64_t MinVal, MaxVal;
830 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
831 if (ISD::isSignedIntSetCC(Cond)) {
832 MinVal = 1ULL << (OperandBitSize-1);
833 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
834 MaxVal = ~0ULL >> (65-OperandBitSize);
839 MaxVal = ~0ULL >> (64-OperandBitSize);
842 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
843 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
844 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
845 --C2; // X >= C1 --> X > (C1-1)
846 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
847 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
850 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
851 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
852 ++C2; // X <= C1 --> X < (C1+1)
853 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
854 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
857 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
858 return getConstant(0, VT); // X < MIN --> false
860 // Canonicalize setgt X, Min --> setne X, Min
861 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
862 return getSetCC(VT, N1, N2, ISD::SETNE);
864 // If we have setult X, 1, turn it into seteq X, 0
865 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
866 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
868 // If we have setugt X, Max-1, turn it into seteq X, Max
869 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
870 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
873 // If we have "setcc X, C1", check to see if we can shrink the immediate
876 // SETUGT X, SINTMAX -> SETLT X, 0
877 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
878 C2 == (~0ULL >> (65-OperandBitSize)))
879 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
881 // FIXME: Implement the rest of these.
884 // Fold bit comparisons when we can.
885 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
886 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
887 if (ConstantSDNode *AndRHS =
888 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
889 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
890 // Perform the xform if the AND RHS is a single bit.
891 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
892 return getNode(ISD::SRL, VT, N1,
893 getConstant(Log2_64(AndRHS->getValue()),
894 TLI.getShiftAmountTy()));
896 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
897 // (X & 8) == 8 --> (X & 8) >> 3
898 // Perform the xform if C2 is a single bit.
899 if ((C2 & (C2-1)) == 0) {
900 return getNode(ISD::SRL, VT, N1,
901 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
906 } else if (isa<ConstantSDNode>(N1.Val)) {
907 // Ensure that the constant occurs on the RHS.
908 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
911 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
912 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
913 double C1 = N1C->getValue(), C2 = N2C->getValue();
916 default: break; // FIXME: Implement the rest of these!
917 case ISD::SETEQ: return getConstant(C1 == C2, VT);
918 case ISD::SETNE: return getConstant(C1 != C2, VT);
919 case ISD::SETLT: return getConstant(C1 < C2, VT);
920 case ISD::SETGT: return getConstant(C1 > C2, VT);
921 case ISD::SETLE: return getConstant(C1 <= C2, VT);
922 case ISD::SETGE: return getConstant(C1 >= C2, VT);
925 // Ensure that the constant occurs on the RHS.
926 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
929 // Could not fold it.
933 /// getNode - Gets or creates the specified node.
935 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
936 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
938 N = new SDNode(Opcode, VT);
939 AllNodes.push_back(N);
941 return SDOperand(N, 0);
944 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
947 // Constant fold unary operations with an integer constant operand.
948 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
949 uint64_t Val = C->getValue();
952 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
953 case ISD::ANY_EXTEND:
954 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
955 case ISD::TRUNCATE: return getConstant(Val, VT);
956 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
957 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
958 case ISD::BIT_CONVERT:
959 if (VT == MVT::f32) {
960 assert(C->getValueType(0) == MVT::i32 && "Invalid bit_convert!");
961 return getConstantFP(BitsToFloat(Val), VT);
962 } else if (VT == MVT::f64) {
963 assert(C->getValueType(0) == MVT::i64 && "Invalid bit_convert!");
964 return getConstantFP(BitsToDouble(Val), VT);
969 default: assert(0 && "Invalid bswap!"); break;
970 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
971 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
972 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
977 default: assert(0 && "Invalid ctpop!"); break;
978 case MVT::i1: return getConstant(Val != 0, VT);
980 Tmp1 = (unsigned)Val & 0xFF;
981 return getConstant(CountPopulation_32(Tmp1), VT);
983 Tmp1 = (unsigned)Val & 0xFFFF;
984 return getConstant(CountPopulation_32(Tmp1), VT);
986 return getConstant(CountPopulation_32((unsigned)Val), VT);
988 return getConstant(CountPopulation_64(Val), VT);
992 default: assert(0 && "Invalid ctlz!"); break;
993 case MVT::i1: return getConstant(Val == 0, VT);
995 Tmp1 = (unsigned)Val & 0xFF;
996 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
998 Tmp1 = (unsigned)Val & 0xFFFF;
999 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1001 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1003 return getConstant(CountLeadingZeros_64(Val), VT);
1007 default: assert(0 && "Invalid cttz!"); break;
1008 case MVT::i1: return getConstant(Val == 0, VT);
1010 Tmp1 = (unsigned)Val | 0x100;
1011 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1013 Tmp1 = (unsigned)Val | 0x10000;
1014 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1016 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1018 return getConstant(CountTrailingZeros_64(Val), VT);
1023 // Constant fold unary operations with an floating point constant operand.
1024 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1027 return getConstantFP(-C->getValue(), VT);
1029 return getConstantFP(fabs(C->getValue()), VT);
1031 case ISD::FP_EXTEND:
1032 return getConstantFP(C->getValue(), VT);
1033 case ISD::FP_TO_SINT:
1034 return getConstant((int64_t)C->getValue(), VT);
1035 case ISD::FP_TO_UINT:
1036 return getConstant((uint64_t)C->getValue(), VT);
1037 case ISD::BIT_CONVERT:
1038 if (VT == MVT::i32) {
1039 assert(C->getValueType(0) == MVT::f32 && "Invalid bit_convert!");
1040 return getConstant(FloatToBits(C->getValue()), VT);
1041 } else if (VT == MVT::i64) {
1042 assert(C->getValueType(0) == MVT::f64 && "Invalid bit_convert!");
1043 return getConstant(DoubleToBits(C->getValue()), VT);
1048 unsigned OpOpcode = Operand.Val->getOpcode();
1050 case ISD::TokenFactor:
1051 return Operand; // Factor of one node? No factor.
1052 case ISD::SIGN_EXTEND:
1053 if (Operand.getValueType() == VT) return Operand; // noop extension
1054 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
1055 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1056 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1058 case ISD::ZERO_EXTEND:
1059 if (Operand.getValueType() == VT) return Operand; // noop extension
1060 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
1061 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1062 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1064 case ISD::ANY_EXTEND:
1065 if (Operand.getValueType() == VT) return Operand; // noop extension
1066 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
1067 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1068 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1069 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1072 if (Operand.getValueType() == VT) return Operand; // noop truncate
1073 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
1074 if (OpOpcode == ISD::TRUNCATE)
1075 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1076 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1077 OpOpcode == ISD::ANY_EXTEND) {
1078 // If the source is smaller than the dest, we still need an extend.
1079 if (Operand.Val->getOperand(0).getValueType() < VT)
1080 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1081 else if (Operand.Val->getOperand(0).getValueType() > VT)
1082 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1084 return Operand.Val->getOperand(0);
1087 case ISD::BIT_CONVERT:
1088 // Basic sanity checking.
1089 assert((Operand.getValueType() == MVT::Vector || // FIXME: This is a hack.
1090 MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType()))
1091 && "Cannot BIT_CONVERT between two different types!");
1092 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1093 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1094 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1096 case ISD::SCALAR_TO_VECTOR:
1097 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1098 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1099 "Illegal SCALAR_TO_VECTOR node!");
1102 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1103 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1104 Operand.Val->getOperand(0));
1105 if (OpOpcode == ISD::FNEG) // --X -> X
1106 return Operand.Val->getOperand(0);
1109 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1110 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1115 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1116 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
1117 if (E) return SDOperand(E, 0);
1118 E = N = new SDNode(Opcode, Operand);
1120 N = new SDNode(Opcode, Operand);
1122 N->setValueTypes(VT);
1123 AllNodes.push_back(N);
1124 return SDOperand(N, 0);
1129 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1130 SDOperand N1, SDOperand N2) {
1133 case ISD::TokenFactor:
1134 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1135 N2.getValueType() == MVT::Other && "Invalid token factor!");
1144 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1151 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1158 assert(N1.getValueType() == N2.getValueType() &&
1159 N1.getValueType() == VT && "Binary operator types must match!");
1161 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1162 assert(N1.getValueType() == VT &&
1163 MVT::isFloatingPoint(N1.getValueType()) &&
1164 MVT::isFloatingPoint(N2.getValueType()) &&
1165 "Invalid FCOPYSIGN!");
1172 assert(VT == N1.getValueType() &&
1173 "Shift operators return type must be the same as their first arg");
1174 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1175 VT != MVT::i1 && "Shifts only work on integers");
1177 case ISD::FP_ROUND_INREG: {
1178 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1179 assert(VT == N1.getValueType() && "Not an inreg round!");
1180 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1181 "Cannot FP_ROUND_INREG integer types");
1182 assert(EVT <= VT && "Not rounding down!");
1185 case ISD::AssertSext:
1186 case ISD::AssertZext:
1187 case ISD::SIGN_EXTEND_INREG: {
1188 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1189 assert(VT == N1.getValueType() && "Not an inreg extend!");
1190 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1191 "Cannot *_EXTEND_INREG FP types");
1192 assert(EVT <= VT && "Not extending!");
1199 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1200 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1203 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1205 case ISD::ADD: return getConstant(C1 + C2, VT);
1206 case ISD::SUB: return getConstant(C1 - C2, VT);
1207 case ISD::MUL: return getConstant(C1 * C2, VT);
1209 if (C2) return getConstant(C1 / C2, VT);
1212 if (C2) return getConstant(C1 % C2, VT);
1215 if (C2) return getConstant(N1C->getSignExtended() /
1216 N2C->getSignExtended(), VT);
1219 if (C2) return getConstant(N1C->getSignExtended() %
1220 N2C->getSignExtended(), VT);
1222 case ISD::AND : return getConstant(C1 & C2, VT);
1223 case ISD::OR : return getConstant(C1 | C2, VT);
1224 case ISD::XOR : return getConstant(C1 ^ C2, VT);
1225 case ISD::SHL : return getConstant(C1 << C2, VT);
1226 case ISD::SRL : return getConstant(C1 >> C2, VT);
1227 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
1229 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1232 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1236 } else { // Cannonicalize constant to RHS if commutative
1237 if (isCommutativeBinOp(Opcode)) {
1238 std::swap(N1C, N2C);
1244 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1245 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1248 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1250 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1251 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1252 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1254 if (C2) return getConstantFP(C1 / C2, VT);
1257 if (C2) return getConstantFP(fmod(C1, C2), VT);
1259 case ISD::FCOPYSIGN: {
1270 if (u2.I < 0) // Sign bit of RHS set?
1271 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1273 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1274 return getConstantFP(u1.F, VT);
1278 } else { // Cannonicalize constant to RHS if commutative
1279 if (isCommutativeBinOp(Opcode)) {
1280 std::swap(N1CFP, N2CFP);
1286 // Finally, fold operations that do not require constants.
1288 case ISD::FP_ROUND_INREG:
1289 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1291 case ISD::SIGN_EXTEND_INREG: {
1292 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1293 if (EVT == VT) return N1; // Not actually extending
1297 // FIXME: figure out how to safely handle things like
1298 // int foo(int x) { return 1 << (x & 255); }
1299 // int bar() { return foo(256); }
1304 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1305 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
1306 return getNode(Opcode, VT, N1, N2.getOperand(0));
1307 else if (N2.getOpcode() == ISD::AND)
1308 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1309 // If the and is only masking out bits that cannot effect the shift,
1310 // eliminate the and.
1311 unsigned NumBits = MVT::getSizeInBits(VT);
1312 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1313 return getNode(Opcode, VT, N1, N2.getOperand(0));
1319 // Memoize this node if possible.
1321 if (VT != MVT::Flag) {
1322 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1323 if (BON) return SDOperand(BON, 0);
1325 BON = N = new SDNode(Opcode, N1, N2);
1327 N = new SDNode(Opcode, N1, N2);
1330 N->setValueTypes(VT);
1331 AllNodes.push_back(N);
1332 return SDOperand(N, 0);
1335 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1336 SDOperand N1, SDOperand N2, SDOperand N3) {
1337 // Perform various simplifications.
1338 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1339 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1340 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1343 // Use SimplifySetCC to simplify SETCC's.
1344 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
1345 if (Simp.Val) return Simp;
1350 if (N1C->getValue())
1351 return N2; // select true, X, Y -> X
1353 return N3; // select false, X, Y -> Y
1355 if (N2 == N3) return N2; // select C, X, X -> X
1359 if (N2C->getValue()) // Unconditional branch
1360 return getNode(ISD::BR, MVT::Other, N1, N3);
1362 return N1; // Never-taken branch
1364 case ISD::VECTOR_SHUFFLE:
1365 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1366 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1367 N3.getOpcode() == ISD::BUILD_VECTOR &&
1368 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1369 "Illegal VECTOR_SHUFFLE node!");
1373 std::vector<SDOperand> Ops;
1379 // Memoize node if it doesn't produce a flag.
1381 if (VT != MVT::Flag) {
1382 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1383 if (E) return SDOperand(E, 0);
1384 E = N = new SDNode(Opcode, N1, N2, N3);
1386 N = new SDNode(Opcode, N1, N2, N3);
1388 N->setValueTypes(VT);
1389 AllNodes.push_back(N);
1390 return SDOperand(N, 0);
1393 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1394 SDOperand N1, SDOperand N2, SDOperand N3,
1396 std::vector<SDOperand> Ops;
1402 return getNode(Opcode, VT, Ops);
1405 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1406 SDOperand N1, SDOperand N2, SDOperand N3,
1407 SDOperand N4, SDOperand N5) {
1408 std::vector<SDOperand> Ops;
1415 return getNode(Opcode, VT, Ops);
1418 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1419 SDOperand Chain, SDOperand Ptr,
1421 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1422 if (N) return SDOperand(N, 0);
1423 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1425 // Loads have a token chain.
1426 setNodeValueTypes(N, VT, MVT::Other);
1427 AllNodes.push_back(N);
1428 return SDOperand(N, 0);
1431 SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1432 SDOperand Chain, SDOperand Ptr,
1434 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1435 if (N) return SDOperand(N, 0);
1436 std::vector<SDOperand> Ops;
1438 Ops.push_back(Chain);
1441 Ops.push_back(getConstant(Count, MVT::i32));
1442 Ops.push_back(getValueType(EVT));
1443 std::vector<MVT::ValueType> VTs;
1445 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1446 return getNode(ISD::VLOAD, VTs, Ops);
1449 SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1450 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1451 MVT::ValueType EVT) {
1452 std::vector<SDOperand> Ops;
1454 Ops.push_back(Chain);
1457 Ops.push_back(getValueType(EVT));
1458 std::vector<MVT::ValueType> VTs;
1460 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1461 return getNode(Opcode, VTs, Ops);
1464 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
1465 assert((!V || isa<PointerType>(V->getType())) &&
1466 "SrcValue is not a pointer?");
1467 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1468 if (N) return SDOperand(N, 0);
1470 N = new SrcValueSDNode(V, Offset);
1471 AllNodes.push_back(N);
1472 return SDOperand(N, 0);
1475 SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1476 SDOperand Chain, SDOperand Ptr,
1478 std::vector<SDOperand> Ops;
1480 Ops.push_back(Chain);
1483 std::vector<MVT::ValueType> VTs;
1485 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1486 return getNode(ISD::VAARG, VTs, Ops);
1489 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1490 std::vector<SDOperand> &Ops) {
1491 switch (Ops.size()) {
1492 case 0: return getNode(Opcode, VT);
1493 case 1: return getNode(Opcode, VT, Ops[0]);
1494 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1495 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
1499 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
1502 case ISD::TRUNCSTORE: {
1503 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1504 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1505 #if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1506 // If this is a truncating store of a constant, convert to the desired type
1507 // and store it instead.
1508 if (isa<Constant>(Ops[0])) {
1509 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1510 if (isa<Constant>(Op))
1513 // Also for ConstantFP?
1515 if (Ops[0].getValueType() == EVT) // Normal store?
1516 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1517 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1518 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1519 "Can't do FP-INT conversion!");
1522 case ISD::SELECT_CC: {
1523 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
1524 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1525 "LHS and RHS of condition must have same type!");
1526 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1527 "True and False arms of SelectCC must have same type!");
1528 assert(Ops[2].getValueType() == VT &&
1529 "select_cc node must be of same type as true and false value!");
1533 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
1534 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1535 "LHS/RHS of comparison should match types!");
1542 if (VT != MVT::Flag) {
1544 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1545 if (E) return SDOperand(E, 0);
1546 E = N = new SDNode(Opcode, Ops);
1548 N = new SDNode(Opcode, Ops);
1550 N->setValueTypes(VT);
1551 AllNodes.push_back(N);
1552 return SDOperand(N, 0);
1555 SDOperand SelectionDAG::getNode(unsigned Opcode,
1556 std::vector<MVT::ValueType> &ResultTys,
1557 std::vector<SDOperand> &Ops) {
1558 if (ResultTys.size() == 1)
1559 return getNode(Opcode, ResultTys[0], Ops);
1564 case ISD::ZEXTLOAD: {
1565 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1566 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1567 // If they are asking for an extending load from/to the same thing, return a
1569 if (ResultTys[0] == EVT)
1570 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1571 if (MVT::isVector(ResultTys[0])) {
1572 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1573 "Invalid vector extload!");
1575 assert(EVT < ResultTys[0] &&
1576 "Should only be an extending load, not truncating!");
1578 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1579 "Cannot sign/zero extend a FP/Vector load!");
1580 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1581 "Cannot convert from FP to Int or Int -> FP!");
1585 // FIXME: figure out how to safely handle things like
1586 // int foo(int x) { return 1 << (x & 255); }
1587 // int bar() { return foo(256); }
1589 case ISD::SRA_PARTS:
1590 case ISD::SRL_PARTS:
1591 case ISD::SHL_PARTS:
1592 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1593 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
1594 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1595 else if (N3.getOpcode() == ISD::AND)
1596 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1597 // If the and is only masking out bits that cannot effect the shift,
1598 // eliminate the and.
1599 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1600 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1601 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1607 // Memoize the node unless it returns a flag.
1609 if (ResultTys.back() != MVT::Flag) {
1611 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1612 if (E) return SDOperand(E, 0);
1613 E = N = new SDNode(Opcode, Ops);
1615 N = new SDNode(Opcode, Ops);
1617 setNodeValueTypes(N, ResultTys);
1618 AllNodes.push_back(N);
1619 return SDOperand(N, 0);
1622 void SelectionDAG::setNodeValueTypes(SDNode *N,
1623 std::vector<MVT::ValueType> &RetVals) {
1624 switch (RetVals.size()) {
1626 case 1: N->setValueTypes(RetVals[0]); return;
1627 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1631 std::list<std::vector<MVT::ValueType> >::iterator I =
1632 std::find(VTList.begin(), VTList.end(), RetVals);
1633 if (I == VTList.end()) {
1634 VTList.push_front(RetVals);
1638 N->setValueTypes(&(*I)[0], I->size());
1641 void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1642 MVT::ValueType VT2) {
1643 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1644 E = VTList.end(); I != E; ++I) {
1645 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1646 N->setValueTypes(&(*I)[0], 2);
1650 std::vector<MVT::ValueType> V;
1653 VTList.push_front(V);
1654 N->setValueTypes(&(*VTList.begin())[0], 2);
1657 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1658 /// specified operands. If the resultant node already exists in the DAG,
1659 /// this does not modify the specified node, instead it returns the node that
1660 /// already exists. If the resultant node does not exist in the DAG, the
1661 /// input node is returned. As a degenerate case, if you specify the same
1662 /// input operands as the node already has, the input node is returned.
1663 SDOperand SelectionDAG::
1664 UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1665 SDNode *N = InN.Val;
1666 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1668 // Check to see if there is no change.
1669 if (Op == N->getOperand(0)) return InN;
1671 // See if the modified node already exists.
1672 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1673 if (NewSlot && *NewSlot)
1674 return SDOperand(*NewSlot, InN.ResNo);
1676 // Nope it doesn't. Remove the node from it's current place in the maps.
1678 RemoveNodeFromCSEMaps(N);
1680 // Now we update the operands.
1681 N->OperandList[0].Val->removeUser(N);
1683 N->OperandList[0] = Op;
1685 // If this gets put into a CSE map, add it.
1686 if (NewSlot) *NewSlot = N;
1690 SDOperand SelectionDAG::
1691 UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1692 SDNode *N = InN.Val;
1693 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1695 // Check to see if there is no change.
1696 bool AnyChange = false;
1697 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1698 return InN; // No operands changed, just return the input node.
1700 // See if the modified node already exists.
1701 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1702 if (NewSlot && *NewSlot)
1703 return SDOperand(*NewSlot, InN.ResNo);
1705 // Nope it doesn't. Remove the node from it's current place in the maps.
1707 RemoveNodeFromCSEMaps(N);
1709 // Now we update the operands.
1710 if (N->OperandList[0] != Op1) {
1711 N->OperandList[0].Val->removeUser(N);
1712 Op1.Val->addUser(N);
1713 N->OperandList[0] = Op1;
1715 if (N->OperandList[1] != Op2) {
1716 N->OperandList[1].Val->removeUser(N);
1717 Op2.Val->addUser(N);
1718 N->OperandList[1] = Op2;
1721 // If this gets put into a CSE map, add it.
1722 if (NewSlot) *NewSlot = N;
1726 SDOperand SelectionDAG::
1727 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1728 std::vector<SDOperand> Ops;
1732 return UpdateNodeOperands(N, Ops);
1735 SDOperand SelectionDAG::
1736 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1737 SDOperand Op3, SDOperand Op4) {
1738 std::vector<SDOperand> Ops;
1743 return UpdateNodeOperands(N, Ops);
1746 SDOperand SelectionDAG::
1747 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1748 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1749 std::vector<SDOperand> Ops;
1755 return UpdateNodeOperands(N, Ops);
1759 SDOperand SelectionDAG::
1760 UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1761 SDNode *N = InN.Val;
1762 assert(N->getNumOperands() == Ops.size() &&
1763 "Update with wrong number of operands");
1765 // Check to see if there is no change.
1766 unsigned NumOps = Ops.size();
1767 bool AnyChange = false;
1768 for (unsigned i = 0; i != NumOps; ++i) {
1769 if (Ops[i] != N->getOperand(i)) {
1775 // No operands changed, just return the input node.
1776 if (!AnyChange) return InN;
1778 // See if the modified node already exists.
1779 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1780 if (NewSlot && *NewSlot)
1781 return SDOperand(*NewSlot, InN.ResNo);
1783 // Nope it doesn't. Remove the node from it's current place in the maps.
1785 RemoveNodeFromCSEMaps(N);
1787 // Now we update the operands.
1788 for (unsigned i = 0; i != NumOps; ++i) {
1789 if (N->OperandList[i] != Ops[i]) {
1790 N->OperandList[i].Val->removeUser(N);
1791 Ops[i].Val->addUser(N);
1792 N->OperandList[i] = Ops[i];
1796 // If this gets put into a CSE map, add it.
1797 if (NewSlot) *NewSlot = N;
1804 /// SelectNodeTo - These are used for target selectors to *mutate* the
1805 /// specified node to have the specified return type, Target opcode, and
1806 /// operands. Note that target opcodes are stored as
1807 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1809 /// Note that SelectNodeTo returns the resultant node. If there is already a
1810 /// node of the specified opcode and operands, it returns that node instead of
1811 /// the current one.
1812 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1813 MVT::ValueType VT) {
1814 // If an identical node already exists, use it.
1815 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1816 if (ON) return SDOperand(ON, 0);
1818 RemoveNodeFromCSEMaps(N);
1820 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1821 N->setValueTypes(VT);
1823 ON = N; // Memoize the new node.
1824 return SDOperand(N, 0);
1827 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1828 MVT::ValueType VT, SDOperand Op1) {
1829 // If an identical node already exists, use it.
1830 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1831 std::make_pair(Op1, VT))];
1832 if (ON) return SDOperand(ON, 0);
1834 RemoveNodeFromCSEMaps(N);
1835 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1836 N->setValueTypes(VT);
1837 N->setOperands(Op1);
1839 ON = N; // Memoize the new node.
1840 return SDOperand(N, 0);
1843 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1844 MVT::ValueType VT, SDOperand Op1,
1846 // If an identical node already exists, use it.
1847 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1848 std::make_pair(Op1, Op2))];
1849 if (ON) return SDOperand(ON, 0);
1851 RemoveNodeFromCSEMaps(N);
1852 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1853 N->setValueTypes(VT);
1854 N->setOperands(Op1, Op2);
1856 ON = N; // Memoize the new node.
1857 return SDOperand(N, 0);
1860 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1861 MVT::ValueType VT, SDOperand Op1,
1862 SDOperand Op2, SDOperand Op3) {
1863 // If an identical node already exists, use it.
1864 std::vector<SDOperand> OpList;
1865 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1866 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1867 std::make_pair(VT, OpList))];
1868 if (ON) return SDOperand(ON, 0);
1870 RemoveNodeFromCSEMaps(N);
1871 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1872 N->setValueTypes(VT);
1873 N->setOperands(Op1, Op2, Op3);
1875 ON = N; // Memoize the new node.
1876 return SDOperand(N, 0);
1879 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1880 MVT::ValueType VT, SDOperand Op1,
1881 SDOperand Op2, SDOperand Op3,
1883 // If an identical node already exists, use it.
1884 std::vector<SDOperand> OpList;
1885 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1886 OpList.push_back(Op4);
1887 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1888 std::make_pair(VT, OpList))];
1889 if (ON) return SDOperand(ON, 0);
1891 RemoveNodeFromCSEMaps(N);
1892 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1893 N->setValueTypes(VT);
1894 N->setOperands(Op1, Op2, Op3, Op4);
1896 ON = N; // Memoize the new node.
1897 return SDOperand(N, 0);
1900 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1901 MVT::ValueType VT, SDOperand Op1,
1902 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1904 // If an identical node already exists, use it.
1905 std::vector<SDOperand> OpList;
1906 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1907 OpList.push_back(Op4); OpList.push_back(Op5);
1908 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1909 std::make_pair(VT, OpList))];
1910 if (ON) return SDOperand(ON, 0);
1912 RemoveNodeFromCSEMaps(N);
1913 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1914 N->setValueTypes(VT);
1915 N->setOperands(Op1, Op2, Op3, Op4, Op5);
1917 ON = N; // Memoize the new node.
1918 return SDOperand(N, 0);
1921 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1922 MVT::ValueType VT, SDOperand Op1,
1923 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1924 SDOperand Op5, SDOperand Op6) {
1925 // If an identical node already exists, use it.
1926 std::vector<SDOperand> OpList;
1927 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1928 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1929 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1930 std::make_pair(VT, OpList))];
1931 if (ON) return SDOperand(ON, 0);
1933 RemoveNodeFromCSEMaps(N);
1934 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1935 N->setValueTypes(VT);
1936 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
1938 ON = N; // Memoize the new node.
1939 return SDOperand(N, 0);
1942 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1943 MVT::ValueType VT, SDOperand Op1,
1944 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1945 SDOperand Op5, SDOperand Op6,
1947 // If an identical node already exists, use it.
1948 std::vector<SDOperand> OpList;
1949 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1950 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1951 OpList.push_back(Op7);
1952 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1953 std::make_pair(VT, OpList))];
1954 if (ON) return SDOperand(ON, 0);
1956 RemoveNodeFromCSEMaps(N);
1957 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1958 N->setValueTypes(VT);
1959 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
1961 ON = N; // Memoize the new node.
1962 return SDOperand(N, 0);
1964 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1965 MVT::ValueType VT, SDOperand Op1,
1966 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1967 SDOperand Op5, SDOperand Op6,
1968 SDOperand Op7, SDOperand Op8) {
1969 // If an identical node already exists, use it.
1970 std::vector<SDOperand> OpList;
1971 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1972 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1973 OpList.push_back(Op7); OpList.push_back(Op8);
1974 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1975 std::make_pair(VT, OpList))];
1976 if (ON) return SDOperand(ON, 0);
1978 RemoveNodeFromCSEMaps(N);
1979 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1980 N->setValueTypes(VT);
1981 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
1983 ON = N; // Memoize the new node.
1984 return SDOperand(N, 0);
1987 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1988 MVT::ValueType VT1, MVT::ValueType VT2,
1989 SDOperand Op1, SDOperand Op2) {
1990 // If an identical node already exists, use it.
1991 std::vector<SDOperand> OpList;
1992 OpList.push_back(Op1); OpList.push_back(Op2);
1993 std::vector<MVT::ValueType> VTList;
1994 VTList.push_back(VT1); VTList.push_back(VT2);
1995 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1996 std::make_pair(VTList, OpList))];
1997 if (ON) return SDOperand(ON, 0);
1999 RemoveNodeFromCSEMaps(N);
2000 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2001 setNodeValueTypes(N, VT1, VT2);
2002 N->setOperands(Op1, Op2);
2004 ON = N; // Memoize the new node.
2005 return SDOperand(N, 0);
2008 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2009 MVT::ValueType VT1, MVT::ValueType VT2,
2010 SDOperand Op1, SDOperand Op2,
2012 // If an identical node already exists, use it.
2013 std::vector<SDOperand> OpList;
2014 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2015 std::vector<MVT::ValueType> VTList;
2016 VTList.push_back(VT1); VTList.push_back(VT2);
2017 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2018 std::make_pair(VTList, OpList))];
2019 if (ON) return SDOperand(ON, 0);
2021 RemoveNodeFromCSEMaps(N);
2022 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2023 setNodeValueTypes(N, VT1, VT2);
2024 N->setOperands(Op1, Op2, Op3);
2026 ON = N; // Memoize the new node.
2027 return SDOperand(N, 0);
2030 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2031 MVT::ValueType VT1, MVT::ValueType VT2,
2032 SDOperand Op1, SDOperand Op2,
2033 SDOperand Op3, SDOperand Op4) {
2034 // If an identical node already exists, use it.
2035 std::vector<SDOperand> OpList;
2036 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2037 OpList.push_back(Op4);
2038 std::vector<MVT::ValueType> VTList;
2039 VTList.push_back(VT1); VTList.push_back(VT2);
2040 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2041 std::make_pair(VTList, OpList))];
2042 if (ON) return SDOperand(ON, 0);
2044 RemoveNodeFromCSEMaps(N);
2045 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2046 setNodeValueTypes(N, VT1, VT2);
2047 N->setOperands(Op1, Op2, Op3, Op4);
2049 ON = N; // Memoize the new node.
2050 return SDOperand(N, 0);
2053 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2054 MVT::ValueType VT1, MVT::ValueType VT2,
2055 SDOperand Op1, SDOperand Op2,
2056 SDOperand Op3, SDOperand Op4,
2058 // If an identical node already exists, use it.
2059 std::vector<SDOperand> OpList;
2060 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2061 OpList.push_back(Op4); OpList.push_back(Op5);
2062 std::vector<MVT::ValueType> VTList;
2063 VTList.push_back(VT1); VTList.push_back(VT2);
2064 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2065 std::make_pair(VTList, OpList))];
2066 if (ON) return SDOperand(ON, 0);
2068 RemoveNodeFromCSEMaps(N);
2069 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2070 setNodeValueTypes(N, VT1, VT2);
2071 N->setOperands(Op1, Op2, Op3, Op4, Op5);
2073 ON = N; // Memoize the new node.
2074 return SDOperand(N, 0);
2077 /// getTargetNode - These are used for target selectors to create a new node
2078 /// with specified return type(s), target opcode, and operands.
2080 /// Note that getTargetNode returns the resultant node. If there is already a
2081 /// node of the specified opcode and operands, it returns that node instead of
2082 /// the current one.
2083 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2084 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2086 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2088 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2090 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2091 SDOperand Op1, SDOperand Op2) {
2092 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2094 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2095 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2096 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2098 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2099 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2101 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2103 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2104 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2105 SDOperand Op4, SDOperand Op5) {
2106 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2108 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2109 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2110 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
2111 std::vector<SDOperand> Ops;
2119 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2121 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2122 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2123 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2125 std::vector<SDOperand> Ops;
2134 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2136 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2137 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2138 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2139 SDOperand Op7, SDOperand Op8) {
2140 std::vector<SDOperand> Ops;
2150 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2152 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2153 std::vector<SDOperand> &Ops) {
2154 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2156 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2157 MVT::ValueType VT2, SDOperand Op1) {
2158 std::vector<MVT::ValueType> ResultTys;
2159 ResultTys.push_back(VT1);
2160 ResultTys.push_back(VT2);
2161 std::vector<SDOperand> Ops;
2163 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2165 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2166 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
2167 std::vector<MVT::ValueType> ResultTys;
2168 ResultTys.push_back(VT1);
2169 ResultTys.push_back(VT2);
2170 std::vector<SDOperand> Ops;
2173 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2175 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2176 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2178 std::vector<MVT::ValueType> ResultTys;
2179 ResultTys.push_back(VT1);
2180 ResultTys.push_back(VT2);
2181 std::vector<SDOperand> Ops;
2185 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2187 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2188 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2189 SDOperand Op3, SDOperand Op4) {
2190 std::vector<MVT::ValueType> ResultTys;
2191 ResultTys.push_back(VT1);
2192 ResultTys.push_back(VT2);
2193 std::vector<SDOperand> Ops;
2198 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2200 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2201 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2202 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2203 std::vector<MVT::ValueType> ResultTys;
2204 ResultTys.push_back(VT1);
2205 ResultTys.push_back(VT2);
2206 std::vector<SDOperand> Ops;
2212 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2214 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2215 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2216 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2218 std::vector<MVT::ValueType> ResultTys;
2219 ResultTys.push_back(VT1);
2220 ResultTys.push_back(VT2);
2221 std::vector<SDOperand> Ops;
2228 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2230 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2231 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2232 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2233 SDOperand Op6, SDOperand Op7) {
2234 std::vector<MVT::ValueType> ResultTys;
2235 ResultTys.push_back(VT1);
2236 ResultTys.push_back(VT2);
2237 std::vector<SDOperand> Ops;
2245 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2247 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2248 MVT::ValueType VT2, MVT::ValueType VT3,
2249 SDOperand Op1, SDOperand Op2) {
2250 std::vector<MVT::ValueType> ResultTys;
2251 ResultTys.push_back(VT1);
2252 ResultTys.push_back(VT2);
2253 ResultTys.push_back(VT3);
2254 std::vector<SDOperand> Ops;
2257 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2259 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2260 MVT::ValueType VT2, MVT::ValueType VT3,
2261 SDOperand Op1, SDOperand Op2,
2262 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2263 std::vector<MVT::ValueType> ResultTys;
2264 ResultTys.push_back(VT1);
2265 ResultTys.push_back(VT2);
2266 ResultTys.push_back(VT3);
2267 std::vector<SDOperand> Ops;
2273 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2275 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2276 MVT::ValueType VT2, MVT::ValueType VT3,
2277 SDOperand Op1, SDOperand Op2,
2278 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2280 std::vector<MVT::ValueType> ResultTys;
2281 ResultTys.push_back(VT1);
2282 ResultTys.push_back(VT2);
2283 ResultTys.push_back(VT3);
2284 std::vector<SDOperand> Ops;
2291 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2293 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2294 MVT::ValueType VT2, MVT::ValueType VT3,
2295 SDOperand Op1, SDOperand Op2,
2296 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2297 SDOperand Op6, SDOperand Op7) {
2298 std::vector<MVT::ValueType> ResultTys;
2299 ResultTys.push_back(VT1);
2300 ResultTys.push_back(VT2);
2301 ResultTys.push_back(VT3);
2302 std::vector<SDOperand> Ops;
2310 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2312 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2313 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
2314 std::vector<MVT::ValueType> ResultTys;
2315 ResultTys.push_back(VT1);
2316 ResultTys.push_back(VT2);
2317 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2320 // ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2321 /// This can cause recursive merging of nodes in the DAG.
2323 /// This version assumes From/To have a single result value.
2325 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2326 std::vector<SDNode*> *Deleted) {
2327 SDNode *From = FromN.Val, *To = ToN.Val;
2328 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2329 "Cannot replace with this method!");
2330 assert(From != To && "Cannot replace uses of with self");
2332 while (!From->use_empty()) {
2333 // Process users until they are all gone.
2334 SDNode *U = *From->use_begin();
2336 // This node is about to morph, remove its old self from the CSE maps.
2337 RemoveNodeFromCSEMaps(U);
2339 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2341 if (I->Val == From) {
2342 From->removeUser(U);
2347 // Now that we have modified U, add it back to the CSE maps. If it already
2348 // exists there, recursively merge the results together.
2349 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2350 ReplaceAllUsesWith(U, Existing, Deleted);
2352 if (Deleted) Deleted->push_back(U);
2353 DeleteNodeNotInCSEMaps(U);
2358 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2359 /// This can cause recursive merging of nodes in the DAG.
2361 /// This version assumes From/To have matching types and numbers of result
2364 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2365 std::vector<SDNode*> *Deleted) {
2366 assert(From != To && "Cannot replace uses of with self");
2367 assert(From->getNumValues() == To->getNumValues() &&
2368 "Cannot use this version of ReplaceAllUsesWith!");
2369 if (From->getNumValues() == 1) { // If possible, use the faster version.
2370 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
2374 while (!From->use_empty()) {
2375 // Process users until they are all gone.
2376 SDNode *U = *From->use_begin();
2378 // This node is about to morph, remove its old self from the CSE maps.
2379 RemoveNodeFromCSEMaps(U);
2381 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2383 if (I->Val == From) {
2384 From->removeUser(U);
2389 // Now that we have modified U, add it back to the CSE maps. If it already
2390 // exists there, recursively merge the results together.
2391 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2392 ReplaceAllUsesWith(U, Existing, Deleted);
2394 if (Deleted) Deleted->push_back(U);
2395 DeleteNodeNotInCSEMaps(U);
2400 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2401 /// This can cause recursive merging of nodes in the DAG.
2403 /// This version can replace From with any result values. To must match the
2404 /// number and types of values returned by From.
2405 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
2406 const std::vector<SDOperand> &To,
2407 std::vector<SDNode*> *Deleted) {
2408 assert(From->getNumValues() == To.size() &&
2409 "Incorrect number of values to replace with!");
2410 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
2411 // Degenerate case handled above.
2412 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
2416 while (!From->use_empty()) {
2417 // Process users until they are all gone.
2418 SDNode *U = *From->use_begin();
2420 // This node is about to morph, remove its old self from the CSE maps.
2421 RemoveNodeFromCSEMaps(U);
2423 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2425 if (I->Val == From) {
2426 const SDOperand &ToOp = To[I->ResNo];
2427 From->removeUser(U);
2429 ToOp.Val->addUser(U);
2432 // Now that we have modified U, add it back to the CSE maps. If it already
2433 // exists there, recursively merge the results together.
2434 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2435 ReplaceAllUsesWith(U, Existing, Deleted);
2437 if (Deleted) Deleted->push_back(U);
2438 DeleteNodeNotInCSEMaps(U);
2443 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2444 /// uses of other values produced by From.Val alone. The Deleted vector is
2445 /// handled the same was as for ReplaceAllUsesWith.
2446 void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2447 std::vector<SDNode*> &Deleted) {
2448 assert(From != To && "Cannot replace a value with itself");
2449 // Handle the simple, trivial, case efficiently.
2450 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2451 ReplaceAllUsesWith(From, To, &Deleted);
2455 // Get all of the users in a nice, deterministically ordered, uniqued set.
2456 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2458 while (!Users.empty()) {
2459 // We know that this user uses some value of From. If it is the right
2460 // value, update it.
2461 SDNode *User = Users.back();
2464 for (SDOperand *Op = User->OperandList,
2465 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2467 // Okay, we know this user needs to be updated. Remove its old self
2468 // from the CSE maps.
2469 RemoveNodeFromCSEMaps(User);
2471 // Update all operands that match "From".
2472 for (; Op != E; ++Op) {
2474 From.Val->removeUser(User);
2476 To.Val->addUser(User);
2480 // Now that we have modified User, add it back to the CSE maps. If it
2481 // already exists there, recursively merge the results together.
2482 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2483 unsigned NumDeleted = Deleted.size();
2484 ReplaceAllUsesWith(User, Existing, &Deleted);
2486 // User is now dead.
2487 Deleted.push_back(User);
2488 DeleteNodeNotInCSEMaps(User);
2490 // We have to be careful here, because ReplaceAllUsesWith could have
2491 // deleted a user of From, which means there may be dangling pointers
2492 // in the "Users" setvector. Scan over the deleted node pointers and
2493 // remove them from the setvector.
2494 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2495 Users.remove(Deleted[i]);
2497 break; // Exit the operand scanning loop.
2504 //===----------------------------------------------------------------------===//
2506 //===----------------------------------------------------------------------===//
2509 /// getValueTypeList - Return a pointer to the specified value type.
2511 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2512 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2517 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2518 /// indicated value. This method ignores uses of other values defined by this
2520 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
2521 assert(Value < getNumValues() && "Bad value!");
2523 // If there is only one value, this is easy.
2524 if (getNumValues() == 1)
2525 return use_size() == NUses;
2526 if (Uses.size() < NUses) return false;
2528 SDOperand TheValue(const_cast<SDNode *>(this), Value);
2530 std::set<SDNode*> UsersHandled;
2532 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
2535 if (User->getNumOperands() == 1 ||
2536 UsersHandled.insert(User).second) // First time we've seen this?
2537 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2538 if (User->getOperand(i) == TheValue) {
2540 return false; // too many uses
2545 // Found exactly the right number of uses?
2550 // isOnlyUse - Return true if this node is the only use of N.
2551 bool SDNode::isOnlyUse(SDNode *N) const {
2553 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2564 // isOperand - Return true if this node is an operand of N.
2565 bool SDOperand::isOperand(SDNode *N) const {
2566 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2567 if (*this == N->getOperand(i))
2572 bool SDNode::isOperand(SDNode *N) const {
2573 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2574 if (this == N->OperandList[i].Val)
2579 const char *SDNode::getOperationName(const SelectionDAG *G) const {
2580 switch (getOpcode()) {
2582 if (getOpcode() < ISD::BUILTIN_OP_END)
2583 return "<<Unknown DAG Node>>";
2586 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
2587 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2588 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
2590 TargetLowering &TLI = G->getTargetLoweringInfo();
2592 TLI.getTargetNodeName(getOpcode());
2593 if (Name) return Name;
2596 return "<<Unknown Target Node>>";
2599 case ISD::PCMARKER: return "PCMarker";
2600 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
2601 case ISD::SRCVALUE: return "SrcValue";
2602 case ISD::EntryToken: return "EntryToken";
2603 case ISD::TokenFactor: return "TokenFactor";
2604 case ISD::AssertSext: return "AssertSext";
2605 case ISD::AssertZext: return "AssertZext";
2607 case ISD::STRING: return "String";
2608 case ISD::BasicBlock: return "BasicBlock";
2609 case ISD::VALUETYPE: return "ValueType";
2610 case ISD::Register: return "Register";
2612 case ISD::Constant: return "Constant";
2613 case ISD::ConstantFP: return "ConstantFP";
2614 case ISD::GlobalAddress: return "GlobalAddress";
2615 case ISD::FrameIndex: return "FrameIndex";
2616 case ISD::ConstantPool: return "ConstantPool";
2617 case ISD::ExternalSymbol: return "ExternalSymbol";
2619 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
2620 case ISD::TargetConstant: return "TargetConstant";
2621 case ISD::TargetConstantFP:return "TargetConstantFP";
2622 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2623 case ISD::TargetFrameIndex: return "TargetFrameIndex";
2624 case ISD::TargetConstantPool: return "TargetConstantPool";
2625 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
2627 case ISD::CopyToReg: return "CopyToReg";
2628 case ISD::CopyFromReg: return "CopyFromReg";
2629 case ISD::UNDEF: return "undef";
2630 case ISD::MERGE_VALUES: return "mergevalues";
2631 case ISD::INLINEASM: return "inlineasm";
2632 case ISD::HANDLENODE: return "handlenode";
2635 case ISD::FABS: return "fabs";
2636 case ISD::FNEG: return "fneg";
2637 case ISD::FSQRT: return "fsqrt";
2638 case ISD::FSIN: return "fsin";
2639 case ISD::FCOS: return "fcos";
2642 case ISD::ADD: return "add";
2643 case ISD::SUB: return "sub";
2644 case ISD::MUL: return "mul";
2645 case ISD::MULHU: return "mulhu";
2646 case ISD::MULHS: return "mulhs";
2647 case ISD::SDIV: return "sdiv";
2648 case ISD::UDIV: return "udiv";
2649 case ISD::SREM: return "srem";
2650 case ISD::UREM: return "urem";
2651 case ISD::AND: return "and";
2652 case ISD::OR: return "or";
2653 case ISD::XOR: return "xor";
2654 case ISD::SHL: return "shl";
2655 case ISD::SRA: return "sra";
2656 case ISD::SRL: return "srl";
2657 case ISD::ROTL: return "rotl";
2658 case ISD::ROTR: return "rotr";
2659 case ISD::FADD: return "fadd";
2660 case ISD::FSUB: return "fsub";
2661 case ISD::FMUL: return "fmul";
2662 case ISD::FDIV: return "fdiv";
2663 case ISD::FREM: return "frem";
2664 case ISD::FCOPYSIGN: return "fcopysign";
2665 case ISD::VADD: return "vadd";
2666 case ISD::VSUB: return "vsub";
2667 case ISD::VMUL: return "vmul";
2669 case ISD::SETCC: return "setcc";
2670 case ISD::SELECT: return "select";
2671 case ISD::SELECT_CC: return "select_cc";
2672 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2673 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2674 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
2675 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
2676 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2677 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2678 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2679 case ISD::ADDC: return "addc";
2680 case ISD::ADDE: return "adde";
2681 case ISD::SUBC: return "subc";
2682 case ISD::SUBE: return "sube";
2683 case ISD::SHL_PARTS: return "shl_parts";
2684 case ISD::SRA_PARTS: return "sra_parts";
2685 case ISD::SRL_PARTS: return "srl_parts";
2687 // Conversion operators.
2688 case ISD::SIGN_EXTEND: return "sign_extend";
2689 case ISD::ZERO_EXTEND: return "zero_extend";
2690 case ISD::ANY_EXTEND: return "any_extend";
2691 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
2692 case ISD::TRUNCATE: return "truncate";
2693 case ISD::FP_ROUND: return "fp_round";
2694 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
2695 case ISD::FP_EXTEND: return "fp_extend";
2697 case ISD::SINT_TO_FP: return "sint_to_fp";
2698 case ISD::UINT_TO_FP: return "uint_to_fp";
2699 case ISD::FP_TO_SINT: return "fp_to_sint";
2700 case ISD::FP_TO_UINT: return "fp_to_uint";
2701 case ISD::BIT_CONVERT: return "bit_convert";
2703 // Control flow instructions
2704 case ISD::BR: return "br";
2705 case ISD::BRCOND: return "brcond";
2706 case ISD::BR_CC: return "br_cc";
2707 case ISD::RET: return "ret";
2708 case ISD::CALLSEQ_START: return "callseq_start";
2709 case ISD::CALLSEQ_END: return "callseq_end";
2712 case ISD::LOAD: return "load";
2713 case ISD::STORE: return "store";
2714 case ISD::VLOAD: return "vload";
2715 case ISD::EXTLOAD: return "extload";
2716 case ISD::SEXTLOAD: return "sextload";
2717 case ISD::ZEXTLOAD: return "zextload";
2718 case ISD::TRUNCSTORE: return "truncstore";
2719 case ISD::VAARG: return "vaarg";
2720 case ISD::VACOPY: return "vacopy";
2721 case ISD::VAEND: return "vaend";
2722 case ISD::VASTART: return "vastart";
2723 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2724 case ISD::EXTRACT_ELEMENT: return "extract_element";
2725 case ISD::BUILD_PAIR: return "build_pair";
2726 case ISD::STACKSAVE: return "stacksave";
2727 case ISD::STACKRESTORE: return "stackrestore";
2729 // Block memory operations.
2730 case ISD::MEMSET: return "memset";
2731 case ISD::MEMCPY: return "memcpy";
2732 case ISD::MEMMOVE: return "memmove";
2735 case ISD::BSWAP: return "bswap";
2736 case ISD::CTPOP: return "ctpop";
2737 case ISD::CTTZ: return "cttz";
2738 case ISD::CTLZ: return "ctlz";
2741 case ISD::LOCATION: return "location";
2742 case ISD::DEBUG_LOC: return "debug_loc";
2743 case ISD::DEBUG_LABEL: return "debug_label";
2746 switch (cast<CondCodeSDNode>(this)->get()) {
2747 default: assert(0 && "Unknown setcc condition!");
2748 case ISD::SETOEQ: return "setoeq";
2749 case ISD::SETOGT: return "setogt";
2750 case ISD::SETOGE: return "setoge";
2751 case ISD::SETOLT: return "setolt";
2752 case ISD::SETOLE: return "setole";
2753 case ISD::SETONE: return "setone";
2755 case ISD::SETO: return "seto";
2756 case ISD::SETUO: return "setuo";
2757 case ISD::SETUEQ: return "setue";
2758 case ISD::SETUGT: return "setugt";
2759 case ISD::SETUGE: return "setuge";
2760 case ISD::SETULT: return "setult";
2761 case ISD::SETULE: return "setule";
2762 case ISD::SETUNE: return "setune";
2764 case ISD::SETEQ: return "seteq";
2765 case ISD::SETGT: return "setgt";
2766 case ISD::SETGE: return "setge";
2767 case ISD::SETLT: return "setlt";
2768 case ISD::SETLE: return "setle";
2769 case ISD::SETNE: return "setne";
2774 void SDNode::dump() const { dump(0); }
2775 void SDNode::dump(const SelectionDAG *G) const {
2776 std::cerr << (void*)this << ": ";
2778 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2779 if (i) std::cerr << ",";
2780 if (getValueType(i) == MVT::Other)
2783 std::cerr << MVT::getValueTypeString(getValueType(i));
2785 std::cerr << " = " << getOperationName(G);
2788 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2789 if (i) std::cerr << ", ";
2790 std::cerr << (void*)getOperand(i).Val;
2791 if (unsigned RN = getOperand(i).ResNo)
2792 std::cerr << ":" << RN;
2795 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2796 std::cerr << "<" << CSDN->getValue() << ">";
2797 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2798 std::cerr << "<" << CSDN->getValue() << ">";
2799 } else if (const GlobalAddressSDNode *GADN =
2800 dyn_cast<GlobalAddressSDNode>(this)) {
2801 int offset = GADN->getOffset();
2803 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
2805 std::cerr << " + " << offset;
2807 std::cerr << " " << offset;
2808 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
2809 std::cerr << "<" << FIDN->getIndex() << ">";
2810 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
2811 int offset = CP->getOffset();
2812 std::cerr << "<" << *CP->get() << ">";
2814 std::cerr << " + " << offset;
2816 std::cerr << " " << offset;
2817 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
2819 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2821 std::cerr << LBB->getName() << " ";
2822 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
2823 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
2824 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2825 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2827 std::cerr << " #" << R->getReg();
2829 } else if (const ExternalSymbolSDNode *ES =
2830 dyn_cast<ExternalSymbolSDNode>(this)) {
2831 std::cerr << "'" << ES->getSymbol() << "'";
2832 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2834 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2836 std::cerr << "<null:" << M->getOffset() << ">";
2837 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2838 std::cerr << ":" << getValueTypeString(N->getVT());
2842 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
2843 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2844 if (N->getOperand(i).Val->hasOneUse())
2845 DumpNodes(N->getOperand(i).Val, indent+2, G);
2847 std::cerr << "\n" << std::string(indent+2, ' ')
2848 << (void*)N->getOperand(i).Val << ": <multiple use>";
2851 std::cerr << "\n" << std::string(indent, ' ');
2855 void SelectionDAG::dump() const {
2856 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
2857 std::vector<const SDNode*> Nodes;
2858 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2862 std::sort(Nodes.begin(), Nodes.end());
2864 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2865 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
2866 DumpNodes(Nodes[i], 2, this);
2869 DumpNodes(getRoot().Val, 2, this);
2871 std::cerr << "\n\n";
2874 /// InsertISelMapEntry - A helper function to insert a key / element pair
2875 /// into a SDOperand to SDOperand map. This is added to avoid the map
2876 /// insertion operator from being inlined.
2877 void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
2878 SDNode *Key, unsigned KeyResNo,
2879 SDNode *Element, unsigned ElementResNo) {
2880 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
2881 SDOperand(Element, ElementResNo)));