1 //===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements the SelectionDAG class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/SelectionDAG.h"
15 #include "llvm/Constants.h"
16 #include "llvm/GlobalValue.h"
17 #include "llvm/Assembly/Writer.h"
18 #include "llvm/CodeGen/MachineBasicBlock.h"
19 #include "llvm/Support/MathExtras.h"
20 #include "llvm/Target/MRegisterInfo.h"
21 #include "llvm/Target/TargetLowering.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/ADT/StringExtras.h"
31 static bool isCommutativeBinOp(unsigned Opcode) {
41 case ISD::XOR: return true;
42 default: return false; // FIXME: Need commutative info for user ops!
46 static bool isAssociativeBinOp(unsigned Opcode) {
52 case ISD::XOR: return true;
53 default: return false; // FIXME: Need associative info for user ops!
57 // isInvertibleForFree - Return true if there is no cost to emitting the logical
58 // inverse of this node.
59 static bool isInvertibleForFree(SDOperand N) {
60 if (isa<ConstantSDNode>(N.Val)) return true;
61 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
66 //===----------------------------------------------------------------------===//
67 // ConstantFPSDNode Class
68 //===----------------------------------------------------------------------===//
70 /// isExactlyValue - We don't rely on operator== working on double values, as
71 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
72 /// As such, this method can be used to do an exact bit-for-bit comparison of
73 /// two floating point values.
74 bool ConstantFPSDNode::isExactlyValue(double V) const {
75 return DoubleToBits(V) == DoubleToBits(Value);
78 //===----------------------------------------------------------------------===//
80 //===----------------------------------------------------------------------===//
82 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
83 /// when given the operation for (X op Y).
84 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
85 // To perform this operation, we just need to swap the L and G bits of the
87 unsigned OldL = (Operation >> 2) & 1;
88 unsigned OldG = (Operation >> 1) & 1;
89 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
90 (OldL << 1) | // New G bit
91 (OldG << 2)); // New L bit.
94 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
95 /// 'op' is a valid SetCC operation.
96 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
97 unsigned Operation = Op;
99 Operation ^= 7; // Flip L, G, E bits, but not U.
101 Operation ^= 15; // Flip all of the condition bits.
102 if (Operation > ISD::SETTRUE2)
103 Operation &= ~8; // Don't let N and U bits get set.
104 return ISD::CondCode(Operation);
108 /// isSignedOp - For an integer comparison, return 1 if the comparison is a
109 /// signed operation and 2 if the result is an unsigned comparison. Return zero
110 /// if the operation does not depend on the sign of the input (setne and seteq).
111 static int isSignedOp(ISD::CondCode Opcode) {
113 default: assert(0 && "Illegal integer setcc operation!");
115 case ISD::SETNE: return 0;
119 case ISD::SETGE: return 1;
123 case ISD::SETUGE: return 2;
127 /// getSetCCOrOperation - Return the result of a logical OR between different
128 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
129 /// returns SETCC_INVALID if it is not possible to represent the resultant
131 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
133 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
134 // Cannot fold a signed integer setcc with an unsigned integer setcc.
135 return ISD::SETCC_INVALID;
137 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
139 // If the N and U bits get set then the resultant comparison DOES suddenly
140 // care about orderedness, and is true when ordered.
141 if (Op > ISD::SETTRUE2)
142 Op &= ~16; // Clear the N bit.
143 return ISD::CondCode(Op);
146 /// getSetCCAndOperation - Return the result of a logical AND between different
147 /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
148 /// function returns zero if it is not possible to represent the resultant
150 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
152 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
153 // Cannot fold a signed setcc with an unsigned setcc.
154 return ISD::SETCC_INVALID;
156 // Combine all of the condition bits.
157 return ISD::CondCode(Op1 & Op2);
160 const TargetMachine &SelectionDAG::getTarget() const {
161 return TLI.getTargetMachine();
164 //===----------------------------------------------------------------------===//
165 // SelectionDAG Class
166 //===----------------------------------------------------------------------===//
168 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
169 /// SelectionDAG, including nodes (like loads) that have uses of their token
170 /// chain but no other uses and no side effect. If a node is passed in as an
171 /// argument, it is used as the seed for node deletion.
172 void SelectionDAG::RemoveDeadNodes(SDNode *N) {
173 // Create a dummy node (which is not added to allnodes), that adds a reference
174 // to the root node, preventing it from being deleted.
175 HandleSDNode Dummy(getRoot());
177 bool MadeChange = false;
179 // If we have a hint to start from, use it.
180 if (N && N->use_empty()) {
185 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
186 if (I->use_empty() && I->getOpcode() != 65535) {
187 // Node is dead, recursively delete newly dead uses.
192 // Walk the nodes list, removing the nodes we've marked as dead.
194 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
201 // If the root changed (e.g. it was a dead load, update the root).
202 setRoot(Dummy.getValue());
205 /// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
206 /// graph. If it is the last user of any of its operands, recursively process
207 /// them the same way.
209 void SelectionDAG::DestroyDeadNode(SDNode *N) {
210 // Okay, we really are going to delete this node. First take this out of the
211 // appropriate CSE map.
212 RemoveNodeFromCSEMaps(N);
214 // Next, brutally remove the operand list. This is safe to do, as there are
215 // no cycles in the graph.
216 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
220 // Now that we removed this operand, see if there are no uses of it left.
224 delete[] N->OperandList;
228 // Mark the node as dead.
229 N->MorphNodeTo(65535);
232 void SelectionDAG::DeleteNode(SDNode *N) {
233 assert(N->use_empty() && "Cannot delete a node that is not dead!");
235 // First take this out of the appropriate CSE map.
236 RemoveNodeFromCSEMaps(N);
238 // Finally, remove uses due to operands of this node, remove from the
239 // AllNodes list, and delete the node.
240 DeleteNodeNotInCSEMaps(N);
243 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
245 // Remove it from the AllNodes list.
248 // Drop all of the operands and decrement used nodes use counts.
249 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
250 I->Val->removeUser(N);
251 delete[] N->OperandList;
258 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
259 /// correspond to it. This is useful when we're about to delete or repurpose
260 /// the node. We don't want future request for structurally identical nodes
261 /// to return N anymore.
262 void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
264 switch (N->getOpcode()) {
265 case ISD::HANDLENODE: return; // noop.
267 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
268 N->getValueType(0)));
270 case ISD::TargetConstant:
271 Erased = TargetConstants.erase(std::make_pair(
272 cast<ConstantSDNode>(N)->getValue(),
273 N->getValueType(0)));
275 case ISD::ConstantFP: {
276 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
277 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
281 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
284 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
285 "Cond code doesn't exist!");
286 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
287 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
289 case ISD::GlobalAddress: {
290 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
291 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
295 case ISD::TargetGlobalAddress: {
296 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
297 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
301 case ISD::FrameIndex:
302 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
304 case ISD::TargetFrameIndex:
305 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
307 case ISD::ConstantPool:
308 Erased = ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
310 case ISD::TargetConstantPool:
311 Erased =TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
313 case ISD::BasicBlock:
314 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
316 case ISD::ExternalSymbol:
317 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
319 case ISD::TargetExternalSymbol:
320 Erased = TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
323 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
324 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
327 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
328 N->getValueType(0)));
330 case ISD::SRCVALUE: {
331 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
332 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
336 Erased = Loads.erase(std::make_pair(N->getOperand(1),
337 std::make_pair(N->getOperand(0),
338 N->getValueType(0))));
341 if (N->getNumValues() == 1) {
342 if (N->getNumOperands() == 0) {
343 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
344 N->getValueType(0)));
345 } else if (N->getNumOperands() == 1) {
347 UnaryOps.erase(std::make_pair(N->getOpcode(),
348 std::make_pair(N->getOperand(0),
349 N->getValueType(0))));
350 } else if (N->getNumOperands() == 2) {
352 BinaryOps.erase(std::make_pair(N->getOpcode(),
353 std::make_pair(N->getOperand(0),
356 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
358 OneResultNodes.erase(std::make_pair(N->getOpcode(),
359 std::make_pair(N->getValueType(0),
363 // Remove the node from the ArbitraryNodes map.
364 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
365 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
367 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
368 std::make_pair(RV, Ops)));
373 // Verify that the node was actually in one of the CSE maps, unless it has a
374 // flag result (which cannot be CSE'd) or is one of the special cases that are
375 // not subject to CSE.
376 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
377 N->getOpcode() != ISD::CALL && N->getOpcode() != ISD::CALLSEQ_START &&
378 N->getOpcode() != ISD::CALLSEQ_END && !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::CALLSEQ_START ||
394 N->getOpcode() == ISD::CALLSEQ_END ||
395 N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
396 return 0; // Never add these nodes.
398 // Check that remaining values produced are not flags.
399 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
400 if (N->getValueType(i) == MVT::Flag)
401 return 0; // Never CSE anything that produces a flag.
403 if (N->getNumValues() == 1) {
404 if (N->getNumOperands() == 1) {
405 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
406 std::make_pair(N->getOperand(0),
407 N->getValueType(0)))];
410 } else if (N->getNumOperands() == 2) {
411 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
412 std::make_pair(N->getOperand(0),
417 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
418 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
419 std::make_pair(N->getValueType(0), Ops))];
424 if (N->getOpcode() == ISD::LOAD) {
425 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
426 std::make_pair(N->getOperand(0),
427 N->getValueType(0)))];
431 // Remove the node from the ArbitraryNodes map.
432 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
433 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
434 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
435 std::make_pair(RV, Ops))];
445 SelectionDAG::~SelectionDAG() {
446 while (!AllNodes.empty()) {
447 SDNode *N = AllNodes.begin();
448 delete [] N->OperandList;
451 AllNodes.pop_front();
455 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
456 if (Op.getValueType() == VT) return Op;
457 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
458 return getNode(ISD::AND, Op.getValueType(), Op,
459 getConstant(Imm, Op.getValueType()));
462 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
463 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
464 // Mask out any bits that are not valid for this constant.
466 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
468 SDNode *&N = Constants[std::make_pair(Val, VT)];
469 if (N) return SDOperand(N, 0);
470 N = new ConstantSDNode(false, Val, VT);
471 AllNodes.push_back(N);
472 return SDOperand(N, 0);
475 SDOperand SelectionDAG::getString(const std::string &Val) {
476 StringSDNode *&N = StringNodes[Val];
478 N = new StringSDNode(Val);
479 AllNodes.push_back(N);
481 return SDOperand(N, 0);
484 SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
485 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
486 // Mask out any bits that are not valid for this constant.
488 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
490 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
491 if (N) return SDOperand(N, 0);
492 N = new ConstantSDNode(true, Val, VT);
493 AllNodes.push_back(N);
494 return SDOperand(N, 0);
497 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
498 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
500 Val = (float)Val; // Mask out extra precision.
502 // Do the map lookup using the actual bit pattern for the floating point
503 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
504 // we don't have issues with SNANs.
505 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
506 if (N) return SDOperand(N, 0);
507 N = new ConstantFPSDNode(Val, VT);
508 AllNodes.push_back(N);
509 return SDOperand(N, 0);
512 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
513 MVT::ValueType VT, int offset) {
514 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
515 if (N) return SDOperand(N, 0);
516 N = new GlobalAddressSDNode(false, GV, VT, offset);
517 AllNodes.push_back(N);
518 return SDOperand(N, 0);
521 SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
522 MVT::ValueType VT, int offset) {
523 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
524 if (N) return SDOperand(N, 0);
525 N = new GlobalAddressSDNode(true, GV, VT, offset);
526 AllNodes.push_back(N);
527 return SDOperand(N, 0);
530 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
531 SDNode *&N = FrameIndices[FI];
532 if (N) return SDOperand(N, 0);
533 N = new FrameIndexSDNode(FI, VT, false);
534 AllNodes.push_back(N);
535 return SDOperand(N, 0);
538 SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
539 SDNode *&N = TargetFrameIndices[FI];
540 if (N) return SDOperand(N, 0);
541 N = new FrameIndexSDNode(FI, VT, true);
542 AllNodes.push_back(N);
543 return SDOperand(N, 0);
546 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
547 SDNode *&N = ConstantPoolIndices[C];
548 if (N) return SDOperand(N, 0);
549 N = new ConstantPoolSDNode(C, VT, false);
550 AllNodes.push_back(N);
551 return SDOperand(N, 0);
554 SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
555 SDNode *&N = TargetConstantPoolIndices[C];
556 if (N) return SDOperand(N, 0);
557 N = new ConstantPoolSDNode(C, VT, true);
558 AllNodes.push_back(N);
559 return SDOperand(N, 0);
562 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
563 SDNode *&N = BBNodes[MBB];
564 if (N) return SDOperand(N, 0);
565 N = new BasicBlockSDNode(MBB);
566 AllNodes.push_back(N);
567 return SDOperand(N, 0);
570 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
571 if ((unsigned)VT >= ValueTypeNodes.size())
572 ValueTypeNodes.resize(VT+1);
573 if (ValueTypeNodes[VT] == 0) {
574 ValueTypeNodes[VT] = new VTSDNode(VT);
575 AllNodes.push_back(ValueTypeNodes[VT]);
578 return SDOperand(ValueTypeNodes[VT], 0);
581 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
582 SDNode *&N = ExternalSymbols[Sym];
583 if (N) return SDOperand(N, 0);
584 N = new ExternalSymbolSDNode(false, Sym, VT);
585 AllNodes.push_back(N);
586 return SDOperand(N, 0);
589 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT::ValueType VT) {
590 SDNode *&N = TargetExternalSymbols[Sym];
591 if (N) return SDOperand(N, 0);
592 N = new ExternalSymbolSDNode(true, Sym, VT);
593 AllNodes.push_back(N);
594 return SDOperand(N, 0);
597 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
598 if ((unsigned)Cond >= CondCodeNodes.size())
599 CondCodeNodes.resize(Cond+1);
601 if (CondCodeNodes[Cond] == 0) {
602 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
603 AllNodes.push_back(CondCodeNodes[Cond]);
605 return SDOperand(CondCodeNodes[Cond], 0);
608 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
609 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
611 Reg = new RegisterSDNode(RegNo, VT);
612 AllNodes.push_back(Reg);
614 return SDOperand(Reg, 0);
617 SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
618 SDOperand N2, ISD::CondCode Cond) {
619 // These setcc operations always fold.
623 case ISD::SETFALSE2: return getConstant(0, VT);
625 case ISD::SETTRUE2: return getConstant(1, VT);
628 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
629 uint64_t C2 = N2C->getValue();
630 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
631 uint64_t C1 = N1C->getValue();
633 // Sign extend the operands if required
634 if (ISD::isSignedIntSetCC(Cond)) {
635 C1 = N1C->getSignExtended();
636 C2 = N2C->getSignExtended();
640 default: assert(0 && "Unknown integer setcc!");
641 case ISD::SETEQ: return getConstant(C1 == C2, VT);
642 case ISD::SETNE: return getConstant(C1 != C2, VT);
643 case ISD::SETULT: return getConstant(C1 < C2, VT);
644 case ISD::SETUGT: return getConstant(C1 > C2, VT);
645 case ISD::SETULE: return getConstant(C1 <= C2, VT);
646 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
647 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
648 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
649 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
650 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
653 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
654 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
655 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
657 // If the comparison constant has bits in the upper part, the
658 // zero-extended value could never match.
659 if (C2 & (~0ULL << InSize)) {
660 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
664 case ISD::SETEQ: return getConstant(0, VT);
667 case ISD::SETNE: return getConstant(1, VT);
670 // True if the sign bit of C2 is set.
671 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
674 // True if the sign bit of C2 isn't set.
675 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
681 // Otherwise, we can perform the comparison with the low bits.
689 return getSetCC(VT, N1.getOperand(0),
690 getConstant(C2, N1.getOperand(0).getValueType()),
693 break; // todo, be more careful with signed comparisons
695 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
696 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
697 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
698 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
699 MVT::ValueType ExtDstTy = N1.getValueType();
700 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
702 // If the extended part has any inconsistent bits, it cannot ever
703 // compare equal. In other words, they have to be all ones or all
706 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
707 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
708 return getConstant(Cond == ISD::SETNE, VT);
710 // Otherwise, make this a use of a zext.
711 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
712 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
716 uint64_t MinVal, MaxVal;
717 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
718 if (ISD::isSignedIntSetCC(Cond)) {
719 MinVal = 1ULL << (OperandBitSize-1);
720 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
721 MaxVal = ~0ULL >> (65-OperandBitSize);
726 MaxVal = ~0ULL >> (64-OperandBitSize);
729 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
730 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
731 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
732 --C2; // X >= C1 --> X > (C1-1)
733 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
734 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
737 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
738 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
739 ++C2; // X <= C1 --> X < (C1+1)
740 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
741 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
744 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
745 return getConstant(0, VT); // X < MIN --> false
747 // Canonicalize setgt X, Min --> setne X, Min
748 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
749 return getSetCC(VT, N1, N2, ISD::SETNE);
751 // If we have setult X, 1, turn it into seteq X, 0
752 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
753 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
755 // If we have setugt X, Max-1, turn it into seteq X, Max
756 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
757 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
760 // If we have "setcc X, C1", check to see if we can shrink the immediate
763 // SETUGT X, SINTMAX -> SETLT X, 0
764 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
765 C2 == (~0ULL >> (65-OperandBitSize)))
766 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
768 // FIXME: Implement the rest of these.
771 // Fold bit comparisons when we can.
772 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
773 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
774 if (ConstantSDNode *AndRHS =
775 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
776 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
777 // Perform the xform if the AND RHS is a single bit.
778 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
779 return getNode(ISD::SRL, VT, N1,
780 getConstant(Log2_64(AndRHS->getValue()),
781 TLI.getShiftAmountTy()));
783 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
784 // (X & 8) == 8 --> (X & 8) >> 3
785 // Perform the xform if C2 is a single bit.
786 if ((C2 & (C2-1)) == 0) {
787 return getNode(ISD::SRL, VT, N1,
788 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
793 } else if (isa<ConstantSDNode>(N1.Val)) {
794 // Ensure that the constant occurs on the RHS.
795 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
798 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
799 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
800 double C1 = N1C->getValue(), C2 = N2C->getValue();
803 default: break; // FIXME: Implement the rest of these!
804 case ISD::SETEQ: return getConstant(C1 == C2, VT);
805 case ISD::SETNE: return getConstant(C1 != C2, VT);
806 case ISD::SETLT: return getConstant(C1 < C2, VT);
807 case ISD::SETGT: return getConstant(C1 > C2, VT);
808 case ISD::SETLE: return getConstant(C1 <= C2, VT);
809 case ISD::SETGE: return getConstant(C1 >= C2, VT);
812 // Ensure that the constant occurs on the RHS.
813 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
816 // Could not fold it.
820 /// getNode - Gets or creates the specified node.
822 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
823 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
825 N = new SDNode(Opcode, VT);
826 AllNodes.push_back(N);
828 return SDOperand(N, 0);
831 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
834 // Constant fold unary operations with an integer constant operand.
835 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
836 uint64_t Val = C->getValue();
839 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
840 case ISD::ANY_EXTEND:
841 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
842 case ISD::TRUNCATE: return getConstant(Val, VT);
843 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
844 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
845 case ISD::BIT_CONVERT:
846 if (VT == MVT::f32) {
847 assert(C->getValueType(0) == MVT::i32 && "Invalid bit_convert!");
848 return getConstantFP(BitsToFloat(Val), VT);
849 } else if (VT == MVT::f64) {
850 assert(C->getValueType(0) == MVT::i64 && "Invalid bit_convert!");
851 return getConstantFP(BitsToDouble(Val), VT);
856 default: assert(0 && "Invalid bswap!"); break;
857 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
858 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
859 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
864 default: assert(0 && "Invalid ctpop!"); break;
865 case MVT::i1: return getConstant(Val != 0, VT);
867 Tmp1 = (unsigned)Val & 0xFF;
868 return getConstant(CountPopulation_32(Tmp1), VT);
870 Tmp1 = (unsigned)Val & 0xFFFF;
871 return getConstant(CountPopulation_32(Tmp1), VT);
873 return getConstant(CountPopulation_32((unsigned)Val), VT);
875 return getConstant(CountPopulation_64(Val), VT);
879 default: assert(0 && "Invalid ctlz!"); break;
880 case MVT::i1: return getConstant(Val == 0, VT);
882 Tmp1 = (unsigned)Val & 0xFF;
883 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
885 Tmp1 = (unsigned)Val & 0xFFFF;
886 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
888 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
890 return getConstant(CountLeadingZeros_64(Val), VT);
894 default: assert(0 && "Invalid cttz!"); break;
895 case MVT::i1: return getConstant(Val == 0, VT);
897 Tmp1 = (unsigned)Val | 0x100;
898 return getConstant(CountTrailingZeros_32(Tmp1), VT);
900 Tmp1 = (unsigned)Val | 0x10000;
901 return getConstant(CountTrailingZeros_32(Tmp1), VT);
903 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
905 return getConstant(CountTrailingZeros_64(Val), VT);
910 // Constant fold unary operations with an floating point constant operand.
911 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
914 return getConstantFP(-C->getValue(), VT);
916 return getConstantFP(fabs(C->getValue()), VT);
919 return getConstantFP(C->getValue(), VT);
920 case ISD::FP_TO_SINT:
921 return getConstant((int64_t)C->getValue(), VT);
922 case ISD::FP_TO_UINT:
923 return getConstant((uint64_t)C->getValue(), VT);
924 case ISD::BIT_CONVERT:
925 if (VT == MVT::i32) {
926 assert(C->getValueType(0) == MVT::f32 && "Invalid bit_convert!");
927 return getConstant(FloatToBits(C->getValue()), VT);
928 } else if (VT == MVT::i64) {
929 assert(C->getValueType(0) == MVT::f64 && "Invalid bit_convert!");
930 return getConstant(DoubleToBits(C->getValue()), VT);
935 unsigned OpOpcode = Operand.Val->getOpcode();
937 case ISD::TokenFactor:
938 return Operand; // Factor of one node? No factor.
939 case ISD::SIGN_EXTEND:
940 if (Operand.getValueType() == VT) return Operand; // noop extension
941 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
942 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
944 case ISD::ZERO_EXTEND:
945 if (Operand.getValueType() == VT) return Operand; // noop extension
946 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
947 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
949 case ISD::ANY_EXTEND:
950 if (Operand.getValueType() == VT) return Operand; // noop extension
951 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
952 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
953 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
956 if (Operand.getValueType() == VT) return Operand; // noop truncate
957 if (OpOpcode == ISD::TRUNCATE)
958 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
959 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
960 OpOpcode == ISD::ANY_EXTEND) {
961 // If the source is smaller than the dest, we still need an extend.
962 if (Operand.Val->getOperand(0).getValueType() < VT)
963 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
964 else if (Operand.Val->getOperand(0).getValueType() > VT)
965 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
967 return Operand.Val->getOperand(0);
970 case ISD::BIT_CONVERT:
971 // Basic sanity checking.
972 assert(MVT::getSizeInBits(VT)==MVT::getSizeInBits(Operand.getValueType()) &&
973 "Cannot BIT_CONVERT between two different types!");
974 if (VT == Operand.getValueType()) return Operand; // noop conversion.
975 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
976 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
979 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
980 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
981 Operand.Val->getOperand(0));
982 if (OpOpcode == ISD::FNEG) // --X -> X
983 return Operand.Val->getOperand(0);
986 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
987 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
992 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
993 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
994 if (E) return SDOperand(E, 0);
995 E = N = new SDNode(Opcode, Operand);
997 N = new SDNode(Opcode, Operand);
999 N->setValueTypes(VT);
1000 AllNodes.push_back(N);
1001 return SDOperand(N, 0);
1006 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1007 SDOperand N1, SDOperand N2) {
1010 case ISD::TokenFactor:
1011 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1012 N2.getValueType() == MVT::Other && "Invalid token factor!");
1021 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1028 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1035 assert(N1.getValueType() == N2.getValueType() &&
1036 N1.getValueType() == VT && "Binary operator types must match!");
1044 assert(VT == N1.getValueType() &&
1045 "Shift operators return type must be the same as their first arg");
1046 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1047 VT != MVT::i1 && "Shifts only work on integers");
1049 case ISD::FP_ROUND_INREG: {
1050 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1051 assert(VT == N1.getValueType() && "Not an inreg round!");
1052 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1053 "Cannot FP_ROUND_INREG integer types");
1054 assert(EVT <= VT && "Not rounding down!");
1057 case ISD::AssertSext:
1058 case ISD::AssertZext:
1059 case ISD::SIGN_EXTEND_INREG: {
1060 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1061 assert(VT == N1.getValueType() && "Not an inreg extend!");
1062 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1063 "Cannot *_EXTEND_INREG FP types");
1064 assert(EVT <= VT && "Not extending!");
1071 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1072 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1075 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1077 case ISD::ADD: return getConstant(C1 + C2, VT);
1078 case ISD::SUB: return getConstant(C1 - C2, VT);
1079 case ISD::MUL: return getConstant(C1 * C2, VT);
1081 if (C2) return getConstant(C1 / C2, VT);
1084 if (C2) return getConstant(C1 % C2, VT);
1087 if (C2) return getConstant(N1C->getSignExtended() /
1088 N2C->getSignExtended(), VT);
1091 if (C2) return getConstant(N1C->getSignExtended() %
1092 N2C->getSignExtended(), VT);
1094 case ISD::AND : return getConstant(C1 & C2, VT);
1095 case ISD::OR : return getConstant(C1 | C2, VT);
1096 case ISD::XOR : return getConstant(C1 ^ C2, VT);
1097 case ISD::SHL : return getConstant(C1 << C2, VT);
1098 case ISD::SRL : return getConstant(C1 >> C2, VT);
1099 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
1101 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1104 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1108 } else { // Cannonicalize constant to RHS if commutative
1109 if (isCommutativeBinOp(Opcode)) {
1110 std::swap(N1C, N2C);
1116 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1117 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1120 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1122 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1123 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1124 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1126 if (C2) return getConstantFP(C1 / C2, VT);
1129 if (C2) return getConstantFP(fmod(C1, C2), VT);
1133 } else { // Cannonicalize constant to RHS if commutative
1134 if (isCommutativeBinOp(Opcode)) {
1135 std::swap(N1CFP, N2CFP);
1141 // Finally, fold operations that do not require constants.
1143 case ISD::FP_ROUND_INREG:
1144 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1146 case ISD::SIGN_EXTEND_INREG: {
1147 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1148 if (EVT == VT) return N1; // Not actually extending
1152 // FIXME: figure out how to safely handle things like
1153 // int foo(int x) { return 1 << (x & 255); }
1154 // int bar() { return foo(256); }
1159 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1160 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
1161 return getNode(Opcode, VT, N1, N2.getOperand(0));
1162 else if (N2.getOpcode() == ISD::AND)
1163 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1164 // If the and is only masking out bits that cannot effect the shift,
1165 // eliminate the and.
1166 unsigned NumBits = MVT::getSizeInBits(VT);
1167 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1168 return getNode(Opcode, VT, N1, N2.getOperand(0));
1174 // Memoize this node if possible.
1176 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END &&
1178 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1179 if (BON) return SDOperand(BON, 0);
1181 BON = N = new SDNode(Opcode, N1, N2);
1183 N = new SDNode(Opcode, N1, N2);
1186 N->setValueTypes(VT);
1187 AllNodes.push_back(N);
1188 return SDOperand(N, 0);
1191 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1192 SDOperand N1, SDOperand N2, SDOperand N3) {
1193 // Perform various simplifications.
1194 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1195 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1196 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1199 // Use SimplifySetCC to simplify SETCC's.
1200 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
1201 if (Simp.Val) return Simp;
1206 if (N1C->getValue())
1207 return N2; // select true, X, Y -> X
1209 return N3; // select false, X, Y -> Y
1211 if (N2 == N3) return N2; // select C, X, X -> X
1215 if (N2C->getValue()) // Unconditional branch
1216 return getNode(ISD::BR, MVT::Other, N1, N3);
1218 return N1; // Never-taken branch
1222 std::vector<SDOperand> Ops;
1228 // Memoize node if it doesn't produce a flag.
1230 if (VT != MVT::Flag) {
1231 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1232 if (E) return SDOperand(E, 0);
1233 E = N = new SDNode(Opcode, N1, N2, N3);
1235 N = new SDNode(Opcode, N1, N2, N3);
1237 N->setValueTypes(VT);
1238 AllNodes.push_back(N);
1239 return SDOperand(N, 0);
1242 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1243 SDOperand N1, SDOperand N2, SDOperand N3,
1245 std::vector<SDOperand> Ops;
1251 return getNode(Opcode, VT, Ops);
1254 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1255 SDOperand N1, SDOperand N2, SDOperand N3,
1256 SDOperand N4, SDOperand N5) {
1257 std::vector<SDOperand> Ops;
1264 return getNode(Opcode, VT, Ops);
1267 // setAdjCallChain - This method changes the token chain of an
1268 // CALLSEQ_START/END node to be the specified operand.
1269 void SDNode::setAdjCallChain(SDOperand N) {
1270 assert(N.getValueType() == MVT::Other);
1271 assert((getOpcode() == ISD::CALLSEQ_START ||
1272 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
1274 OperandList[0].Val->removeUser(this);
1276 OperandList[0].Val->Uses.push_back(this);
1279 // setAdjCallFlag - This method changes the flag input of an
1280 // CALLSEQ_START/END node to be the specified operand.
1281 void SDNode::setAdjCallFlag(SDOperand N) {
1282 assert(N.getValueType() == MVT::Flag);
1283 assert((getOpcode() == ISD::CALLSEQ_START ||
1284 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
1286 SDOperand &FlagOp = OperandList[getNumOperands()-1];
1287 assert(FlagOp.getValueType() == MVT::Flag);
1288 FlagOp.Val->removeUser(this);
1290 FlagOp.Val->Uses.push_back(this);
1294 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1295 SDOperand Chain, SDOperand Ptr,
1297 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1298 if (N) return SDOperand(N, 0);
1299 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1301 // Loads have a token chain.
1302 setNodeValueTypes(N, VT, MVT::Other);
1303 AllNodes.push_back(N);
1304 return SDOperand(N, 0);
1307 SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1308 SDOperand Chain, SDOperand Ptr,
1310 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1311 if (N) return SDOperand(N, 0);
1312 std::vector<SDOperand> Ops;
1314 Ops.push_back(Chain);
1316 Ops.push_back(getConstant(Count, MVT::i32));
1317 Ops.push_back(getValueType(EVT));
1319 std::vector<MVT::ValueType> VTs;
1321 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1322 return getNode(ISD::VLOAD, VTs, Ops);
1325 SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1326 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1327 MVT::ValueType EVT) {
1328 std::vector<SDOperand> Ops;
1330 Ops.push_back(Chain);
1333 Ops.push_back(getValueType(EVT));
1334 std::vector<MVT::ValueType> VTs;
1336 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1337 return getNode(Opcode, VTs, Ops);
1340 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
1341 assert((!V || isa<PointerType>(V->getType())) &&
1342 "SrcValue is not a pointer?");
1343 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1344 if (N) return SDOperand(N, 0);
1346 N = new SrcValueSDNode(V, Offset);
1347 AllNodes.push_back(N);
1348 return SDOperand(N, 0);
1351 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1352 std::vector<SDOperand> &Ops) {
1353 switch (Ops.size()) {
1354 case 0: return getNode(Opcode, VT);
1355 case 1: return getNode(Opcode, VT, Ops[0]);
1356 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1357 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
1361 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
1364 case ISD::BRCONDTWOWAY:
1366 if (N1C->getValue()) // Unconditional branch to true dest.
1367 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
1368 else // Unconditional branch to false dest.
1369 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
1371 case ISD::BRTWOWAY_CC:
1372 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1373 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1374 "LHS and RHS of comparison must have same type!");
1376 case ISD::TRUNCSTORE: {
1377 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1378 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1379 #if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1380 // If this is a truncating store of a constant, convert to the desired type
1381 // and store it instead.
1382 if (isa<Constant>(Ops[0])) {
1383 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1384 if (isa<Constant>(Op))
1387 // Also for ConstantFP?
1389 if (Ops[0].getValueType() == EVT) // Normal store?
1390 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1391 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1392 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1393 "Can't do FP-INT conversion!");
1396 case ISD::SELECT_CC: {
1397 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
1398 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1399 "LHS and RHS of condition must have same type!");
1400 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1401 "True and False arms of SelectCC must have same type!");
1402 assert(Ops[2].getValueType() == VT &&
1403 "select_cc node must be of same type as true and false value!");
1407 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
1408 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1409 "LHS/RHS of comparison should match types!");
1416 if (VT != MVT::Flag) {
1418 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1419 if (E) return SDOperand(E, 0);
1420 E = N = new SDNode(Opcode, Ops);
1422 N = new SDNode(Opcode, Ops);
1424 N->setValueTypes(VT);
1425 AllNodes.push_back(N);
1426 return SDOperand(N, 0);
1429 SDOperand SelectionDAG::getNode(unsigned Opcode,
1430 std::vector<MVT::ValueType> &ResultTys,
1431 std::vector<SDOperand> &Ops) {
1432 if (ResultTys.size() == 1)
1433 return getNode(Opcode, ResultTys[0], Ops);
1438 case ISD::ZEXTLOAD: {
1439 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1440 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1441 // If they are asking for an extending load from/to the same thing, return a
1443 if (ResultTys[0] == EVT)
1444 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1445 assert(EVT < ResultTys[0] &&
1446 "Should only be an extending load, not truncating!");
1447 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1448 "Cannot sign/zero extend a FP load!");
1449 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1450 "Cannot convert from FP to Int or Int -> FP!");
1454 // FIXME: figure out how to safely handle things like
1455 // int foo(int x) { return 1 << (x & 255); }
1456 // int bar() { return foo(256); }
1458 case ISD::SRA_PARTS:
1459 case ISD::SRL_PARTS:
1460 case ISD::SHL_PARTS:
1461 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1462 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
1463 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1464 else if (N3.getOpcode() == ISD::AND)
1465 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1466 // If the and is only masking out bits that cannot effect the shift,
1467 // eliminate the and.
1468 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1469 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1470 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1476 // Memoize the node unless it returns a flag.
1478 if (ResultTys.back() != MVT::Flag) {
1480 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1481 if (E) return SDOperand(E, 0);
1482 E = N = new SDNode(Opcode, Ops);
1484 N = new SDNode(Opcode, Ops);
1486 setNodeValueTypes(N, ResultTys);
1487 AllNodes.push_back(N);
1488 return SDOperand(N, 0);
1491 void SelectionDAG::setNodeValueTypes(SDNode *N,
1492 std::vector<MVT::ValueType> &RetVals) {
1493 switch (RetVals.size()) {
1495 case 1: N->setValueTypes(RetVals[0]); return;
1496 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1500 std::list<std::vector<MVT::ValueType> >::iterator I =
1501 std::find(VTList.begin(), VTList.end(), RetVals);
1502 if (I == VTList.end()) {
1503 VTList.push_front(RetVals);
1507 N->setValueTypes(&(*I)[0], I->size());
1510 void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1511 MVT::ValueType VT2) {
1512 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1513 E = VTList.end(); I != E; ++I) {
1514 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1515 N->setValueTypes(&(*I)[0], 2);
1519 std::vector<MVT::ValueType> V;
1522 VTList.push_front(V);
1523 N->setValueTypes(&(*VTList.begin())[0], 2);
1527 /// SelectNodeTo - These are used for target selectors to *mutate* the
1528 /// specified node to have the specified return type, Target opcode, and
1529 /// operands. Note that target opcodes are stored as
1530 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1532 /// Note that SelectNodeTo returns the resultant node. If there is already a
1533 /// node of the specified opcode and operands, it returns that node instead of
1534 /// the current one.
1535 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1536 MVT::ValueType VT) {
1537 // If an identical node already exists, use it.
1538 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1539 if (ON) return SDOperand(ON, 0);
1541 RemoveNodeFromCSEMaps(N);
1543 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1544 N->setValueTypes(VT);
1546 ON = N; // Memoize the new node.
1547 return SDOperand(N, 0);
1550 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1551 MVT::ValueType VT, SDOperand Op1) {
1552 // If an identical node already exists, use it.
1553 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1554 std::make_pair(Op1, VT))];
1555 if (ON) return SDOperand(ON, 0);
1557 RemoveNodeFromCSEMaps(N);
1558 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1559 N->setValueTypes(VT);
1560 N->setOperands(Op1);
1562 ON = N; // Memoize the new node.
1563 return SDOperand(N, 0);
1566 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1567 MVT::ValueType VT, SDOperand Op1,
1569 // If an identical node already exists, use it.
1570 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1571 std::make_pair(Op1, Op2))];
1572 if (ON) return SDOperand(ON, 0);
1574 RemoveNodeFromCSEMaps(N);
1575 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1576 N->setValueTypes(VT);
1577 N->setOperands(Op1, Op2);
1579 ON = N; // Memoize the new node.
1580 return SDOperand(N, 0);
1583 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1584 MVT::ValueType VT, SDOperand Op1,
1585 SDOperand Op2, SDOperand Op3) {
1586 // If an identical node already exists, use it.
1587 std::vector<SDOperand> OpList;
1588 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1589 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1590 std::make_pair(VT, OpList))];
1591 if (ON) return SDOperand(ON, 0);
1593 RemoveNodeFromCSEMaps(N);
1594 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1595 N->setValueTypes(VT);
1596 N->setOperands(Op1, Op2, Op3);
1598 ON = N; // Memoize the new node.
1599 return SDOperand(N, 0);
1602 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1603 MVT::ValueType VT, SDOperand Op1,
1604 SDOperand Op2, SDOperand Op3,
1606 // If an identical node already exists, use it.
1607 std::vector<SDOperand> OpList;
1608 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1609 OpList.push_back(Op4);
1610 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1611 std::make_pair(VT, OpList))];
1612 if (ON) return SDOperand(ON, 0);
1614 RemoveNodeFromCSEMaps(N);
1615 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1616 N->setValueTypes(VT);
1617 N->setOperands(Op1, Op2, Op3, Op4);
1619 ON = N; // Memoize the new node.
1620 return SDOperand(N, 0);
1623 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1624 MVT::ValueType VT, SDOperand Op1,
1625 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1627 // If an identical node already exists, use it.
1628 std::vector<SDOperand> OpList;
1629 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1630 OpList.push_back(Op4); OpList.push_back(Op5);
1631 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1632 std::make_pair(VT, OpList))];
1633 if (ON) return SDOperand(ON, 0);
1635 RemoveNodeFromCSEMaps(N);
1636 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1637 N->setValueTypes(VT);
1638 N->setOperands(Op1, Op2, Op3, Op4, Op5);
1640 ON = N; // Memoize the new node.
1641 return SDOperand(N, 0);
1644 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1645 MVT::ValueType VT, SDOperand Op1,
1646 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1647 SDOperand Op5, SDOperand Op6) {
1648 // If an identical node already exists, use it.
1649 std::vector<SDOperand> OpList;
1650 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1651 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1652 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1653 std::make_pair(VT, OpList))];
1654 if (ON) return SDOperand(ON, 0);
1656 RemoveNodeFromCSEMaps(N);
1657 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1658 N->setValueTypes(VT);
1659 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
1661 ON = N; // Memoize the new node.
1662 return SDOperand(N, 0);
1665 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1666 MVT::ValueType VT, SDOperand Op1,
1667 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1668 SDOperand Op5, SDOperand Op6,
1670 // If an identical node already exists, use it.
1671 std::vector<SDOperand> OpList;
1672 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1673 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1674 OpList.push_back(Op7);
1675 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1676 std::make_pair(VT, OpList))];
1677 if (ON) return SDOperand(ON, 0);
1679 RemoveNodeFromCSEMaps(N);
1680 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1681 N->setValueTypes(VT);
1682 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
1684 ON = N; // Memoize the new node.
1685 return SDOperand(N, 0);
1687 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1688 MVT::ValueType VT, SDOperand Op1,
1689 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1690 SDOperand Op5, SDOperand Op6,
1691 SDOperand Op7, SDOperand Op8) {
1692 // If an identical node already exists, use it.
1693 std::vector<SDOperand> OpList;
1694 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1695 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1696 OpList.push_back(Op7); OpList.push_back(Op8);
1697 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1698 std::make_pair(VT, OpList))];
1699 if (ON) return SDOperand(ON, 0);
1701 RemoveNodeFromCSEMaps(N);
1702 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1703 N->setValueTypes(VT);
1704 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
1706 ON = N; // Memoize the new node.
1707 return SDOperand(N, 0);
1710 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1711 MVT::ValueType VT1, MVT::ValueType VT2,
1712 SDOperand Op1, SDOperand Op2) {
1713 // If an identical node already exists, use it.
1714 std::vector<SDOperand> OpList;
1715 OpList.push_back(Op1); OpList.push_back(Op2);
1716 std::vector<MVT::ValueType> VTList;
1717 VTList.push_back(VT1); VTList.push_back(VT2);
1718 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1719 std::make_pair(VTList, OpList))];
1720 if (ON) return SDOperand(ON, 0);
1722 RemoveNodeFromCSEMaps(N);
1723 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1724 setNodeValueTypes(N, VT1, VT2);
1725 N->setOperands(Op1, Op2);
1727 ON = N; // Memoize the new node.
1728 return SDOperand(N, 0);
1731 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1732 MVT::ValueType VT1, MVT::ValueType VT2,
1733 SDOperand Op1, SDOperand Op2,
1735 // If an identical node already exists, use it.
1736 std::vector<SDOperand> OpList;
1737 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1738 std::vector<MVT::ValueType> VTList;
1739 VTList.push_back(VT1); VTList.push_back(VT2);
1740 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1741 std::make_pair(VTList, OpList))];
1742 if (ON) return SDOperand(ON, 0);
1744 RemoveNodeFromCSEMaps(N);
1745 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1746 setNodeValueTypes(N, VT1, VT2);
1747 N->setOperands(Op1, Op2, Op3);
1749 ON = N; // Memoize the new node.
1750 return SDOperand(N, 0);
1753 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1754 MVT::ValueType VT1, MVT::ValueType VT2,
1755 SDOperand Op1, SDOperand Op2,
1756 SDOperand Op3, SDOperand Op4) {
1757 // If an identical node already exists, use it.
1758 std::vector<SDOperand> OpList;
1759 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1760 OpList.push_back(Op4);
1761 std::vector<MVT::ValueType> VTList;
1762 VTList.push_back(VT1); VTList.push_back(VT2);
1763 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1764 std::make_pair(VTList, OpList))];
1765 if (ON) return SDOperand(ON, 0);
1767 RemoveNodeFromCSEMaps(N);
1768 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1769 setNodeValueTypes(N, VT1, VT2);
1770 N->setOperands(Op1, Op2, Op3, Op4);
1772 ON = N; // Memoize the new node.
1773 return SDOperand(N, 0);
1776 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1777 MVT::ValueType VT1, MVT::ValueType VT2,
1778 SDOperand Op1, SDOperand Op2,
1779 SDOperand Op3, SDOperand Op4,
1781 // If an identical node already exists, use it.
1782 std::vector<SDOperand> OpList;
1783 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1784 OpList.push_back(Op4); OpList.push_back(Op5);
1785 std::vector<MVT::ValueType> VTList;
1786 VTList.push_back(VT1); VTList.push_back(VT2);
1787 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1788 std::make_pair(VTList, OpList))];
1789 if (ON) return SDOperand(ON, 0);
1791 RemoveNodeFromCSEMaps(N);
1792 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1793 setNodeValueTypes(N, VT1, VT2);
1794 N->setOperands(Op1, Op2, Op3, Op4, Op5);
1796 ON = N; // Memoize the new node.
1797 return SDOperand(N, 0);
1800 // ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1801 /// This can cause recursive merging of nodes in the DAG.
1803 /// This version assumes From/To have a single result value.
1805 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
1806 std::vector<SDNode*> *Deleted) {
1807 SDNode *From = FromN.Val, *To = ToN.Val;
1808 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
1809 "Cannot replace with this method!");
1810 assert(From != To && "Cannot replace uses of with self");
1812 while (!From->use_empty()) {
1813 // Process users until they are all gone.
1814 SDNode *U = *From->use_begin();
1816 // This node is about to morph, remove its old self from the CSE maps.
1817 RemoveNodeFromCSEMaps(U);
1819 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1821 if (I->Val == From) {
1822 From->removeUser(U);
1827 // Now that we have modified U, add it back to the CSE maps. If it already
1828 // exists there, recursively merge the results together.
1829 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1830 ReplaceAllUsesWith(U, Existing, Deleted);
1832 if (Deleted) Deleted->push_back(U);
1833 DeleteNodeNotInCSEMaps(U);
1838 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1839 /// This can cause recursive merging of nodes in the DAG.
1841 /// This version assumes From/To have matching types and numbers of result
1844 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
1845 std::vector<SDNode*> *Deleted) {
1846 assert(From != To && "Cannot replace uses of with self");
1847 assert(From->getNumValues() == To->getNumValues() &&
1848 "Cannot use this version of ReplaceAllUsesWith!");
1849 if (From->getNumValues() == 1) { // If possible, use the faster version.
1850 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
1854 while (!From->use_empty()) {
1855 // Process users until they are all gone.
1856 SDNode *U = *From->use_begin();
1858 // This node is about to morph, remove its old self from the CSE maps.
1859 RemoveNodeFromCSEMaps(U);
1861 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1863 if (I->Val == From) {
1864 From->removeUser(U);
1869 // Now that we have modified U, add it back to the CSE maps. If it already
1870 // exists there, recursively merge the results together.
1871 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1872 ReplaceAllUsesWith(U, Existing, Deleted);
1874 if (Deleted) Deleted->push_back(U);
1875 DeleteNodeNotInCSEMaps(U);
1880 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1881 /// This can cause recursive merging of nodes in the DAG.
1883 /// This version can replace From with any result values. To must match the
1884 /// number and types of values returned by From.
1885 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
1886 const std::vector<SDOperand> &To,
1887 std::vector<SDNode*> *Deleted) {
1888 assert(From->getNumValues() == To.size() &&
1889 "Incorrect number of values to replace with!");
1890 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
1891 // Degenerate case handled above.
1892 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
1896 while (!From->use_empty()) {
1897 // Process users until they are all gone.
1898 SDNode *U = *From->use_begin();
1900 // This node is about to morph, remove its old self from the CSE maps.
1901 RemoveNodeFromCSEMaps(U);
1903 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1905 if (I->Val == From) {
1906 const SDOperand &ToOp = To[I->ResNo];
1907 From->removeUser(U);
1909 ToOp.Val->addUser(U);
1912 // Now that we have modified U, add it back to the CSE maps. If it already
1913 // exists there, recursively merge the results together.
1914 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1915 ReplaceAllUsesWith(U, Existing, Deleted);
1917 if (Deleted) Deleted->push_back(U);
1918 DeleteNodeNotInCSEMaps(U);
1924 //===----------------------------------------------------------------------===//
1926 //===----------------------------------------------------------------------===//
1929 /// getValueTypeList - Return a pointer to the specified value type.
1931 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
1932 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
1937 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1938 /// indicated value. This method ignores uses of other values defined by this
1940 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1941 assert(Value < getNumValues() && "Bad value!");
1943 // If there is only one value, this is easy.
1944 if (getNumValues() == 1)
1945 return use_size() == NUses;
1946 if (Uses.size() < NUses) return false;
1948 SDOperand TheValue(this, Value);
1950 std::set<SDNode*> UsersHandled;
1952 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1955 if (User->getNumOperands() == 1 ||
1956 UsersHandled.insert(User).second) // First time we've seen this?
1957 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1958 if (User->getOperand(i) == TheValue) {
1960 return false; // too many uses
1965 // Found exactly the right number of uses?
1970 const char *SDNode::getOperationName(const SelectionDAG *G) const {
1971 switch (getOpcode()) {
1973 if (getOpcode() < ISD::BUILTIN_OP_END)
1974 return "<<Unknown DAG Node>>";
1977 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
1978 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
1979 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
1981 TargetLowering &TLI = G->getTargetLoweringInfo();
1983 TLI.getTargetNodeName(getOpcode());
1984 if (Name) return Name;
1987 return "<<Unknown Target Node>>";
1990 case ISD::PCMARKER: return "PCMarker";
1991 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
1992 case ISD::SRCVALUE: return "SrcValue";
1993 case ISD::VALUETYPE: return "ValueType";
1994 case ISD::STRING: return "String";
1995 case ISD::EntryToken: return "EntryToken";
1996 case ISD::TokenFactor: return "TokenFactor";
1997 case ISD::AssertSext: return "AssertSext";
1998 case ISD::AssertZext: return "AssertZext";
1999 case ISD::Constant: return "Constant";
2000 case ISD::TargetConstant: return "TargetConstant";
2001 case ISD::ConstantFP: return "ConstantFP";
2002 case ISD::ConstantVec: return "ConstantVec";
2003 case ISD::GlobalAddress: return "GlobalAddress";
2004 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2005 case ISD::FrameIndex: return "FrameIndex";
2006 case ISD::TargetFrameIndex: return "TargetFrameIndex";
2007 case ISD::BasicBlock: return "BasicBlock";
2008 case ISD::Register: return "Register";
2009 case ISD::ExternalSymbol: return "ExternalSymbol";
2010 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
2011 case ISD::ConstantPool: return "ConstantPool";
2012 case ISD::TargetConstantPool: return "TargetConstantPool";
2013 case ISD::CopyToReg: return "CopyToReg";
2014 case ISD::CopyFromReg: return "CopyFromReg";
2015 case ISD::UNDEF: return "undef";
2016 case ISD::MERGE_VALUES: return "mergevalues";
2019 case ISD::FABS: return "fabs";
2020 case ISD::FNEG: return "fneg";
2021 case ISD::FSQRT: return "fsqrt";
2022 case ISD::FSIN: return "fsin";
2023 case ISD::FCOS: return "fcos";
2026 case ISD::ADD: return "add";
2027 case ISD::SUB: return "sub";
2028 case ISD::MUL: return "mul";
2029 case ISD::MULHU: return "mulhu";
2030 case ISD::MULHS: return "mulhs";
2031 case ISD::SDIV: return "sdiv";
2032 case ISD::UDIV: return "udiv";
2033 case ISD::SREM: return "srem";
2034 case ISD::UREM: return "urem";
2035 case ISD::AND: return "and";
2036 case ISD::OR: return "or";
2037 case ISD::XOR: return "xor";
2038 case ISD::SHL: return "shl";
2039 case ISD::SRA: return "sra";
2040 case ISD::SRL: return "srl";
2041 case ISD::ROTL: return "rotl";
2042 case ISD::ROTR: return "rotr";
2043 case ISD::FADD: return "fadd";
2044 case ISD::FSUB: return "fsub";
2045 case ISD::FMUL: return "fmul";
2046 case ISD::FDIV: return "fdiv";
2047 case ISD::FREM: return "frem";
2048 case ISD::VADD: return "vadd";
2049 case ISD::VSUB: return "vsub";
2050 case ISD::VMUL: return "vmul";
2052 case ISD::SETCC: return "setcc";
2053 case ISD::SELECT: return "select";
2054 case ISD::SELECT_CC: return "select_cc";
2055 case ISD::ADD_PARTS: return "add_parts";
2056 case ISD::SUB_PARTS: return "sub_parts";
2057 case ISD::SHL_PARTS: return "shl_parts";
2058 case ISD::SRA_PARTS: return "sra_parts";
2059 case ISD::SRL_PARTS: return "srl_parts";
2061 // Conversion operators.
2062 case ISD::SIGN_EXTEND: return "sign_extend";
2063 case ISD::ZERO_EXTEND: return "zero_extend";
2064 case ISD::ANY_EXTEND: return "any_extend";
2065 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
2066 case ISD::TRUNCATE: return "truncate";
2067 case ISD::FP_ROUND: return "fp_round";
2068 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
2069 case ISD::FP_EXTEND: return "fp_extend";
2071 case ISD::SINT_TO_FP: return "sint_to_fp";
2072 case ISD::UINT_TO_FP: return "uint_to_fp";
2073 case ISD::FP_TO_SINT: return "fp_to_sint";
2074 case ISD::FP_TO_UINT: return "fp_to_uint";
2075 case ISD::BIT_CONVERT: return "bit_convert";
2077 // Control flow instructions
2078 case ISD::BR: return "br";
2079 case ISD::BRCOND: return "brcond";
2080 case ISD::BRCONDTWOWAY: return "brcondtwoway";
2081 case ISD::BR_CC: return "br_cc";
2082 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
2083 case ISD::RET: return "ret";
2084 case ISD::CALL: return "call";
2085 case ISD::TAILCALL:return "tailcall";
2086 case ISD::CALLSEQ_START: return "callseq_start";
2087 case ISD::CALLSEQ_END: return "callseq_end";
2090 case ISD::LOAD: return "load";
2091 case ISD::STORE: return "store";
2092 case ISD::VLOAD: return "vload";
2093 case ISD::EXTLOAD: return "extload";
2094 case ISD::SEXTLOAD: return "sextload";
2095 case ISD::ZEXTLOAD: return "zextload";
2096 case ISD::TRUNCSTORE: return "truncstore";
2098 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2099 case ISD::EXTRACT_ELEMENT: return "extract_element";
2100 case ISD::BUILD_PAIR: return "build_pair";
2101 case ISD::STACKSAVE: return "stacksave";
2102 case ISD::STACKRESTORE: return "stackrestore";
2104 // Block memory operations.
2105 case ISD::MEMSET: return "memset";
2106 case ISD::MEMCPY: return "memcpy";
2107 case ISD::MEMMOVE: return "memmove";
2110 case ISD::BSWAP: return "bswap";
2111 case ISD::CTPOP: return "ctpop";
2112 case ISD::CTTZ: return "cttz";
2113 case ISD::CTLZ: return "ctlz";
2116 case ISD::READPORT: return "readport";
2117 case ISD::WRITEPORT: return "writeport";
2118 case ISD::READIO: return "readio";
2119 case ISD::WRITEIO: return "writeio";
2122 case ISD::LOCATION: return "location";
2123 case ISD::DEBUG_LOC: return "debug_loc";
2124 case ISD::DEBUG_LABEL: return "debug_label";
2127 switch (cast<CondCodeSDNode>(this)->get()) {
2128 default: assert(0 && "Unknown setcc condition!");
2129 case ISD::SETOEQ: return "setoeq";
2130 case ISD::SETOGT: return "setogt";
2131 case ISD::SETOGE: return "setoge";
2132 case ISD::SETOLT: return "setolt";
2133 case ISD::SETOLE: return "setole";
2134 case ISD::SETONE: return "setone";
2136 case ISD::SETO: return "seto";
2137 case ISD::SETUO: return "setuo";
2138 case ISD::SETUEQ: return "setue";
2139 case ISD::SETUGT: return "setugt";
2140 case ISD::SETUGE: return "setuge";
2141 case ISD::SETULT: return "setult";
2142 case ISD::SETULE: return "setule";
2143 case ISD::SETUNE: return "setune";
2145 case ISD::SETEQ: return "seteq";
2146 case ISD::SETGT: return "setgt";
2147 case ISD::SETGE: return "setge";
2148 case ISD::SETLT: return "setlt";
2149 case ISD::SETLE: return "setle";
2150 case ISD::SETNE: return "setne";
2155 void SDNode::dump() const { dump(0); }
2156 void SDNode::dump(const SelectionDAG *G) const {
2157 std::cerr << (void*)this << ": ";
2159 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2160 if (i) std::cerr << ",";
2161 if (getValueType(i) == MVT::Other)
2164 std::cerr << MVT::getValueTypeString(getValueType(i));
2166 std::cerr << " = " << getOperationName(G);
2169 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2170 if (i) std::cerr << ", ";
2171 std::cerr << (void*)getOperand(i).Val;
2172 if (unsigned RN = getOperand(i).ResNo)
2173 std::cerr << ":" << RN;
2176 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2177 std::cerr << "<" << CSDN->getValue() << ">";
2178 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2179 std::cerr << "<" << CSDN->getValue() << ">";
2180 } else if (const GlobalAddressSDNode *GADN =
2181 dyn_cast<GlobalAddressSDNode>(this)) {
2182 int offset = GADN->getOffset();
2184 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
2186 std::cerr << " + " << offset;
2188 std::cerr << " " << offset;
2189 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
2190 std::cerr << "<" << FIDN->getIndex() << ">";
2191 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
2192 std::cerr << "<" << *CP->get() << ">";
2193 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
2195 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2197 std::cerr << LBB->getName() << " ";
2198 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
2199 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
2200 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2201 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2203 std::cerr << " #" << R->getReg();
2205 } else if (const ExternalSymbolSDNode *ES =
2206 dyn_cast<ExternalSymbolSDNode>(this)) {
2207 std::cerr << "'" << ES->getSymbol() << "'";
2208 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2210 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2212 std::cerr << "<null:" << M->getOffset() << ">";
2213 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2214 std::cerr << ":" << getValueTypeString(N->getVT());
2218 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
2219 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2220 if (N->getOperand(i).Val->hasOneUse())
2221 DumpNodes(N->getOperand(i).Val, indent+2, G);
2223 std::cerr << "\n" << std::string(indent+2, ' ')
2224 << (void*)N->getOperand(i).Val << ": <multiple use>";
2227 std::cerr << "\n" << std::string(indent, ' ');
2231 void SelectionDAG::dump() const {
2232 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
2233 std::vector<const SDNode*> Nodes;
2234 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2238 std::sort(Nodes.begin(), Nodes.end());
2240 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2241 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
2242 DumpNodes(Nodes[i], 2, this);
2245 DumpNodes(getRoot().Val, 2, this);
2247 std::cerr << "\n\n";