1 //===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements the SelectionDAG class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/SelectionDAG.h"
15 #include "llvm/Constants.h"
16 #include "llvm/GlobalValue.h"
17 #include "llvm/Intrinsics.h"
18 #include "llvm/Assembly/Writer.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/CodeGen/MachineConstantPool.h"
21 #include "llvm/Support/MathExtras.h"
22 #include "llvm/Target/MRegisterInfo.h"
23 #include "llvm/Target/TargetLowering.h"
24 #include "llvm/Target/TargetInstrInfo.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/ADT/SetVector.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/StringExtras.h"
35 /// makeVTList - Return an instance of the SDVTList struct initialized with the
36 /// specified members.
37 static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
38 SDVTList Res = {VTs, NumVTs};
42 // isInvertibleForFree - Return true if there is no cost to emitting the logical
43 // inverse of this node.
44 static bool isInvertibleForFree(SDOperand N) {
45 if (isa<ConstantSDNode>(N.Val)) return true;
46 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
51 //===----------------------------------------------------------------------===//
52 // ConstantFPSDNode Class
53 //===----------------------------------------------------------------------===//
55 /// isExactlyValue - We don't rely on operator== working on double values, as
56 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
57 /// As such, this method can be used to do an exact bit-for-bit comparison of
58 /// two floating point values.
59 bool ConstantFPSDNode::isExactlyValue(double V) const {
60 return DoubleToBits(V) == DoubleToBits(Value);
63 //===----------------------------------------------------------------------===//
65 //===----------------------------------------------------------------------===//
67 /// isBuildVectorAllOnes - Return true if the specified node is a
68 /// BUILD_VECTOR where all of the elements are ~0 or undef.
69 bool ISD::isBuildVectorAllOnes(const SDNode *N) {
70 // Look through a bit convert.
71 if (N->getOpcode() == ISD::BIT_CONVERT)
72 N = N->getOperand(0).Val;
74 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
76 unsigned i = 0, e = N->getNumOperands();
78 // Skip over all of the undef values.
79 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
82 // Do not accept an all-undef vector.
83 if (i == e) return false;
85 // Do not accept build_vectors that aren't all constants or which have non-~0
87 SDOperand NotZero = N->getOperand(i);
88 if (isa<ConstantSDNode>(NotZero)) {
89 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
91 } else if (isa<ConstantFPSDNode>(NotZero)) {
92 MVT::ValueType VT = NotZero.getValueType();
94 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
98 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
105 // Okay, we have at least one ~0 value, check to see if the rest match or are
107 for (++i; i != e; ++i)
108 if (N->getOperand(i) != NotZero &&
109 N->getOperand(i).getOpcode() != ISD::UNDEF)
115 /// isBuildVectorAllZeros - Return true if the specified node is a
116 /// BUILD_VECTOR where all of the elements are 0 or undef.
117 bool ISD::isBuildVectorAllZeros(const SDNode *N) {
118 // Look through a bit convert.
119 if (N->getOpcode() == ISD::BIT_CONVERT)
120 N = N->getOperand(0).Val;
122 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
124 unsigned i = 0, e = N->getNumOperands();
126 // Skip over all of the undef values.
127 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
130 // Do not accept an all-undef vector.
131 if (i == e) return false;
133 // Do not accept build_vectors that aren't all constants or which have non-~0
135 SDOperand Zero = N->getOperand(i);
136 if (isa<ConstantSDNode>(Zero)) {
137 if (!cast<ConstantSDNode>(Zero)->isNullValue())
139 } else if (isa<ConstantFPSDNode>(Zero)) {
140 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
145 // Okay, we have at least one ~0 value, check to see if the rest match or are
147 for (++i; i != e; ++i)
148 if (N->getOperand(i) != Zero &&
149 N->getOperand(i).getOpcode() != ISD::UNDEF)
154 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
155 /// when given the operation for (X op Y).
156 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
157 // To perform this operation, we just need to swap the L and G bits of the
159 unsigned OldL = (Operation >> 2) & 1;
160 unsigned OldG = (Operation >> 1) & 1;
161 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
162 (OldL << 1) | // New G bit
163 (OldG << 2)); // New L bit.
166 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
167 /// 'op' is a valid SetCC operation.
168 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
169 unsigned Operation = Op;
171 Operation ^= 7; // Flip L, G, E bits, but not U.
173 Operation ^= 15; // Flip all of the condition bits.
174 if (Operation > ISD::SETTRUE2)
175 Operation &= ~8; // Don't let N and U bits get set.
176 return ISD::CondCode(Operation);
180 /// isSignedOp - For an integer comparison, return 1 if the comparison is a
181 /// signed operation and 2 if the result is an unsigned comparison. Return zero
182 /// if the operation does not depend on the sign of the input (setne and seteq).
183 static int isSignedOp(ISD::CondCode Opcode) {
185 default: assert(0 && "Illegal integer setcc operation!");
187 case ISD::SETNE: return 0;
191 case ISD::SETGE: return 1;
195 case ISD::SETUGE: return 2;
199 /// getSetCCOrOperation - Return the result of a logical OR between different
200 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
201 /// returns SETCC_INVALID if it is not possible to represent the resultant
203 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
205 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
206 // Cannot fold a signed integer setcc with an unsigned integer setcc.
207 return ISD::SETCC_INVALID;
209 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
211 // If the N and U bits get set then the resultant comparison DOES suddenly
212 // care about orderedness, and is true when ordered.
213 if (Op > ISD::SETTRUE2)
214 Op &= ~16; // Clear the U bit if the N bit is set.
216 // Canonicalize illegal integer setcc's.
217 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
220 return ISD::CondCode(Op);
223 /// getSetCCAndOperation - Return the result of a logical AND between different
224 /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
225 /// function returns zero if it is not possible to represent the resultant
227 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
229 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
230 // Cannot fold a signed setcc with an unsigned setcc.
231 return ISD::SETCC_INVALID;
233 // Combine all of the condition bits.
234 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
236 // Canonicalize illegal integer setcc's.
240 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
241 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
242 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
243 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
250 const TargetMachine &SelectionDAG::getTarget() const {
251 return TLI.getTargetMachine();
254 //===----------------------------------------------------------------------===//
255 // SDNode Profile Support
256 //===----------------------------------------------------------------------===//
258 /// getNodeIDOpcode - Return the opcode that has been set for this NodeID.
260 static unsigned getNodeIDOpcode(FoldingSetNodeID &ID) {
261 return ID.getRawData(0);
264 /// AddNodeIDOpcode - Add the node opcode to the NodeID data.
266 static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
270 /// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
271 /// solely with their pointer.
272 void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
273 ID.AddPointer(VTList.VTs);
276 /// AddNodeIDOperand - Add an operands data to the NodeID data.
278 static void AddNodeIDOperand(FoldingSetNodeID &ID, SDOperand Op) {
279 ID.AddPointer(Op.Val);
280 ID.AddInteger(Op.ResNo);
283 /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
285 static void AddNodeIDOperands(FoldingSetNodeID &ID) {
287 static void AddNodeIDOperands(FoldingSetNodeID &ID, SDOperand Op) {
288 AddNodeIDOperand(ID, Op);
290 static void AddNodeIDOperands(FoldingSetNodeID &ID,
291 SDOperand Op1, SDOperand Op2) {
292 AddNodeIDOperand(ID, Op1);
293 AddNodeIDOperand(ID, Op2);
295 static void AddNodeIDOperands(FoldingSetNodeID &ID,
296 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
297 AddNodeIDOperand(ID, Op1);
298 AddNodeIDOperand(ID, Op2);
299 AddNodeIDOperand(ID, Op3);
301 static void AddNodeIDOperands(FoldingSetNodeID &ID,
302 const SDOperand *Ops, unsigned NumOps) {
303 for (; NumOps; --NumOps, ++Ops)
304 AddNodeIDOperand(ID, *Ops);
307 /// AddNodeIDOperands - Various routines for adding node info to the NodeID
309 static void AddNodeIDNode(FoldingSetNodeID &ID,
310 unsigned short OpC, SDVTList VTList) {
311 AddNodeIDOpcode(ID, OpC);
312 AddNodeIDValueTypes(ID, VTList);
313 AddNodeIDOperands(ID);
315 static void AddNodeIDNode(FoldingSetNodeID &ID,
316 unsigned short OpC, SDVTList VTList,
318 AddNodeIDOpcode(ID, OpC);
319 AddNodeIDValueTypes(ID, VTList);
320 AddNodeIDOperands(ID, Op);
322 static void AddNodeIDNode(FoldingSetNodeID &ID,
323 unsigned short OpC, SDVTList VTList,
324 SDOperand Op1, SDOperand Op2) {
325 AddNodeIDOpcode(ID, OpC);
326 AddNodeIDValueTypes(ID, VTList);
327 AddNodeIDOperands(ID, Op1, Op2);
329 static void AddNodeIDNode(FoldingSetNodeID &ID,
330 unsigned short OpC, SDVTList VTList,
331 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
332 AddNodeIDOpcode(ID, OpC);
333 AddNodeIDValueTypes(ID, VTList);
334 AddNodeIDOperands(ID, Op1, Op2);
336 static void AddNodeIDNode(FoldingSetNodeID &ID,
337 unsigned short OpC, SDVTList VTList,
338 const SDOperand *OpList, unsigned N) {
339 AddNodeIDOpcode(ID, OpC);
340 AddNodeIDValueTypes(ID, VTList);
341 AddNodeIDOperands(ID, OpList, N);
344 /// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
346 static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
347 AddNodeIDOpcode(ID, N->getOpcode());
348 // Add the return value info.
349 AddNodeIDValueTypes(ID, N->getVTList());
350 // Add the operand info.
351 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
353 // Handle SDNode leafs with special info.
354 if (N->getNumOperands() == 0) {
355 switch (N->getOpcode()) {
356 default: break; // Normal nodes don't need extra info.
357 case ISD::TargetConstant:
359 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
361 case ISD::TargetConstantFP:
362 case ISD::ConstantFP:
363 ID.AddDouble(cast<ConstantFPSDNode>(N)->getValue());
365 case ISD::TargetGlobalAddress:
366 case ISD::GlobalAddress:
367 ID.AddPointer(cast<GlobalAddressSDNode>(N)->getGlobal());
368 ID.AddInteger(cast<GlobalAddressSDNode>(N)->getOffset());
370 case ISD::BasicBlock:
371 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
374 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
377 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
378 ID.AddInteger(cast<SrcValueSDNode>(N)->getOffset());
380 case ISD::FrameIndex:
381 case ISD::TargetFrameIndex:
382 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
385 case ISD::TargetJumpTable:
386 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
388 case ISD::ConstantPool:
389 case ISD::TargetConstantPool:
390 ID.AddInteger(cast<ConstantPoolSDNode>(N)->getAlignment());
391 ID.AddInteger(cast<ConstantPoolSDNode>(N)->getOffset());
392 if (cast<ConstantPoolSDNode>(N)->isMachineConstantPoolEntry())
393 cast<ConstantPoolSDNode>(N)->getMachineCPVal()->
394 AddSelectionDAGCSEId(ID);
396 ID.AddPointer(cast<ConstantPoolSDNode>(N)->getConstVal());
402 //===----------------------------------------------------------------------===//
403 // SelectionDAG Class
404 //===----------------------------------------------------------------------===//
406 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
408 void SelectionDAG::RemoveDeadNodes() {
409 // Create a dummy node (which is not added to allnodes), that adds a reference
410 // to the root node, preventing it from being deleted.
411 HandleSDNode Dummy(getRoot());
413 SmallVector<SDNode*, 128> DeadNodes;
415 // Add all obviously-dead nodes to the DeadNodes worklist.
416 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
418 DeadNodes.push_back(I);
420 // Process the worklist, deleting the nodes and adding their uses to the
422 while (!DeadNodes.empty()) {
423 SDNode *N = DeadNodes.back();
424 DeadNodes.pop_back();
426 // Take the node out of the appropriate CSE map.
427 RemoveNodeFromCSEMaps(N);
429 // Next, brutally remove the operand list. This is safe to do, as there are
430 // no cycles in the graph.
431 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
432 SDNode *Operand = I->Val;
433 Operand->removeUser(N);
435 // Now that we removed this operand, see if there are no uses of it left.
436 if (Operand->use_empty())
437 DeadNodes.push_back(Operand);
439 delete[] N->OperandList;
443 // Finally, remove N itself.
447 // If the root changed (e.g. it was a dead load, update the root).
448 setRoot(Dummy.getValue());
451 void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
452 SmallVector<SDNode*, 16> DeadNodes;
453 DeadNodes.push_back(N);
455 // Process the worklist, deleting the nodes and adding their uses to the
457 while (!DeadNodes.empty()) {
458 SDNode *N = DeadNodes.back();
459 DeadNodes.pop_back();
461 // Take the node out of the appropriate CSE map.
462 RemoveNodeFromCSEMaps(N);
464 // Next, brutally remove the operand list. This is safe to do, as there are
465 // no cycles in the graph.
466 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
467 SDNode *Operand = I->Val;
468 Operand->removeUser(N);
470 // Now that we removed this operand, see if there are no uses of it left.
471 if (Operand->use_empty())
472 DeadNodes.push_back(Operand);
474 delete[] N->OperandList;
478 // Finally, remove N itself.
479 Deleted.push_back(N);
484 void SelectionDAG::DeleteNode(SDNode *N) {
485 assert(N->use_empty() && "Cannot delete a node that is not dead!");
487 // First take this out of the appropriate CSE map.
488 RemoveNodeFromCSEMaps(N);
490 // Finally, remove uses due to operands of this node, remove from the
491 // AllNodes list, and delete the node.
492 DeleteNodeNotInCSEMaps(N);
495 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
497 // Remove it from the AllNodes list.
500 // Drop all of the operands and decrement used nodes use counts.
501 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
502 I->Val->removeUser(N);
503 delete[] N->OperandList;
510 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
511 /// correspond to it. This is useful when we're about to delete or repurpose
512 /// the node. We don't want future request for structurally identical nodes
513 /// to return N anymore.
514 void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
516 switch (N->getOpcode()) {
517 case ISD::HANDLENODE: return; // noop.
519 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
522 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
523 "Cond code doesn't exist!");
524 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
525 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
527 case ISD::ExternalSymbol:
528 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
530 case ISD::TargetExternalSymbol:
532 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
535 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
536 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
539 // Remove it from the CSE Map.
540 Erased = CSEMap.RemoveNode(N);
544 // Verify that the node was actually in one of the CSE maps, unless it has a
545 // flag result (which cannot be CSE'd) or is one of the special cases that are
546 // not subject to CSE.
547 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
548 !N->isTargetOpcode()) {
551 assert(0 && "Node is not in map!");
556 /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
557 /// has been taken out and modified in some way. If the specified node already
558 /// exists in the CSE maps, do not modify the maps, but return the existing node
559 /// instead. If it doesn't exist, add it and return null.
561 SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
562 assert(N->getNumOperands() && "This is a leaf node!");
563 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
564 return 0; // Never add these nodes.
566 // Check that remaining values produced are not flags.
567 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
568 if (N->getValueType(i) == MVT::Flag)
569 return 0; // Never CSE anything that produces a flag.
571 SDNode *New = CSEMap.GetOrInsertNode(N);
572 if (New != N) return New; // Node already existed.
576 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
577 /// were replaced with those specified. If this node is never memoized,
578 /// return null, otherwise return a pointer to the slot it would take. If a
579 /// node already exists with these operands, the slot will be non-null.
580 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
582 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
583 return 0; // Never add these nodes.
585 // Check that remaining values produced are not flags.
586 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
587 if (N->getValueType(i) == MVT::Flag)
588 return 0; // Never CSE anything that produces a flag.
591 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Op);
592 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
595 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
596 /// were replaced with those specified. If this node is never memoized,
597 /// return null, otherwise return a pointer to the slot it would take. If a
598 /// node already exists with these operands, the slot will be non-null.
599 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
600 SDOperand Op1, SDOperand Op2,
602 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
603 return 0; // Never add these nodes.
605 // Check that remaining values produced are not flags.
606 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
607 if (N->getValueType(i) == MVT::Flag)
608 return 0; // Never CSE anything that produces a flag.
611 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Op1, Op2);
612 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
616 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
617 /// were replaced with those specified. If this node is never memoized,
618 /// return null, otherwise return a pointer to the slot it would take. If a
619 /// node already exists with these operands, the slot will be non-null.
620 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
621 const SDOperand *Ops,unsigned NumOps,
623 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
624 return 0; // Never add these nodes.
626 // Check that remaining values produced are not flags.
627 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
628 if (N->getValueType(i) == MVT::Flag)
629 return 0; // Never CSE anything that produces a flag.
632 AddNodeIDNode(ID, N->getOpcode(), N->getVTList());
634 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
635 ID.AddInteger(LD->getAddressingMode());
636 ID.AddInteger(LD->getExtensionType());
637 ID.AddInteger(LD->getLoadedVT());
638 ID.AddPointer(LD->getSrcValue());
639 ID.AddInteger(LD->getSrcValueOffset());
640 ID.AddInteger(LD->getAlignment());
641 ID.AddInteger(LD->isVolatile());
642 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
643 ID.AddInteger(ST->getAddressingMode());
644 ID.AddInteger(ST->isTruncatingStore());
645 ID.AddInteger(ST->getStoredVT());
646 ID.AddPointer(ST->getSrcValue());
647 ID.AddInteger(ST->getSrcValueOffset());
648 ID.AddInteger(ST->getAlignment());
649 ID.AddInteger(ST->isVolatile());
652 AddNodeIDOperands(ID, Ops, NumOps);
653 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
657 SelectionDAG::~SelectionDAG() {
658 while (!AllNodes.empty()) {
659 SDNode *N = AllNodes.begin();
660 N->SetNextInBucket(0);
661 delete [] N->OperandList;
664 AllNodes.pop_front();
668 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
669 if (Op.getValueType() == VT) return Op;
670 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
671 return getNode(ISD::AND, Op.getValueType(), Op,
672 getConstant(Imm, Op.getValueType()));
675 SDOperand SelectionDAG::getString(const std::string &Val) {
676 StringSDNode *&N = StringNodes[Val];
678 N = new StringSDNode(Val);
679 AllNodes.push_back(N);
681 return SDOperand(N, 0);
684 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
685 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
686 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
688 // Mask out any bits that are not valid for this constant.
689 Val &= MVT::getIntVTBitMask(VT);
691 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
693 AddNodeIDNode(ID, Opc, getVTList(VT));
696 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
697 return SDOperand(E, 0);
698 SDNode *N = new ConstantSDNode(isT, Val, VT);
699 CSEMap.InsertNode(N, IP);
700 AllNodes.push_back(N);
701 return SDOperand(N, 0);
705 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
707 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
709 Val = (float)Val; // Mask out extra precision.
711 // Do the map lookup using the actual bit pattern for the floating point
712 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
713 // we don't have issues with SNANs.
714 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
716 AddNodeIDNode(ID, Opc, getVTList(VT));
719 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
720 return SDOperand(E, 0);
721 SDNode *N = new ConstantFPSDNode(isTarget, Val, VT);
722 CSEMap.InsertNode(N, IP);
723 AllNodes.push_back(N);
724 return SDOperand(N, 0);
727 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
728 MVT::ValueType VT, int Offset,
730 unsigned Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
732 AddNodeIDNode(ID, Opc, getVTList(VT));
734 ID.AddInteger(Offset);
736 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
737 return SDOperand(E, 0);
738 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
739 CSEMap.InsertNode(N, IP);
740 AllNodes.push_back(N);
741 return SDOperand(N, 0);
744 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
746 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
748 AddNodeIDNode(ID, Opc, getVTList(VT));
751 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
752 return SDOperand(E, 0);
753 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
754 CSEMap.InsertNode(N, IP);
755 AllNodes.push_back(N);
756 return SDOperand(N, 0);
759 SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
760 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
762 AddNodeIDNode(ID, Opc, getVTList(VT));
765 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
766 return SDOperand(E, 0);
767 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
768 CSEMap.InsertNode(N, IP);
769 AllNodes.push_back(N);
770 return SDOperand(N, 0);
773 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
774 unsigned Alignment, int Offset,
776 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
778 AddNodeIDNode(ID, Opc, getVTList(VT));
779 ID.AddInteger(Alignment);
780 ID.AddInteger(Offset);
783 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
784 return SDOperand(E, 0);
785 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
786 CSEMap.InsertNode(N, IP);
787 AllNodes.push_back(N);
788 return SDOperand(N, 0);
792 SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
794 unsigned Alignment, int Offset,
796 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
798 AddNodeIDNode(ID, Opc, getVTList(VT));
799 ID.AddInteger(Alignment);
800 ID.AddInteger(Offset);
801 C->AddSelectionDAGCSEId(ID);
803 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
804 return SDOperand(E, 0);
805 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
806 CSEMap.InsertNode(N, IP);
807 AllNodes.push_back(N);
808 return SDOperand(N, 0);
812 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
814 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other));
817 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
818 return SDOperand(E, 0);
819 SDNode *N = new BasicBlockSDNode(MBB);
820 CSEMap.InsertNode(N, IP);
821 AllNodes.push_back(N);
822 return SDOperand(N, 0);
825 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
826 if ((unsigned)VT >= ValueTypeNodes.size())
827 ValueTypeNodes.resize(VT+1);
828 if (ValueTypeNodes[VT] == 0) {
829 ValueTypeNodes[VT] = new VTSDNode(VT);
830 AllNodes.push_back(ValueTypeNodes[VT]);
833 return SDOperand(ValueTypeNodes[VT], 0);
836 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
837 SDNode *&N = ExternalSymbols[Sym];
838 if (N) return SDOperand(N, 0);
839 N = new ExternalSymbolSDNode(false, Sym, VT);
840 AllNodes.push_back(N);
841 return SDOperand(N, 0);
844 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
846 SDNode *&N = TargetExternalSymbols[Sym];
847 if (N) return SDOperand(N, 0);
848 N = new ExternalSymbolSDNode(true, Sym, VT);
849 AllNodes.push_back(N);
850 return SDOperand(N, 0);
853 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
854 if ((unsigned)Cond >= CondCodeNodes.size())
855 CondCodeNodes.resize(Cond+1);
857 if (CondCodeNodes[Cond] == 0) {
858 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
859 AllNodes.push_back(CondCodeNodes[Cond]);
861 return SDOperand(CondCodeNodes[Cond], 0);
864 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
866 AddNodeIDNode(ID, ISD::Register, getVTList(VT));
867 ID.AddInteger(RegNo);
869 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
870 return SDOperand(E, 0);
871 SDNode *N = new RegisterSDNode(RegNo, VT);
872 CSEMap.InsertNode(N, IP);
873 AllNodes.push_back(N);
874 return SDOperand(N, 0);
877 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
878 assert((!V || isa<PointerType>(V->getType())) &&
879 "SrcValue is not a pointer?");
882 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other));
884 ID.AddInteger(Offset);
886 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
887 return SDOperand(E, 0);
888 SDNode *N = new SrcValueSDNode(V, Offset);
889 CSEMap.InsertNode(N, IP);
890 AllNodes.push_back(N);
891 return SDOperand(N, 0);
894 SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
895 SDOperand N2, ISD::CondCode Cond) {
896 // These setcc operations always fold.
900 case ISD::SETFALSE2: return getConstant(0, VT);
902 case ISD::SETTRUE2: return getConstant(1, VT);
914 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
918 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
919 uint64_t C2 = N2C->getValue();
920 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
921 uint64_t C1 = N1C->getValue();
923 // Sign extend the operands if required
924 if (ISD::isSignedIntSetCC(Cond)) {
925 C1 = N1C->getSignExtended();
926 C2 = N2C->getSignExtended();
930 default: assert(0 && "Unknown integer setcc!");
931 case ISD::SETEQ: return getConstant(C1 == C2, VT);
932 case ISD::SETNE: return getConstant(C1 != C2, VT);
933 case ISD::SETULT: return getConstant(C1 < C2, VT);
934 case ISD::SETUGT: return getConstant(C1 > C2, VT);
935 case ISD::SETULE: return getConstant(C1 <= C2, VT);
936 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
937 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
938 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
939 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
940 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
944 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
945 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
946 double C1 = N1C->getValue(), C2 = N2C->getValue();
949 default: break; // FIXME: Implement the rest of these!
950 case ISD::SETEQ: return getConstant(C1 == C2, VT);
951 case ISD::SETNE: return getConstant(C1 != C2, VT);
952 case ISD::SETLT: return getConstant(C1 < C2, VT);
953 case ISD::SETGT: return getConstant(C1 > C2, VT);
954 case ISD::SETLE: return getConstant(C1 <= C2, VT);
955 case ISD::SETGE: return getConstant(C1 >= C2, VT);
958 // Ensure that the constant occurs on the RHS.
959 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
962 // Could not fold it.
967 /// getNode - Gets or creates the specified node.
969 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
971 AddNodeIDNode(ID, Opcode, getVTList(VT));
973 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
974 return SDOperand(E, 0);
975 SDNode *N = new SDNode(Opcode, VT);
976 CSEMap.InsertNode(N, IP);
978 AllNodes.push_back(N);
979 return SDOperand(N, 0);
982 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
985 // Constant fold unary operations with an integer constant operand.
986 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
987 uint64_t Val = C->getValue();
990 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
991 case ISD::ANY_EXTEND:
992 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
993 case ISD::TRUNCATE: return getConstant(Val, VT);
994 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
995 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
996 case ISD::BIT_CONVERT:
997 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
998 return getConstantFP(BitsToFloat(Val), VT);
999 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
1000 return getConstantFP(BitsToDouble(Val), VT);
1004 default: assert(0 && "Invalid bswap!"); break;
1005 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1006 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1007 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1012 default: assert(0 && "Invalid ctpop!"); break;
1013 case MVT::i1: return getConstant(Val != 0, VT);
1015 Tmp1 = (unsigned)Val & 0xFF;
1016 return getConstant(CountPopulation_32(Tmp1), VT);
1018 Tmp1 = (unsigned)Val & 0xFFFF;
1019 return getConstant(CountPopulation_32(Tmp1), VT);
1021 return getConstant(CountPopulation_32((unsigned)Val), VT);
1023 return getConstant(CountPopulation_64(Val), VT);
1027 default: assert(0 && "Invalid ctlz!"); break;
1028 case MVT::i1: return getConstant(Val == 0, VT);
1030 Tmp1 = (unsigned)Val & 0xFF;
1031 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1033 Tmp1 = (unsigned)Val & 0xFFFF;
1034 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1036 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1038 return getConstant(CountLeadingZeros_64(Val), VT);
1042 default: assert(0 && "Invalid cttz!"); break;
1043 case MVT::i1: return getConstant(Val == 0, VT);
1045 Tmp1 = (unsigned)Val | 0x100;
1046 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1048 Tmp1 = (unsigned)Val | 0x10000;
1049 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1051 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1053 return getConstant(CountTrailingZeros_64(Val), VT);
1058 // Constant fold unary operations with an floating point constant operand.
1059 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1062 return getConstantFP(-C->getValue(), VT);
1064 return getConstantFP(fabs(C->getValue()), VT);
1066 case ISD::FP_EXTEND:
1067 return getConstantFP(C->getValue(), VT);
1068 case ISD::FP_TO_SINT:
1069 return getConstant((int64_t)C->getValue(), VT);
1070 case ISD::FP_TO_UINT:
1071 return getConstant((uint64_t)C->getValue(), VT);
1072 case ISD::BIT_CONVERT:
1073 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1074 return getConstant(FloatToBits(C->getValue()), VT);
1075 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1076 return getConstant(DoubleToBits(C->getValue()), VT);
1080 unsigned OpOpcode = Operand.Val->getOpcode();
1082 case ISD::TokenFactor:
1083 return Operand; // Factor of one node? No factor.
1084 case ISD::SIGN_EXTEND:
1085 if (Operand.getValueType() == VT) return Operand; // noop extension
1086 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
1087 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1088 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1090 case ISD::ZERO_EXTEND:
1091 if (Operand.getValueType() == VT) return Operand; // noop extension
1092 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
1093 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1094 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1096 case ISD::ANY_EXTEND:
1097 if (Operand.getValueType() == VT) return Operand; // noop extension
1098 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
1099 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1100 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1101 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1104 if (Operand.getValueType() == VT) return Operand; // noop truncate
1105 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
1106 if (OpOpcode == ISD::TRUNCATE)
1107 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1108 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1109 OpOpcode == ISD::ANY_EXTEND) {
1110 // If the source is smaller than the dest, we still need an extend.
1111 if (Operand.Val->getOperand(0).getValueType() < VT)
1112 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1113 else if (Operand.Val->getOperand(0).getValueType() > VT)
1114 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1116 return Operand.Val->getOperand(0);
1119 case ISD::BIT_CONVERT:
1120 // Basic sanity checking.
1121 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
1122 && "Cannot BIT_CONVERT between two different types!");
1123 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1124 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1125 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1126 if (OpOpcode == ISD::UNDEF)
1127 return getNode(ISD::UNDEF, VT);
1129 case ISD::SCALAR_TO_VECTOR:
1130 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1131 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1132 "Illegal SCALAR_TO_VECTOR node!");
1135 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1136 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1137 Operand.Val->getOperand(0));
1138 if (OpOpcode == ISD::FNEG) // --X -> X
1139 return Operand.Val->getOperand(0);
1142 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1143 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1148 SDVTList VTs = getVTList(VT);
1149 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1150 FoldingSetNodeID ID;
1151 AddNodeIDNode(ID, Opcode, VTs, Operand);
1153 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1154 return SDOperand(E, 0);
1155 N = new SDNode(Opcode, Operand);
1156 N->setValueTypes(VTs);
1157 CSEMap.InsertNode(N, IP);
1159 N = new SDNode(Opcode, Operand);
1160 N->setValueTypes(VTs);
1162 AllNodes.push_back(N);
1163 return SDOperand(N, 0);
1168 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1169 SDOperand N1, SDOperand N2) {
1172 case ISD::TokenFactor:
1173 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1174 N2.getValueType() == MVT::Other && "Invalid token factor!");
1183 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1190 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1197 assert(N1.getValueType() == N2.getValueType() &&
1198 N1.getValueType() == VT && "Binary operator types must match!");
1200 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1201 assert(N1.getValueType() == VT &&
1202 MVT::isFloatingPoint(N1.getValueType()) &&
1203 MVT::isFloatingPoint(N2.getValueType()) &&
1204 "Invalid FCOPYSIGN!");
1211 assert(VT == N1.getValueType() &&
1212 "Shift operators return type must be the same as their first arg");
1213 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1214 VT != MVT::i1 && "Shifts only work on integers");
1216 case ISD::FP_ROUND_INREG: {
1217 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1218 assert(VT == N1.getValueType() && "Not an inreg round!");
1219 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1220 "Cannot FP_ROUND_INREG integer types");
1221 assert(EVT <= VT && "Not rounding down!");
1224 case ISD::AssertSext:
1225 case ISD::AssertZext:
1226 case ISD::SIGN_EXTEND_INREG: {
1227 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1228 assert(VT == N1.getValueType() && "Not an inreg extend!");
1229 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1230 "Cannot *_EXTEND_INREG FP types");
1231 assert(EVT <= VT && "Not extending!");
1238 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1239 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1241 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1242 int64_t Val = N1C->getValue();
1243 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1244 Val <<= 64-FromBits;
1245 Val >>= 64-FromBits;
1246 return getConstant(Val, VT);
1250 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1252 case ISD::ADD: return getConstant(C1 + C2, VT);
1253 case ISD::SUB: return getConstant(C1 - C2, VT);
1254 case ISD::MUL: return getConstant(C1 * C2, VT);
1256 if (C2) return getConstant(C1 / C2, VT);
1259 if (C2) return getConstant(C1 % C2, VT);
1262 if (C2) return getConstant(N1C->getSignExtended() /
1263 N2C->getSignExtended(), VT);
1266 if (C2) return getConstant(N1C->getSignExtended() %
1267 N2C->getSignExtended(), VT);
1269 case ISD::AND : return getConstant(C1 & C2, VT);
1270 case ISD::OR : return getConstant(C1 | C2, VT);
1271 case ISD::XOR : return getConstant(C1 ^ C2, VT);
1272 case ISD::SHL : return getConstant(C1 << C2, VT);
1273 case ISD::SRL : return getConstant(C1 >> C2, VT);
1274 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
1276 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1279 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1283 } else { // Cannonicalize constant to RHS if commutative
1284 if (isCommutativeBinOp(Opcode)) {
1285 std::swap(N1C, N2C);
1291 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1292 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1295 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1297 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1298 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1299 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1301 if (C2) return getConstantFP(C1 / C2, VT);
1304 if (C2) return getConstantFP(fmod(C1, C2), VT);
1306 case ISD::FCOPYSIGN: {
1317 if (u2.I < 0) // Sign bit of RHS set?
1318 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1320 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1321 return getConstantFP(u1.F, VT);
1325 } else { // Cannonicalize constant to RHS if commutative
1326 if (isCommutativeBinOp(Opcode)) {
1327 std::swap(N1CFP, N2CFP);
1333 // Canonicalize an UNDEF to the RHS, even over a constant.
1334 if (N1.getOpcode() == ISD::UNDEF) {
1335 if (isCommutativeBinOp(Opcode)) {
1339 case ISD::FP_ROUND_INREG:
1340 case ISD::SIGN_EXTEND_INREG:
1346 return N1; // fold op(undef, arg2) -> undef
1353 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1358 // Fold a bunch of operators when the RHS is undef.
1359 if (N2.getOpcode() == ISD::UNDEF) {
1373 return N2; // fold op(arg1, undef) -> undef
1378 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1380 return getConstant(MVT::getIntVTBitMask(VT), VT);
1389 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1390 // worth handling here.
1391 if (N2C && N2C->getValue() == 0)
1396 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1397 // worth handling here.
1398 if (N2C && N2C->getValue() == 0)
1401 case ISD::FP_ROUND_INREG:
1402 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1404 case ISD::SIGN_EXTEND_INREG: {
1405 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1406 if (EVT == VT) return N1; // Not actually extending
1409 case ISD::EXTRACT_ELEMENT:
1410 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
1412 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
1413 // 64-bit integers into 32-bit parts. Instead of building the extract of
1414 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
1415 if (N1.getOpcode() == ISD::BUILD_PAIR)
1416 return N1.getOperand(N2C->getValue());
1418 // EXTRACT_ELEMENT of a constant int is also very common.
1419 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
1420 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
1421 return getConstant(C->getValue() >> Shift, VT);
1425 // FIXME: figure out how to safely handle things like
1426 // int foo(int x) { return 1 << (x & 255); }
1427 // int bar() { return foo(256); }
1432 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1433 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
1434 return getNode(Opcode, VT, N1, N2.getOperand(0));
1435 else if (N2.getOpcode() == ISD::AND)
1436 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1437 // If the and is only masking out bits that cannot effect the shift,
1438 // eliminate the and.
1439 unsigned NumBits = MVT::getSizeInBits(VT);
1440 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1441 return getNode(Opcode, VT, N1, N2.getOperand(0));
1447 // Memoize this node if possible.
1449 SDVTList VTs = getVTList(VT);
1450 if (VT != MVT::Flag) {
1451 FoldingSetNodeID ID;
1452 AddNodeIDNode(ID, Opcode, VTs, N1, N2);
1454 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1455 return SDOperand(E, 0);
1456 N = new SDNode(Opcode, N1, N2);
1457 N->setValueTypes(VTs);
1458 CSEMap.InsertNode(N, IP);
1460 N = new SDNode(Opcode, N1, N2);
1461 N->setValueTypes(VTs);
1464 AllNodes.push_back(N);
1465 return SDOperand(N, 0);
1468 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1469 SDOperand N1, SDOperand N2, SDOperand N3) {
1470 // Perform various simplifications.
1471 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1472 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1473 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1476 // Use FoldSetCC to simplify SETCC's.
1477 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
1478 if (Simp.Val) return Simp;
1483 if (N1C->getValue())
1484 return N2; // select true, X, Y -> X
1486 return N3; // select false, X, Y -> Y
1488 if (N2 == N3) return N2; // select C, X, X -> X
1492 if (N2C->getValue()) // Unconditional branch
1493 return getNode(ISD::BR, MVT::Other, N1, N3);
1495 return N1; // Never-taken branch
1497 case ISD::VECTOR_SHUFFLE:
1498 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1499 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1500 N3.getOpcode() == ISD::BUILD_VECTOR &&
1501 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1502 "Illegal VECTOR_SHUFFLE node!");
1506 // Memoize node if it doesn't produce a flag.
1508 SDVTList VTs = getVTList(VT);
1509 if (VT != MVT::Flag) {
1510 FoldingSetNodeID ID;
1511 AddNodeIDNode(ID, Opcode, VTs, N1, N2, N3);
1513 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1514 return SDOperand(E, 0);
1515 N = new SDNode(Opcode, N1, N2, N3);
1516 N->setValueTypes(VTs);
1517 CSEMap.InsertNode(N, IP);
1519 N = new SDNode(Opcode, N1, N2, N3);
1520 N->setValueTypes(VTs);
1522 AllNodes.push_back(N);
1523 return SDOperand(N, 0);
1526 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1527 SDOperand N1, SDOperand N2, SDOperand N3,
1529 SDOperand Ops[] = { N1, N2, N3, N4 };
1530 return getNode(Opcode, VT, Ops, 4);
1533 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1534 SDOperand N1, SDOperand N2, SDOperand N3,
1535 SDOperand N4, SDOperand N5) {
1536 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1537 return getNode(Opcode, VT, Ops, 5);
1540 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1541 SDOperand Chain, SDOperand Ptr,
1542 const Value *SV, int SVOffset,
1544 // FIXME: Alignment == 1 for now.
1545 unsigned Alignment = 1;
1546 SDVTList VTs = getVTList(VT, MVT::Other);
1547 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
1548 FoldingSetNodeID ID;
1549 AddNodeIDNode(ID, ISD::LOAD, VTs, Chain, Ptr, Undef);
1550 ID.AddInteger(ISD::UNINDEXED);
1551 ID.AddInteger(ISD::NON_EXTLOAD);
1554 ID.AddInteger(SVOffset);
1555 ID.AddInteger(Alignment);
1556 ID.AddInteger(isVolatile);
1558 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1559 return SDOperand(E, 0);
1560 SDNode *N = new LoadSDNode(Chain, Ptr, Undef, ISD::UNINDEXED,
1561 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
1563 N->setValueTypes(VTs);
1564 CSEMap.InsertNode(N, IP);
1565 AllNodes.push_back(N);
1566 return SDOperand(N, 0);
1569 SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
1570 SDOperand Chain, SDOperand Ptr, const Value *SV,
1571 int SVOffset, MVT::ValueType EVT,
1573 // If they are asking for an extending load from/to the same thing, return a
1576 ExtType = ISD::NON_EXTLOAD;
1578 if (MVT::isVector(VT))
1579 assert(EVT == MVT::getVectorBaseType(VT) && "Invalid vector extload!");
1581 assert(EVT < VT && "Should only be an extending load, not truncating!");
1582 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
1583 "Cannot sign/zero extend a FP/Vector load!");
1584 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
1585 "Cannot convert from FP to Int or Int -> FP!");
1587 // FIXME: Alignment == 1 for now.
1588 unsigned Alignment = 1;
1589 SDVTList VTs = getVTList(VT, MVT::Other);
1590 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
1591 FoldingSetNodeID ID;
1592 AddNodeIDNode(ID, ISD::LOAD, VTs, Chain, Ptr, Undef);
1593 ID.AddInteger(ISD::UNINDEXED);
1594 ID.AddInteger(ExtType);
1597 ID.AddInteger(SVOffset);
1598 ID.AddInteger(Alignment);
1599 ID.AddInteger(isVolatile);
1601 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1602 return SDOperand(E, 0);
1603 SDNode *N = new LoadSDNode(Chain, Ptr, Undef, ISD::UNINDEXED, ExtType, EVT,
1604 SV, SVOffset, Alignment, isVolatile);
1605 N->setValueTypes(VTs);
1606 CSEMap.InsertNode(N, IP);
1607 AllNodes.push_back(N);
1608 return SDOperand(N, 0);
1611 SDOperand SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
1612 SDOperand Offset, ISD::MemOpAddrMode AM){
1613 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
1614 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
1615 "Load is already a indexed load!");
1616 MVT::ValueType VT = OrigLoad.getValueType();
1617 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
1618 FoldingSetNodeID ID;
1619 AddNodeIDNode(ID, ISD::LOAD, VTs, LD->getChain(), Base, Offset);
1621 ID.AddInteger(LD->getExtensionType());
1622 ID.AddInteger(LD->getLoadedVT());
1623 ID.AddPointer(LD->getSrcValue());
1624 ID.AddInteger(LD->getSrcValueOffset());
1625 ID.AddInteger(LD->getAlignment());
1626 ID.AddInteger(LD->isVolatile());
1628 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1629 return SDOperand(E, 0);
1630 SDNode *N = new LoadSDNode(LD->getChain(), Base, Offset, AM,
1631 LD->getExtensionType(), LD->getLoadedVT(),
1632 LD->getSrcValue(), LD->getSrcValueOffset(),
1633 LD->getAlignment(), LD->isVolatile());
1634 N->setValueTypes(VTs);
1635 CSEMap.InsertNode(N, IP);
1636 AllNodes.push_back(N);
1637 return SDOperand(N, 0);
1640 SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1641 SDOperand Chain, SDOperand Ptr,
1643 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1644 getValueType(EVT) };
1645 return getNode(ISD::VLOAD, getVTList(MVT::Vector, MVT::Other), Ops, 5);
1648 SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Value,
1649 SDOperand Ptr, const Value *SV, int SVOffset,
1651 MVT::ValueType VT = Value.getValueType();
1653 // FIXME: Alignment == 1 for now.
1654 unsigned Alignment = 1;
1655 SDVTList VTs = getVTList(MVT::Other);
1656 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
1657 SDOperand Ops[] = { Chain, Value, Ptr, Undef };
1658 FoldingSetNodeID ID;
1659 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
1660 ID.AddInteger(ISD::UNINDEXED);
1661 ID.AddInteger(false);
1664 ID.AddInteger(SVOffset);
1665 ID.AddInteger(Alignment);
1666 ID.AddInteger(isVolatile);
1668 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1669 return SDOperand(E, 0);
1670 SDNode *N = new StoreSDNode(Chain, Value, Ptr, Undef, ISD::UNINDEXED, false,
1671 VT, SV, SVOffset, Alignment, isVolatile);
1672 N->setValueTypes(VTs);
1673 CSEMap.InsertNode(N, IP);
1674 AllNodes.push_back(N);
1675 return SDOperand(N, 0);
1678 SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Value,
1679 SDOperand Ptr, const Value *SV,
1680 int SVOffset, MVT::ValueType SVT,
1682 MVT::ValueType VT = Value.getValueType();
1683 bool isTrunc = VT != SVT;
1685 assert(VT > SVT && "Not a truncation?");
1686 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
1687 "Can't do FP-INT conversion!");
1689 // FIXME: Alignment == 1 for now.
1690 unsigned Alignment = 1;
1691 SDVTList VTs = getVTList(MVT::Other);
1692 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
1693 SDOperand Ops[] = { Chain, Value, Ptr, Undef };
1694 FoldingSetNodeID ID;
1695 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
1696 ID.AddInteger(ISD::UNINDEXED);
1697 ID.AddInteger(isTrunc);
1700 ID.AddInteger(SVOffset);
1701 ID.AddInteger(Alignment);
1702 ID.AddInteger(isVolatile);
1704 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1705 return SDOperand(E, 0);
1706 SDNode *N = new StoreSDNode(Chain, Value, Ptr, Undef, ISD::UNINDEXED, isTrunc,
1707 SVT, SV, SVOffset, Alignment, isVolatile);
1708 N->setValueTypes(VTs);
1709 CSEMap.InsertNode(N, IP);
1710 AllNodes.push_back(N);
1711 return SDOperand(N, 0);
1714 SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1715 SDOperand Chain, SDOperand Ptr,
1717 SDOperand Ops[] = { Chain, Ptr, SV };
1718 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
1721 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1722 const SDOperand *Ops, unsigned NumOps) {
1724 case 0: return getNode(Opcode, VT);
1725 case 1: return getNode(Opcode, VT, Ops[0]);
1726 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1727 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
1733 case ISD::SELECT_CC: {
1734 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
1735 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1736 "LHS and RHS of condition must have same type!");
1737 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1738 "True and False arms of SelectCC must have same type!");
1739 assert(Ops[2].getValueType() == VT &&
1740 "select_cc node must be of same type as true and false value!");
1744 assert(NumOps == 5 && "BR_CC takes 5 operands!");
1745 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1746 "LHS/RHS of comparison should match types!");
1753 SDVTList VTs = getVTList(VT);
1754 if (VT != MVT::Flag) {
1755 FoldingSetNodeID ID;
1756 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
1758 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1759 return SDOperand(E, 0);
1760 N = new SDNode(Opcode, Ops, NumOps);
1761 N->setValueTypes(VTs);
1762 CSEMap.InsertNode(N, IP);
1764 N = new SDNode(Opcode, Ops, NumOps);
1765 N->setValueTypes(VTs);
1767 AllNodes.push_back(N);
1768 return SDOperand(N, 0);
1771 SDOperand SelectionDAG::getNode(unsigned Opcode,
1772 std::vector<MVT::ValueType> &ResultTys,
1773 const SDOperand *Ops, unsigned NumOps) {
1774 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
1778 SDOperand SelectionDAG::getNode(unsigned Opcode,
1779 const MVT::ValueType *VTs, unsigned NumVTs,
1780 const SDOperand *Ops, unsigned NumOps) {
1782 return getNode(Opcode, VTs[0], Ops, NumOps);
1783 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
1786 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
1787 const SDOperand *Ops, unsigned NumOps) {
1788 if (VTList.NumVTs == 1)
1789 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
1792 // FIXME: figure out how to safely handle things like
1793 // int foo(int x) { return 1 << (x & 255); }
1794 // int bar() { return foo(256); }
1796 case ISD::SRA_PARTS:
1797 case ISD::SRL_PARTS:
1798 case ISD::SHL_PARTS:
1799 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1800 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
1801 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1802 else if (N3.getOpcode() == ISD::AND)
1803 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1804 // If the and is only masking out bits that cannot effect the shift,
1805 // eliminate the and.
1806 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1807 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1808 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1814 // Memoize the node unless it returns a flag.
1816 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
1817 FoldingSetNodeID ID;
1818 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
1820 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1821 return SDOperand(E, 0);
1822 N = new SDNode(Opcode, Ops, NumOps);
1823 N->setValueTypes(VTList);
1824 CSEMap.InsertNode(N, IP);
1826 N = new SDNode(Opcode, Ops, NumOps);
1827 N->setValueTypes(VTList);
1829 AllNodes.push_back(N);
1830 return SDOperand(N, 0);
1833 SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
1834 return makeVTList(SDNode::getValueTypeList(VT), 1);
1837 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
1838 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1839 E = VTList.end(); I != E; ++I) {
1840 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
1841 return makeVTList(&(*I)[0], 2);
1843 std::vector<MVT::ValueType> V;
1846 VTList.push_front(V);
1847 return makeVTList(&(*VTList.begin())[0], 2);
1849 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
1850 MVT::ValueType VT3) {
1851 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1852 E = VTList.end(); I != E; ++I) {
1853 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
1855 return makeVTList(&(*I)[0], 3);
1857 std::vector<MVT::ValueType> V;
1861 VTList.push_front(V);
1862 return makeVTList(&(*VTList.begin())[0], 3);
1865 SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
1867 case 0: assert(0 && "Cannot have nodes without results!");
1868 case 1: return makeVTList(SDNode::getValueTypeList(VTs[0]), 1);
1869 case 2: return getVTList(VTs[0], VTs[1]);
1870 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
1874 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1875 E = VTList.end(); I != E; ++I) {
1876 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
1878 bool NoMatch = false;
1879 for (unsigned i = 2; i != NumVTs; ++i)
1880 if (VTs[i] != (*I)[i]) {
1885 return makeVTList(&*I->begin(), NumVTs);
1888 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
1889 return makeVTList(&*VTList.begin()->begin(), NumVTs);
1893 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1894 /// specified operands. If the resultant node already exists in the DAG,
1895 /// this does not modify the specified node, instead it returns the node that
1896 /// already exists. If the resultant node does not exist in the DAG, the
1897 /// input node is returned. As a degenerate case, if you specify the same
1898 /// input operands as the node already has, the input node is returned.
1899 SDOperand SelectionDAG::
1900 UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1901 SDNode *N = InN.Val;
1902 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1904 // Check to see if there is no change.
1905 if (Op == N->getOperand(0)) return InN;
1907 // See if the modified node already exists.
1908 void *InsertPos = 0;
1909 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1910 return SDOperand(Existing, InN.ResNo);
1912 // Nope it doesn't. Remove the node from it's current place in the maps.
1914 RemoveNodeFromCSEMaps(N);
1916 // Now we update the operands.
1917 N->OperandList[0].Val->removeUser(N);
1919 N->OperandList[0] = Op;
1921 // If this gets put into a CSE map, add it.
1922 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1926 SDOperand SelectionDAG::
1927 UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1928 SDNode *N = InN.Val;
1929 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1931 // Check to see if there is no change.
1932 bool AnyChange = false;
1933 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1934 return InN; // No operands changed, just return the input node.
1936 // See if the modified node already exists.
1937 void *InsertPos = 0;
1938 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1939 return SDOperand(Existing, InN.ResNo);
1941 // Nope it doesn't. Remove the node from it's current place in the maps.
1943 RemoveNodeFromCSEMaps(N);
1945 // Now we update the operands.
1946 if (N->OperandList[0] != Op1) {
1947 N->OperandList[0].Val->removeUser(N);
1948 Op1.Val->addUser(N);
1949 N->OperandList[0] = Op1;
1951 if (N->OperandList[1] != Op2) {
1952 N->OperandList[1].Val->removeUser(N);
1953 Op2.Val->addUser(N);
1954 N->OperandList[1] = Op2;
1957 // If this gets put into a CSE map, add it.
1958 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1962 SDOperand SelectionDAG::
1963 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1964 SDOperand Ops[] = { Op1, Op2, Op3 };
1965 return UpdateNodeOperands(N, Ops, 3);
1968 SDOperand SelectionDAG::
1969 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1970 SDOperand Op3, SDOperand Op4) {
1971 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
1972 return UpdateNodeOperands(N, Ops, 4);
1975 SDOperand SelectionDAG::
1976 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1977 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1978 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
1979 return UpdateNodeOperands(N, Ops, 5);
1983 SDOperand SelectionDAG::
1984 UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
1985 SDNode *N = InN.Val;
1986 assert(N->getNumOperands() == NumOps &&
1987 "Update with wrong number of operands");
1989 // Check to see if there is no change.
1990 bool AnyChange = false;
1991 for (unsigned i = 0; i != NumOps; ++i) {
1992 if (Ops[i] != N->getOperand(i)) {
1998 // No operands changed, just return the input node.
1999 if (!AnyChange) return InN;
2001 // See if the modified node already exists.
2002 void *InsertPos = 0;
2003 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
2004 return SDOperand(Existing, InN.ResNo);
2006 // Nope it doesn't. Remove the node from it's current place in the maps.
2008 RemoveNodeFromCSEMaps(N);
2010 // Now we update the operands.
2011 for (unsigned i = 0; i != NumOps; ++i) {
2012 if (N->OperandList[i] != Ops[i]) {
2013 N->OperandList[i].Val->removeUser(N);
2014 Ops[i].Val->addUser(N);
2015 N->OperandList[i] = Ops[i];
2019 // If this gets put into a CSE map, add it.
2020 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2027 /// SelectNodeTo - These are used for target selectors to *mutate* the
2028 /// specified node to have the specified return type, Target opcode, and
2029 /// operands. Note that target opcodes are stored as
2030 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
2032 /// Note that SelectNodeTo returns the resultant node. If there is already a
2033 /// node of the specified opcode and operands, it returns that node instead of
2034 /// the current one.
2035 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2036 MVT::ValueType VT) {
2037 SDVTList VTs = getVTList(VT);
2038 FoldingSetNodeID ID;
2039 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs);
2041 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2044 RemoveNodeFromCSEMaps(N);
2046 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2047 N->setValueTypes(VTs);
2049 CSEMap.InsertNode(N, IP);
2053 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2054 MVT::ValueType VT, SDOperand Op1) {
2055 // If an identical node already exists, use it.
2056 SDVTList VTs = getVTList(VT);
2057 FoldingSetNodeID ID;
2058 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
2060 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2063 RemoveNodeFromCSEMaps(N);
2064 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2065 N->setValueTypes(VTs);
2066 N->setOperands(Op1);
2067 CSEMap.InsertNode(N, IP);
2071 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2072 MVT::ValueType VT, SDOperand Op1,
2074 // If an identical node already exists, use it.
2075 SDVTList VTs = getVTList(VT);
2076 FoldingSetNodeID ID;
2077 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
2079 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2082 RemoveNodeFromCSEMaps(N);
2083 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2084 N->setValueTypes(VTs);
2085 N->setOperands(Op1, Op2);
2087 CSEMap.InsertNode(N, IP); // Memoize the new node.
2091 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2092 MVT::ValueType VT, SDOperand Op1,
2093 SDOperand Op2, SDOperand Op3) {
2094 // If an identical node already exists, use it.
2095 SDVTList VTs = getVTList(VT);
2096 FoldingSetNodeID ID;
2097 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2, Op3);
2099 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2102 RemoveNodeFromCSEMaps(N);
2103 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2104 N->setValueTypes(VTs);
2105 N->setOperands(Op1, Op2, Op3);
2107 CSEMap.InsertNode(N, IP); // Memoize the new node.
2111 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2112 MVT::ValueType VT, const SDOperand *Ops,
2114 // If an identical node already exists, use it.
2115 SDVTList VTs = getVTList(VT);
2116 FoldingSetNodeID ID;
2117 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
2119 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2122 RemoveNodeFromCSEMaps(N);
2123 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2124 N->setValueTypes(VTs);
2125 N->setOperands(Ops, NumOps);
2127 CSEMap.InsertNode(N, IP); // Memoize the new node.
2131 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2132 MVT::ValueType VT1, MVT::ValueType VT2,
2133 SDOperand Op1, SDOperand Op2) {
2134 SDVTList VTs = getVTList(VT1, VT2);
2135 FoldingSetNodeID ID;
2136 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
2138 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2141 RemoveNodeFromCSEMaps(N);
2142 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2143 N->setValueTypes(VTs);
2144 N->setOperands(Op1, Op2);
2146 CSEMap.InsertNode(N, IP); // Memoize the new node.
2150 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2151 MVT::ValueType VT1, MVT::ValueType VT2,
2152 SDOperand Op1, SDOperand Op2,
2154 // If an identical node already exists, use it.
2155 SDVTList VTs = getVTList(VT1, VT2);
2156 FoldingSetNodeID ID;
2157 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2, Op3);
2159 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2162 RemoveNodeFromCSEMaps(N);
2163 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2164 N->setValueTypes(VTs);
2165 N->setOperands(Op1, Op2, Op3);
2167 CSEMap.InsertNode(N, IP); // Memoize the new node.
2172 /// getTargetNode - These are used for target selectors to create a new node
2173 /// with specified return type(s), target opcode, and operands.
2175 /// Note that getTargetNode returns the resultant node. If there is already a
2176 /// node of the specified opcode and operands, it returns that node instead of
2177 /// the current one.
2178 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2179 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2181 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2183 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2185 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2186 SDOperand Op1, SDOperand Op2) {
2187 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2189 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2190 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2191 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2193 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2194 const SDOperand *Ops, unsigned NumOps) {
2195 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
2197 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2198 MVT::ValueType VT2, SDOperand Op1) {
2199 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2200 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
2202 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2203 MVT::ValueType VT2, SDOperand Op1,
2205 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2206 SDOperand Ops[] = { Op1, Op2 };
2207 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
2209 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2210 MVT::ValueType VT2, SDOperand Op1,
2211 SDOperand Op2, SDOperand Op3) {
2212 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2213 SDOperand Ops[] = { Op1, Op2, Op3 };
2214 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
2216 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2218 const SDOperand *Ops, unsigned NumOps) {
2219 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2220 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
2222 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2223 MVT::ValueType VT2, MVT::ValueType VT3,
2224 SDOperand Op1, SDOperand Op2) {
2225 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
2226 SDOperand Ops[] = { Op1, Op2 };
2227 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
2229 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2230 MVT::ValueType VT2, MVT::ValueType VT3,
2231 const SDOperand *Ops, unsigned NumOps) {
2232 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
2233 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
2236 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2237 /// This can cause recursive merging of nodes in the DAG.
2239 /// This version assumes From/To have a single result value.
2241 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2242 std::vector<SDNode*> *Deleted) {
2243 SDNode *From = FromN.Val, *To = ToN.Val;
2244 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2245 "Cannot replace with this method!");
2246 assert(From != To && "Cannot replace uses of with self");
2248 while (!From->use_empty()) {
2249 // Process users until they are all gone.
2250 SDNode *U = *From->use_begin();
2252 // This node is about to morph, remove its old self from the CSE maps.
2253 RemoveNodeFromCSEMaps(U);
2255 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2257 if (I->Val == From) {
2258 From->removeUser(U);
2263 // Now that we have modified U, add it back to the CSE maps. If it already
2264 // exists there, recursively merge the results together.
2265 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2266 ReplaceAllUsesWith(U, Existing, Deleted);
2268 if (Deleted) Deleted->push_back(U);
2269 DeleteNodeNotInCSEMaps(U);
2274 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2275 /// This can cause recursive merging of nodes in the DAG.
2277 /// This version assumes From/To have matching types and numbers of result
2280 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2281 std::vector<SDNode*> *Deleted) {
2282 assert(From != To && "Cannot replace uses of with self");
2283 assert(From->getNumValues() == To->getNumValues() &&
2284 "Cannot use this version of ReplaceAllUsesWith!");
2285 if (From->getNumValues() == 1) { // If possible, use the faster version.
2286 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
2290 while (!From->use_empty()) {
2291 // Process users until they are all gone.
2292 SDNode *U = *From->use_begin();
2294 // This node is about to morph, remove its old self from the CSE maps.
2295 RemoveNodeFromCSEMaps(U);
2297 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2299 if (I->Val == From) {
2300 From->removeUser(U);
2305 // Now that we have modified U, add it back to the CSE maps. If it already
2306 // exists there, recursively merge the results together.
2307 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2308 ReplaceAllUsesWith(U, Existing, Deleted);
2310 if (Deleted) Deleted->push_back(U);
2311 DeleteNodeNotInCSEMaps(U);
2316 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2317 /// This can cause recursive merging of nodes in the DAG.
2319 /// This version can replace From with any result values. To must match the
2320 /// number and types of values returned by From.
2321 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
2322 const SDOperand *To,
2323 std::vector<SDNode*> *Deleted) {
2324 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
2325 // Degenerate case handled above.
2326 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
2330 while (!From->use_empty()) {
2331 // Process users until they are all gone.
2332 SDNode *U = *From->use_begin();
2334 // This node is about to morph, remove its old self from the CSE maps.
2335 RemoveNodeFromCSEMaps(U);
2337 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2339 if (I->Val == From) {
2340 const SDOperand &ToOp = To[I->ResNo];
2341 From->removeUser(U);
2343 ToOp.Val->addUser(U);
2346 // Now that we have modified U, add it back to the CSE maps. If it already
2347 // exists there, recursively merge the results together.
2348 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2349 ReplaceAllUsesWith(U, Existing, Deleted);
2351 if (Deleted) Deleted->push_back(U);
2352 DeleteNodeNotInCSEMaps(U);
2357 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2358 /// uses of other values produced by From.Val alone. The Deleted vector is
2359 /// handled the same was as for ReplaceAllUsesWith.
2360 void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2361 std::vector<SDNode*> &Deleted) {
2362 assert(From != To && "Cannot replace a value with itself");
2363 // Handle the simple, trivial, case efficiently.
2364 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2365 ReplaceAllUsesWith(From, To, &Deleted);
2369 // Get all of the users in a nice, deterministically ordered, uniqued set.
2370 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2372 while (!Users.empty()) {
2373 // We know that this user uses some value of From. If it is the right
2374 // value, update it.
2375 SDNode *User = Users.back();
2378 for (SDOperand *Op = User->OperandList,
2379 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2381 // Okay, we know this user needs to be updated. Remove its old self
2382 // from the CSE maps.
2383 RemoveNodeFromCSEMaps(User);
2385 // Update all operands that match "From".
2386 for (; Op != E; ++Op) {
2388 From.Val->removeUser(User);
2390 To.Val->addUser(User);
2394 // Now that we have modified User, add it back to the CSE maps. If it
2395 // already exists there, recursively merge the results together.
2396 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2397 unsigned NumDeleted = Deleted.size();
2398 ReplaceAllUsesWith(User, Existing, &Deleted);
2400 // User is now dead.
2401 Deleted.push_back(User);
2402 DeleteNodeNotInCSEMaps(User);
2404 // We have to be careful here, because ReplaceAllUsesWith could have
2405 // deleted a user of From, which means there may be dangling pointers
2406 // in the "Users" setvector. Scan over the deleted node pointers and
2407 // remove them from the setvector.
2408 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2409 Users.remove(Deleted[i]);
2411 break; // Exit the operand scanning loop.
2418 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2419 /// their allnodes order. It returns the maximum id.
2420 unsigned SelectionDAG::AssignNodeIds() {
2422 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2429 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
2430 /// based on their topological order. It returns the maximum id and a vector
2431 /// of the SDNodes* in assigned order by reference.
2432 unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
2433 unsigned DAGSize = AllNodes.size();
2434 std::vector<unsigned> InDegree(DAGSize);
2435 std::vector<SDNode*> Sources;
2437 // Use a two pass approach to avoid using a std::map which is slow.
2439 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2442 unsigned Degree = N->use_size();
2443 InDegree[N->getNodeId()] = Degree;
2445 Sources.push_back(N);
2449 while (!Sources.empty()) {
2450 SDNode *N = Sources.back();
2452 TopOrder.push_back(N);
2453 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2455 unsigned Degree = --InDegree[P->getNodeId()];
2457 Sources.push_back(P);
2461 // Second pass, assign the actual topological order as node ids.
2463 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2465 (*TI)->setNodeId(Id++);
2472 //===----------------------------------------------------------------------===//
2474 //===----------------------------------------------------------------------===//
2476 // Out-of-line virtual method to give class a home.
2477 void SDNode::ANCHOR() {
2480 /// Profile - Gather unique data for the node.
2482 void SDNode::Profile(FoldingSetNodeID &ID) {
2483 AddNodeIDNode(ID, this);
2486 /// getValueTypeList - Return a pointer to the specified value type.
2488 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2489 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2494 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2495 /// indicated value. This method ignores uses of other values defined by this
2497 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
2498 assert(Value < getNumValues() && "Bad value!");
2500 // If there is only one value, this is easy.
2501 if (getNumValues() == 1)
2502 return use_size() == NUses;
2503 if (Uses.size() < NUses) return false;
2505 SDOperand TheValue(const_cast<SDNode *>(this), Value);
2507 std::set<SDNode*> UsersHandled;
2509 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
2511 if (User->getNumOperands() == 1 ||
2512 UsersHandled.insert(User).second) // First time we've seen this?
2513 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2514 if (User->getOperand(i) == TheValue) {
2516 return false; // too many uses
2521 // Found exactly the right number of uses?
2526 // isOnlyUse - Return true if this node is the only use of N.
2527 bool SDNode::isOnlyUse(SDNode *N) const {
2529 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2540 // isOperand - Return true if this node is an operand of N.
2541 bool SDOperand::isOperand(SDNode *N) const {
2542 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2543 if (*this == N->getOperand(i))
2548 bool SDNode::isOperand(SDNode *N) const {
2549 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2550 if (this == N->OperandList[i].Val)
2555 uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
2556 assert(Num < NumOperands && "Invalid child # of SDNode!");
2557 return cast<ConstantSDNode>(OperandList[Num])->getValue();
2560 const char *SDNode::getOperationName(const SelectionDAG *G) const {
2561 switch (getOpcode()) {
2563 if (getOpcode() < ISD::BUILTIN_OP_END)
2564 return "<<Unknown DAG Node>>";
2567 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
2568 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2569 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
2571 TargetLowering &TLI = G->getTargetLoweringInfo();
2573 TLI.getTargetNodeName(getOpcode());
2574 if (Name) return Name;
2577 return "<<Unknown Target Node>>";
2580 case ISD::PCMARKER: return "PCMarker";
2581 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
2582 case ISD::SRCVALUE: return "SrcValue";
2583 case ISD::EntryToken: return "EntryToken";
2584 case ISD::TokenFactor: return "TokenFactor";
2585 case ISD::AssertSext: return "AssertSext";
2586 case ISD::AssertZext: return "AssertZext";
2588 case ISD::STRING: return "String";
2589 case ISD::BasicBlock: return "BasicBlock";
2590 case ISD::VALUETYPE: return "ValueType";
2591 case ISD::Register: return "Register";
2593 case ISD::Constant: return "Constant";
2594 case ISD::ConstantFP: return "ConstantFP";
2595 case ISD::GlobalAddress: return "GlobalAddress";
2596 case ISD::FrameIndex: return "FrameIndex";
2597 case ISD::JumpTable: return "JumpTable";
2598 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
2599 case ISD::ConstantPool: return "ConstantPool";
2600 case ISD::ExternalSymbol: return "ExternalSymbol";
2601 case ISD::INTRINSIC_WO_CHAIN: {
2602 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2603 return Intrinsic::getName((Intrinsic::ID)IID);
2605 case ISD::INTRINSIC_VOID:
2606 case ISD::INTRINSIC_W_CHAIN: {
2607 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
2608 return Intrinsic::getName((Intrinsic::ID)IID);
2611 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
2612 case ISD::TargetConstant: return "TargetConstant";
2613 case ISD::TargetConstantFP:return "TargetConstantFP";
2614 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2615 case ISD::TargetFrameIndex: return "TargetFrameIndex";
2616 case ISD::TargetJumpTable: return "TargetJumpTable";
2617 case ISD::TargetConstantPool: return "TargetConstantPool";
2618 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
2620 case ISD::CopyToReg: return "CopyToReg";
2621 case ISD::CopyFromReg: return "CopyFromReg";
2622 case ISD::UNDEF: return "undef";
2623 case ISD::MERGE_VALUES: return "mergevalues";
2624 case ISD::INLINEASM: return "inlineasm";
2625 case ISD::HANDLENODE: return "handlenode";
2626 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
2627 case ISD::CALL: return "call";
2630 case ISD::FABS: return "fabs";
2631 case ISD::FNEG: return "fneg";
2632 case ISD::FSQRT: return "fsqrt";
2633 case ISD::FSIN: return "fsin";
2634 case ISD::FCOS: return "fcos";
2635 case ISD::FPOWI: return "fpowi";
2638 case ISD::ADD: return "add";
2639 case ISD::SUB: return "sub";
2640 case ISD::MUL: return "mul";
2641 case ISD::MULHU: return "mulhu";
2642 case ISD::MULHS: return "mulhs";
2643 case ISD::SDIV: return "sdiv";
2644 case ISD::UDIV: return "udiv";
2645 case ISD::SREM: return "srem";
2646 case ISD::UREM: return "urem";
2647 case ISD::AND: return "and";
2648 case ISD::OR: return "or";
2649 case ISD::XOR: return "xor";
2650 case ISD::SHL: return "shl";
2651 case ISD::SRA: return "sra";
2652 case ISD::SRL: return "srl";
2653 case ISD::ROTL: return "rotl";
2654 case ISD::ROTR: return "rotr";
2655 case ISD::FADD: return "fadd";
2656 case ISD::FSUB: return "fsub";
2657 case ISD::FMUL: return "fmul";
2658 case ISD::FDIV: return "fdiv";
2659 case ISD::FREM: return "frem";
2660 case ISD::FCOPYSIGN: return "fcopysign";
2661 case ISD::VADD: return "vadd";
2662 case ISD::VSUB: return "vsub";
2663 case ISD::VMUL: return "vmul";
2664 case ISD::VSDIV: return "vsdiv";
2665 case ISD::VUDIV: return "vudiv";
2666 case ISD::VAND: return "vand";
2667 case ISD::VOR: return "vor";
2668 case ISD::VXOR: return "vxor";
2670 case ISD::SETCC: return "setcc";
2671 case ISD::SELECT: return "select";
2672 case ISD::SELECT_CC: return "select_cc";
2673 case ISD::VSELECT: return "vselect";
2674 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2675 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2676 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
2677 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
2678 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2679 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2680 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2681 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2682 case ISD::VBIT_CONVERT: return "vbit_convert";
2683 case ISD::ADDC: return "addc";
2684 case ISD::ADDE: return "adde";
2685 case ISD::SUBC: return "subc";
2686 case ISD::SUBE: return "sube";
2687 case ISD::SHL_PARTS: return "shl_parts";
2688 case ISD::SRA_PARTS: return "sra_parts";
2689 case ISD::SRL_PARTS: return "srl_parts";
2691 // Conversion operators.
2692 case ISD::SIGN_EXTEND: return "sign_extend";
2693 case ISD::ZERO_EXTEND: return "zero_extend";
2694 case ISD::ANY_EXTEND: return "any_extend";
2695 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
2696 case ISD::TRUNCATE: return "truncate";
2697 case ISD::FP_ROUND: return "fp_round";
2698 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
2699 case ISD::FP_EXTEND: return "fp_extend";
2701 case ISD::SINT_TO_FP: return "sint_to_fp";
2702 case ISD::UINT_TO_FP: return "uint_to_fp";
2703 case ISD::FP_TO_SINT: return "fp_to_sint";
2704 case ISD::FP_TO_UINT: return "fp_to_uint";
2705 case ISD::BIT_CONVERT: return "bit_convert";
2707 // Control flow instructions
2708 case ISD::BR: return "br";
2709 case ISD::BRIND: return "brind";
2710 case ISD::BRCOND: return "brcond";
2711 case ISD::BR_CC: return "br_cc";
2712 case ISD::RET: return "ret";
2713 case ISD::CALLSEQ_START: return "callseq_start";
2714 case ISD::CALLSEQ_END: return "callseq_end";
2717 case ISD::LOAD: return "load";
2718 case ISD::STORE: return "store";
2719 case ISD::VLOAD: return "vload";
2720 case ISD::VAARG: return "vaarg";
2721 case ISD::VACOPY: return "vacopy";
2722 case ISD::VAEND: return "vaend";
2723 case ISD::VASTART: return "vastart";
2724 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2725 case ISD::EXTRACT_ELEMENT: return "extract_element";
2726 case ISD::BUILD_PAIR: return "build_pair";
2727 case ISD::STACKSAVE: return "stacksave";
2728 case ISD::STACKRESTORE: return "stackrestore";
2730 // Block memory operations.
2731 case ISD::MEMSET: return "memset";
2732 case ISD::MEMCPY: return "memcpy";
2733 case ISD::MEMMOVE: return "memmove";
2736 case ISD::BSWAP: return "bswap";
2737 case ISD::CTPOP: return "ctpop";
2738 case ISD::CTTZ: return "cttz";
2739 case ISD::CTLZ: return "ctlz";
2742 case ISD::LOCATION: return "location";
2743 case ISD::DEBUG_LOC: return "debug_loc";
2744 case ISD::DEBUG_LABEL: return "debug_label";
2747 switch (cast<CondCodeSDNode>(this)->get()) {
2748 default: assert(0 && "Unknown setcc condition!");
2749 case ISD::SETOEQ: return "setoeq";
2750 case ISD::SETOGT: return "setogt";
2751 case ISD::SETOGE: return "setoge";
2752 case ISD::SETOLT: return "setolt";
2753 case ISD::SETOLE: return "setole";
2754 case ISD::SETONE: return "setone";
2756 case ISD::SETO: return "seto";
2757 case ISD::SETUO: return "setuo";
2758 case ISD::SETUEQ: return "setue";
2759 case ISD::SETUGT: return "setugt";
2760 case ISD::SETUGE: return "setuge";
2761 case ISD::SETULT: return "setult";
2762 case ISD::SETULE: return "setule";
2763 case ISD::SETUNE: return "setune";
2765 case ISD::SETEQ: return "seteq";
2766 case ISD::SETGT: return "setgt";
2767 case ISD::SETGE: return "setge";
2768 case ISD::SETLT: return "setlt";
2769 case ISD::SETLE: return "setle";
2770 case ISD::SETNE: return "setne";
2775 const char *SDNode::getAddressingModeName(ISD::MemOpAddrMode AM) {
2784 return "<post-inc>";
2786 return "<post-dec>";
2790 void SDNode::dump() const { dump(0); }
2791 void SDNode::dump(const SelectionDAG *G) const {
2792 std::cerr << (void*)this << ": ";
2794 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2795 if (i) std::cerr << ",";
2796 if (getValueType(i) == MVT::Other)
2799 std::cerr << MVT::getValueTypeString(getValueType(i));
2801 std::cerr << " = " << getOperationName(G);
2804 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2805 if (i) std::cerr << ", ";
2806 std::cerr << (void*)getOperand(i).Val;
2807 if (unsigned RN = getOperand(i).ResNo)
2808 std::cerr << ":" << RN;
2811 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2812 std::cerr << "<" << CSDN->getValue() << ">";
2813 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2814 std::cerr << "<" << CSDN->getValue() << ">";
2815 } else if (const GlobalAddressSDNode *GADN =
2816 dyn_cast<GlobalAddressSDNode>(this)) {
2817 int offset = GADN->getOffset();
2819 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
2821 std::cerr << " + " << offset;
2823 std::cerr << " " << offset;
2824 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
2825 std::cerr << "<" << FIDN->getIndex() << ">";
2826 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
2827 int offset = CP->getOffset();
2828 if (CP->isMachineConstantPoolEntry())
2829 std::cerr << "<" << *CP->getMachineCPVal() << ">";
2831 std::cerr << "<" << *CP->getConstVal() << ">";
2833 std::cerr << " + " << offset;
2835 std::cerr << " " << offset;
2836 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
2838 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2840 std::cerr << LBB->getName() << " ";
2841 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
2842 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
2843 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2844 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2846 std::cerr << " #" << R->getReg();
2848 } else if (const ExternalSymbolSDNode *ES =
2849 dyn_cast<ExternalSymbolSDNode>(this)) {
2850 std::cerr << "'" << ES->getSymbol() << "'";
2851 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2853 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2855 std::cerr << "<null:" << M->getOffset() << ">";
2856 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2857 std::cerr << ":" << getValueTypeString(N->getVT());
2858 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
2860 switch (LD->getExtensionType()) {
2861 default: doExt = false; break;
2863 std::cerr << " <anyext ";
2866 std::cerr << " <sext ";
2869 std::cerr << " <zext ";
2873 std::cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
2875 const char *AM = getAddressingModeName(LD->getAddressingMode());
2877 std::cerr << " " << AM;
2878 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
2879 if (ST->isTruncatingStore())
2880 std::cerr << " <trunc "
2881 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
2883 const char *AM = getAddressingModeName(ST->getAddressingMode());
2885 std::cerr << " " << AM;
2889 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
2890 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2891 if (N->getOperand(i).Val->hasOneUse())
2892 DumpNodes(N->getOperand(i).Val, indent+2, G);
2894 std::cerr << "\n" << std::string(indent+2, ' ')
2895 << (void*)N->getOperand(i).Val << ": <multiple use>";
2898 std::cerr << "\n" << std::string(indent, ' ');
2902 void SelectionDAG::dump() const {
2903 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
2904 std::vector<const SDNode*> Nodes;
2905 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2909 std::sort(Nodes.begin(), Nodes.end());
2911 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2912 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
2913 DumpNodes(Nodes[i], 2, this);
2916 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
2918 std::cerr << "\n\n";
2921 const Type *ConstantPoolSDNode::getType() const {
2922 if (isMachineConstantPoolEntry())
2923 return Val.MachineCPVal->getType();
2924 return Val.ConstVal->getType();