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/GlobalVariable.h"
17 #include "llvm/Intrinsics.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Assembly/Writer.h"
20 #include "llvm/CodeGen/MachineBasicBlock.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Target/MRegisterInfo.h"
24 #include "llvm/Target/TargetData.h"
25 #include "llvm/Target/TargetLowering.h"
26 #include "llvm/Target/TargetInstrInfo.h"
27 #include "llvm/Target/TargetMachine.h"
28 #include "llvm/ADT/SetVector.h"
29 #include "llvm/ADT/SmallPtrSet.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/ADT/StringExtras.h"
36 /// makeVTList - Return an instance of the SDVTList struct initialized with the
37 /// specified members.
38 static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
39 SDVTList Res = {VTs, NumVTs};
43 //===----------------------------------------------------------------------===//
44 // ConstantFPSDNode Class
45 //===----------------------------------------------------------------------===//
47 /// isExactlyValue - We don't rely on operator== working on double values, as
48 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
49 /// As such, this method can be used to do an exact bit-for-bit comparison of
50 /// two floating point values.
51 bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
52 return Value.bitwiseIsEqual(V);
55 bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
57 // convert modifies in place, so make a copy.
58 APFloat Val2 = APFloat(Val);
61 return false; // These can't be represented as floating point!
63 // FIXME rounding mode needs to be more flexible
65 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
66 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) ==
69 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
70 &Val2.getSemantics() == &APFloat::IEEEdouble ||
71 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
73 // TODO: Figure out how to test if we can use a shorter type instead!
81 //===----------------------------------------------------------------------===//
83 //===----------------------------------------------------------------------===//
85 /// isBuildVectorAllOnes - Return true if the specified node is a
86 /// BUILD_VECTOR where all of the elements are ~0 or undef.
87 bool ISD::isBuildVectorAllOnes(const SDNode *N) {
88 // Look through a bit convert.
89 if (N->getOpcode() == ISD::BIT_CONVERT)
90 N = N->getOperand(0).Val;
92 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
94 unsigned i = 0, e = N->getNumOperands();
96 // Skip over all of the undef values.
97 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
100 // Do not accept an all-undef vector.
101 if (i == e) return false;
103 // Do not accept build_vectors that aren't all constants or which have non-~0
105 SDOperand NotZero = N->getOperand(i);
106 if (isa<ConstantSDNode>(NotZero)) {
107 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
109 } else if (isa<ConstantFPSDNode>(NotZero)) {
110 MVT::ValueType VT = NotZero.getValueType();
112 if (((cast<ConstantFPSDNode>(NotZero)->getValueAPF().
113 convertToAPInt().getZExtValue())) != (uint64_t)-1)
116 if ((uint32_t)cast<ConstantFPSDNode>(NotZero)->
117 getValueAPF().convertToAPInt().getZExtValue() !=
124 // Okay, we have at least one ~0 value, check to see if the rest match or are
126 for (++i; i != e; ++i)
127 if (N->getOperand(i) != NotZero &&
128 N->getOperand(i).getOpcode() != ISD::UNDEF)
134 /// isBuildVectorAllZeros - Return true if the specified node is a
135 /// BUILD_VECTOR where all of the elements are 0 or undef.
136 bool ISD::isBuildVectorAllZeros(const SDNode *N) {
137 // Look through a bit convert.
138 if (N->getOpcode() == ISD::BIT_CONVERT)
139 N = N->getOperand(0).Val;
141 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
143 unsigned i = 0, e = N->getNumOperands();
145 // Skip over all of the undef values.
146 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
149 // Do not accept an all-undef vector.
150 if (i == e) return false;
152 // Do not accept build_vectors that aren't all constants or which have non-~0
154 SDOperand Zero = N->getOperand(i);
155 if (isa<ConstantSDNode>(Zero)) {
156 if (!cast<ConstantSDNode>(Zero)->isNullValue())
158 } else if (isa<ConstantFPSDNode>(Zero)) {
159 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
164 // Okay, we have at least one ~0 value, check to see if the rest match or are
166 for (++i; i != e; ++i)
167 if (N->getOperand(i) != Zero &&
168 N->getOperand(i).getOpcode() != ISD::UNDEF)
173 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
174 /// when given the operation for (X op Y).
175 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
176 // To perform this operation, we just need to swap the L and G bits of the
178 unsigned OldL = (Operation >> 2) & 1;
179 unsigned OldG = (Operation >> 1) & 1;
180 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
181 (OldL << 1) | // New G bit
182 (OldG << 2)); // New L bit.
185 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
186 /// 'op' is a valid SetCC operation.
187 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
188 unsigned Operation = Op;
190 Operation ^= 7; // Flip L, G, E bits, but not U.
192 Operation ^= 15; // Flip all of the condition bits.
193 if (Operation > ISD::SETTRUE2)
194 Operation &= ~8; // Don't let N and U bits get set.
195 return ISD::CondCode(Operation);
199 /// isSignedOp - For an integer comparison, return 1 if the comparison is a
200 /// signed operation and 2 if the result is an unsigned comparison. Return zero
201 /// if the operation does not depend on the sign of the input (setne and seteq).
202 static int isSignedOp(ISD::CondCode Opcode) {
204 default: assert(0 && "Illegal integer setcc operation!");
206 case ISD::SETNE: return 0;
210 case ISD::SETGE: return 1;
214 case ISD::SETUGE: return 2;
218 /// getSetCCOrOperation - Return the result of a logical OR between different
219 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
220 /// returns SETCC_INVALID if it is not possible to represent the resultant
222 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
224 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
225 // Cannot fold a signed integer setcc with an unsigned integer setcc.
226 return ISD::SETCC_INVALID;
228 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
230 // If the N and U bits get set then the resultant comparison DOES suddenly
231 // care about orderedness, and is true when ordered.
232 if (Op > ISD::SETTRUE2)
233 Op &= ~16; // Clear the U bit if the N bit is set.
235 // Canonicalize illegal integer setcc's.
236 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
239 return ISD::CondCode(Op);
242 /// getSetCCAndOperation - Return the result of a logical AND between different
243 /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
244 /// function returns zero if it is not possible to represent the resultant
246 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
248 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
249 // Cannot fold a signed setcc with an unsigned setcc.
250 return ISD::SETCC_INVALID;
252 // Combine all of the condition bits.
253 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
255 // Canonicalize illegal integer setcc's.
259 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
260 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
261 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
262 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
269 const TargetMachine &SelectionDAG::getTarget() const {
270 return TLI.getTargetMachine();
273 //===----------------------------------------------------------------------===//
274 // SDNode Profile Support
275 //===----------------------------------------------------------------------===//
277 /// AddNodeIDOpcode - Add the node opcode to the NodeID data.
279 static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
283 /// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
284 /// solely with their pointer.
285 void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
286 ID.AddPointer(VTList.VTs);
289 /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
291 static void AddNodeIDOperands(FoldingSetNodeID &ID,
292 const SDOperand *Ops, unsigned NumOps) {
293 for (; NumOps; --NumOps, ++Ops) {
294 ID.AddPointer(Ops->Val);
295 ID.AddInteger(Ops->ResNo);
299 static void AddNodeIDNode(FoldingSetNodeID &ID,
300 unsigned short OpC, SDVTList VTList,
301 const SDOperand *OpList, unsigned N) {
302 AddNodeIDOpcode(ID, OpC);
303 AddNodeIDValueTypes(ID, VTList);
304 AddNodeIDOperands(ID, OpList, N);
307 /// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
309 static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
310 AddNodeIDOpcode(ID, N->getOpcode());
311 // Add the return value info.
312 AddNodeIDValueTypes(ID, N->getVTList());
313 // Add the operand info.
314 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
316 // Handle SDNode leafs with special info.
317 switch (N->getOpcode()) {
318 default: break; // Normal nodes don't need extra info.
319 case ISD::TargetConstant:
321 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
323 case ISD::TargetConstantFP:
324 case ISD::ConstantFP: {
325 APFloat V = cast<ConstantFPSDNode>(N)->getValueAPF();
326 if (&V.getSemantics() == &APFloat::IEEEdouble)
327 ID.AddDouble(V.convertToDouble());
328 else if (&V.getSemantics() == &APFloat::IEEEsingle)
329 ID.AddDouble((double)V.convertToFloat());
334 case ISD::TargetGlobalAddress:
335 case ISD::GlobalAddress:
336 case ISD::TargetGlobalTLSAddress:
337 case ISD::GlobalTLSAddress: {
338 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
339 ID.AddPointer(GA->getGlobal());
340 ID.AddInteger(GA->getOffset());
343 case ISD::BasicBlock:
344 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
347 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
349 case ISD::SRCVALUE: {
350 SrcValueSDNode *SV = cast<SrcValueSDNode>(N);
351 ID.AddPointer(SV->getValue());
352 ID.AddInteger(SV->getOffset());
355 case ISD::FrameIndex:
356 case ISD::TargetFrameIndex:
357 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
360 case ISD::TargetJumpTable:
361 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
363 case ISD::ConstantPool:
364 case ISD::TargetConstantPool: {
365 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
366 ID.AddInteger(CP->getAlignment());
367 ID.AddInteger(CP->getOffset());
368 if (CP->isMachineConstantPoolEntry())
369 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
371 ID.AddPointer(CP->getConstVal());
375 LoadSDNode *LD = cast<LoadSDNode>(N);
376 ID.AddInteger(LD->getAddressingMode());
377 ID.AddInteger(LD->getExtensionType());
378 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
379 ID.AddPointer(LD->getSrcValue());
380 ID.AddInteger(LD->getSrcValueOffset());
381 ID.AddInteger(LD->getAlignment());
382 ID.AddInteger(LD->isVolatile());
386 StoreSDNode *ST = cast<StoreSDNode>(N);
387 ID.AddInteger(ST->getAddressingMode());
388 ID.AddInteger(ST->isTruncatingStore());
389 ID.AddInteger((unsigned int)(ST->getStoredVT()));
390 ID.AddPointer(ST->getSrcValue());
391 ID.AddInteger(ST->getSrcValueOffset());
392 ID.AddInteger(ST->getAlignment());
393 ID.AddInteger(ST->isVolatile());
399 //===----------------------------------------------------------------------===//
400 // SelectionDAG Class
401 //===----------------------------------------------------------------------===//
403 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
405 void SelectionDAG::RemoveDeadNodes() {
406 // Create a dummy node (which is not added to allnodes), that adds a reference
407 // to the root node, preventing it from being deleted.
408 HandleSDNode Dummy(getRoot());
410 SmallVector<SDNode*, 128> DeadNodes;
412 // Add all obviously-dead nodes to the DeadNodes worklist.
413 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
415 DeadNodes.push_back(I);
417 // Process the worklist, deleting the nodes and adding their uses to the
419 while (!DeadNodes.empty()) {
420 SDNode *N = DeadNodes.back();
421 DeadNodes.pop_back();
423 // Take the node out of the appropriate CSE map.
424 RemoveNodeFromCSEMaps(N);
426 // Next, brutally remove the operand list. This is safe to do, as there are
427 // no cycles in the graph.
428 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
429 SDNode *Operand = I->Val;
430 Operand->removeUser(N);
432 // Now that we removed this operand, see if there are no uses of it left.
433 if (Operand->use_empty())
434 DeadNodes.push_back(Operand);
436 if (N->OperandsNeedDelete)
437 delete[] N->OperandList;
441 // Finally, remove N itself.
445 // If the root changed (e.g. it was a dead load, update the root).
446 setRoot(Dummy.getValue());
449 void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
450 SmallVector<SDNode*, 16> DeadNodes;
451 DeadNodes.push_back(N);
453 // Process the worklist, deleting the nodes and adding their uses to the
455 while (!DeadNodes.empty()) {
456 SDNode *N = DeadNodes.back();
457 DeadNodes.pop_back();
459 // Take the node out of the appropriate CSE map.
460 RemoveNodeFromCSEMaps(N);
462 // Next, brutally remove the operand list. This is safe to do, as there are
463 // no cycles in the graph.
464 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
465 SDNode *Operand = I->Val;
466 Operand->removeUser(N);
468 // Now that we removed this operand, see if there are no uses of it left.
469 if (Operand->use_empty())
470 DeadNodes.push_back(Operand);
472 if (N->OperandsNeedDelete)
473 delete[] N->OperandList;
477 // Finally, remove N itself.
478 Deleted.push_back(N);
483 void SelectionDAG::DeleteNode(SDNode *N) {
484 assert(N->use_empty() && "Cannot delete a node that is not dead!");
486 // First take this out of the appropriate CSE map.
487 RemoveNodeFromCSEMaps(N);
489 // Finally, remove uses due to operands of this node, remove from the
490 // AllNodes list, and delete the node.
491 DeleteNodeNotInCSEMaps(N);
494 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
496 // Remove it from the AllNodes list.
499 // Drop all of the operands and decrement used nodes use counts.
500 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
501 I->Val->removeUser(N);
502 if (N->OperandsNeedDelete)
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.
590 SDOperand Ops[] = { Op };
592 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
593 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
596 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
597 /// were replaced with those specified. If this node is never memoized,
598 /// return null, otherwise return a pointer to the slot it would take. If a
599 /// node already exists with these operands, the slot will be non-null.
600 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
601 SDOperand Op1, SDOperand Op2,
603 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
604 return 0; // Never add these nodes.
606 // Check that remaining values produced are not flags.
607 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
608 if (N->getValueType(i) == MVT::Flag)
609 return 0; // Never CSE anything that produces a flag.
611 SDOperand Ops[] = { Op1, Op2 };
613 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
614 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
618 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
619 /// were replaced with those specified. If this node is never memoized,
620 /// return null, otherwise return a pointer to the slot it would take. If a
621 /// node already exists with these operands, the slot will be non-null.
622 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
623 const SDOperand *Ops,unsigned NumOps,
625 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
626 return 0; // Never add these nodes.
628 // Check that remaining values produced are not flags.
629 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
630 if (N->getValueType(i) == MVT::Flag)
631 return 0; // Never CSE anything that produces a flag.
634 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
636 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
637 ID.AddInteger(LD->getAddressingMode());
638 ID.AddInteger(LD->getExtensionType());
639 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
640 ID.AddPointer(LD->getSrcValue());
641 ID.AddInteger(LD->getSrcValueOffset());
642 ID.AddInteger(LD->getAlignment());
643 ID.AddInteger(LD->isVolatile());
644 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
645 ID.AddInteger(ST->getAddressingMode());
646 ID.AddInteger(ST->isTruncatingStore());
647 ID.AddInteger((unsigned int)(ST->getStoredVT()));
648 ID.AddPointer(ST->getSrcValue());
649 ID.AddInteger(ST->getSrcValueOffset());
650 ID.AddInteger(ST->getAlignment());
651 ID.AddInteger(ST->isVolatile());
654 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
658 SelectionDAG::~SelectionDAG() {
659 while (!AllNodes.empty()) {
660 SDNode *N = AllNodes.begin();
661 N->SetNextInBucket(0);
662 if (N->OperandsNeedDelete)
663 delete [] N->OperandList;
666 AllNodes.pop_front();
670 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
671 if (Op.getValueType() == VT) return Op;
672 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
673 return getNode(ISD::AND, Op.getValueType(), Op,
674 getConstant(Imm, Op.getValueType()));
677 SDOperand SelectionDAG::getString(const std::string &Val) {
678 StringSDNode *&N = StringNodes[Val];
680 N = new StringSDNode(Val);
681 AllNodes.push_back(N);
683 return SDOperand(N, 0);
686 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
687 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
688 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
690 // Mask out any bits that are not valid for this constant.
691 Val &= MVT::getIntVTBitMask(VT);
693 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
695 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
698 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
699 return SDOperand(E, 0);
700 SDNode *N = new ConstantSDNode(isT, Val, VT);
701 CSEMap.InsertNode(N, IP);
702 AllNodes.push_back(N);
703 return SDOperand(N, 0);
706 SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
708 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
710 MVT::ValueType EltVT =
711 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
712 bool isDouble = (EltVT == MVT::f64);
713 double Val = isDouble ? V.convertToDouble() : (double)V.convertToFloat();
715 // Do the map lookup using the actual bit pattern for the floating point
716 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
717 // we don't have issues with SNANs.
718 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
719 // ?? Should we store float/double/longdouble separately in ID?
721 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
725 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
726 if (!MVT::isVector(VT))
727 return SDOperand(N, 0);
729 N = new ConstantFPSDNode(isTarget,
730 isDouble ? APFloat(Val) : APFloat((float)Val), EltVT);
731 CSEMap.InsertNode(N, IP);
732 AllNodes.push_back(N);
735 SDOperand Result(N, 0);
736 if (MVT::isVector(VT)) {
737 SmallVector<SDOperand, 8> Ops;
738 Ops.assign(MVT::getVectorNumElements(VT), Result);
739 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
744 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
746 MVT::ValueType EltVT =
747 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
749 return getConstantFP(APFloat((float)Val), VT, isTarget);
751 return getConstantFP(APFloat(Val), VT, isTarget);
754 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
755 MVT::ValueType VT, int Offset,
757 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
759 if (GVar && GVar->isThreadLocal())
760 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
762 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
764 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
766 ID.AddInteger(Offset);
768 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
769 return SDOperand(E, 0);
770 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
771 CSEMap.InsertNode(N, IP);
772 AllNodes.push_back(N);
773 return SDOperand(N, 0);
776 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
778 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
780 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
783 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
784 return SDOperand(E, 0);
785 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
786 CSEMap.InsertNode(N, IP);
787 AllNodes.push_back(N);
788 return SDOperand(N, 0);
791 SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
792 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
794 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
797 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
798 return SDOperand(E, 0);
799 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
800 CSEMap.InsertNode(N, IP);
801 AllNodes.push_back(N);
802 return SDOperand(N, 0);
805 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
806 unsigned Alignment, int Offset,
808 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
810 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
811 ID.AddInteger(Alignment);
812 ID.AddInteger(Offset);
815 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
816 return SDOperand(E, 0);
817 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
818 CSEMap.InsertNode(N, IP);
819 AllNodes.push_back(N);
820 return SDOperand(N, 0);
824 SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
826 unsigned Alignment, int Offset,
828 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
830 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
831 ID.AddInteger(Alignment);
832 ID.AddInteger(Offset);
833 C->AddSelectionDAGCSEId(ID);
835 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
836 return SDOperand(E, 0);
837 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
838 CSEMap.InsertNode(N, IP);
839 AllNodes.push_back(N);
840 return SDOperand(N, 0);
844 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
846 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
849 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
850 return SDOperand(E, 0);
851 SDNode *N = new BasicBlockSDNode(MBB);
852 CSEMap.InsertNode(N, IP);
853 AllNodes.push_back(N);
854 return SDOperand(N, 0);
857 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
858 if ((unsigned)VT >= ValueTypeNodes.size())
859 ValueTypeNodes.resize(VT+1);
860 if (ValueTypeNodes[VT] == 0) {
861 ValueTypeNodes[VT] = new VTSDNode(VT);
862 AllNodes.push_back(ValueTypeNodes[VT]);
865 return SDOperand(ValueTypeNodes[VT], 0);
868 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
869 SDNode *&N = ExternalSymbols[Sym];
870 if (N) return SDOperand(N, 0);
871 N = new ExternalSymbolSDNode(false, Sym, VT);
872 AllNodes.push_back(N);
873 return SDOperand(N, 0);
876 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
878 SDNode *&N = TargetExternalSymbols[Sym];
879 if (N) return SDOperand(N, 0);
880 N = new ExternalSymbolSDNode(true, Sym, VT);
881 AllNodes.push_back(N);
882 return SDOperand(N, 0);
885 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
886 if ((unsigned)Cond >= CondCodeNodes.size())
887 CondCodeNodes.resize(Cond+1);
889 if (CondCodeNodes[Cond] == 0) {
890 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
891 AllNodes.push_back(CondCodeNodes[Cond]);
893 return SDOperand(CondCodeNodes[Cond], 0);
896 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
898 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
899 ID.AddInteger(RegNo);
901 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
902 return SDOperand(E, 0);
903 SDNode *N = new RegisterSDNode(RegNo, VT);
904 CSEMap.InsertNode(N, IP);
905 AllNodes.push_back(N);
906 return SDOperand(N, 0);
909 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
910 assert((!V || isa<PointerType>(V->getType())) &&
911 "SrcValue is not a pointer?");
914 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
916 ID.AddInteger(Offset);
918 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
919 return SDOperand(E, 0);
920 SDNode *N = new SrcValueSDNode(V, Offset);
921 CSEMap.InsertNode(N, IP);
922 AllNodes.push_back(N);
923 return SDOperand(N, 0);
926 SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
927 SDOperand N2, ISD::CondCode Cond) {
928 // These setcc operations always fold.
932 case ISD::SETFALSE2: return getConstant(0, VT);
934 case ISD::SETTRUE2: return getConstant(1, VT);
946 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
950 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
951 uint64_t C2 = N2C->getValue();
952 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
953 uint64_t C1 = N1C->getValue();
955 // Sign extend the operands if required
956 if (ISD::isSignedIntSetCC(Cond)) {
957 C1 = N1C->getSignExtended();
958 C2 = N2C->getSignExtended();
962 default: assert(0 && "Unknown integer setcc!");
963 case ISD::SETEQ: return getConstant(C1 == C2, VT);
964 case ISD::SETNE: return getConstant(C1 != C2, VT);
965 case ISD::SETULT: return getConstant(C1 < C2, VT);
966 case ISD::SETUGT: return getConstant(C1 > C2, VT);
967 case ISD::SETULE: return getConstant(C1 <= C2, VT);
968 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
969 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
970 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
971 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
972 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
976 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
977 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
979 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
982 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
983 return getNode(ISD::UNDEF, VT);
985 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
986 case ISD::SETNE: if (R==APFloat::cmpUnordered)
987 return getNode(ISD::UNDEF, VT);
989 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
990 R==APFloat::cmpLessThan, VT);
991 case ISD::SETLT: if (R==APFloat::cmpUnordered)
992 return getNode(ISD::UNDEF, VT);
994 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
995 case ISD::SETGT: if (R==APFloat::cmpUnordered)
996 return getNode(ISD::UNDEF, VT);
998 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
999 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1000 return getNode(ISD::UNDEF, VT);
1002 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
1003 R==APFloat::cmpEqual, VT);
1004 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1005 return getNode(ISD::UNDEF, VT);
1007 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
1008 R==APFloat::cmpEqual, VT);
1009 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1010 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1011 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1012 R==APFloat::cmpEqual, VT);
1013 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1014 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1015 R==APFloat::cmpLessThan, VT);
1016 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1017 R==APFloat::cmpUnordered, VT);
1018 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1019 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
1022 // Ensure that the constant occurs on the RHS.
1023 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
1026 // Could not fold it.
1030 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1031 /// this predicate to simplify operations downstream. Mask is known to be zero
1032 /// for bits that V cannot have.
1033 bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1034 unsigned Depth) const {
1035 // The masks are not wide enough to represent this type! Should use APInt.
1036 if (Op.getValueType() == MVT::i128)
1039 uint64_t KnownZero, KnownOne;
1040 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1041 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1042 return (KnownZero & Mask) == Mask;
1045 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
1046 /// known to be either zero or one and return them in the KnownZero/KnownOne
1047 /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1049 void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1050 uint64_t &KnownZero, uint64_t &KnownOne,
1051 unsigned Depth) const {
1052 KnownZero = KnownOne = 0; // Don't know anything.
1053 if (Depth == 6 || Mask == 0)
1054 return; // Limit search depth.
1056 // The masks are not wide enough to represent this type! Should use APInt.
1057 if (Op.getValueType() == MVT::i128)
1060 uint64_t KnownZero2, KnownOne2;
1062 switch (Op.getOpcode()) {
1064 // We know all of the bits for a constant!
1065 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1066 KnownZero = ~KnownOne & Mask;
1069 // If either the LHS or the RHS are Zero, the result is zero.
1070 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1072 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1073 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1074 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1076 // Output known-1 bits are only known if set in both the LHS & RHS.
1077 KnownOne &= KnownOne2;
1078 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1079 KnownZero |= KnownZero2;
1082 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1084 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1085 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1086 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1088 // Output known-0 bits are only known if clear in both the LHS & RHS.
1089 KnownZero &= KnownZero2;
1090 // Output known-1 are known to be set if set in either the LHS | RHS.
1091 KnownOne |= KnownOne2;
1094 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1095 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1096 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1097 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1099 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1100 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1101 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1102 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1103 KnownZero = KnownZeroOut;
1107 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1108 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1109 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1110 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1112 // Only known if known in both the LHS and RHS.
1113 KnownOne &= KnownOne2;
1114 KnownZero &= KnownZero2;
1116 case ISD::SELECT_CC:
1117 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1118 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1119 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1120 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1122 // Only known if known in both the LHS and RHS.
1123 KnownOne &= KnownOne2;
1124 KnownZero &= KnownZero2;
1127 // If we know the result of a setcc has the top bits zero, use this info.
1128 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1129 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1132 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1133 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1134 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1135 KnownZero, KnownOne, Depth+1);
1136 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1137 KnownZero <<= SA->getValue();
1138 KnownOne <<= SA->getValue();
1139 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1143 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1144 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1145 MVT::ValueType VT = Op.getValueType();
1146 unsigned ShAmt = SA->getValue();
1148 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1149 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1150 KnownZero, KnownOne, Depth+1);
1151 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1152 KnownZero &= TypeMask;
1153 KnownOne &= TypeMask;
1154 KnownZero >>= ShAmt;
1157 uint64_t HighBits = (1ULL << ShAmt)-1;
1158 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1159 KnownZero |= HighBits; // High bits known zero.
1163 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1164 MVT::ValueType VT = Op.getValueType();
1165 unsigned ShAmt = SA->getValue();
1167 // Compute the new bits that are at the top now.
1168 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1170 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1171 // If any of the demanded bits are produced by the sign extension, we also
1172 // demand the input sign bit.
1173 uint64_t HighBits = (1ULL << ShAmt)-1;
1174 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1175 if (HighBits & Mask)
1176 InDemandedMask |= MVT::getIntVTSignBit(VT);
1178 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1180 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1181 KnownZero &= TypeMask;
1182 KnownOne &= TypeMask;
1183 KnownZero >>= ShAmt;
1186 // Handle the sign bits.
1187 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1188 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1190 if (KnownZero & SignBit) {
1191 KnownZero |= HighBits; // New bits are known zero.
1192 } else if (KnownOne & SignBit) {
1193 KnownOne |= HighBits; // New bits are known one.
1197 case ISD::SIGN_EXTEND_INREG: {
1198 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1200 // Sign extension. Compute the demanded bits in the result that are not
1201 // present in the input.
1202 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1204 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1205 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1207 // If the sign extended bits are demanded, we know that the sign
1210 InputDemandedBits |= InSignBit;
1212 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1213 KnownZero, KnownOne, Depth+1);
1214 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1216 // If the sign bit of the input is known set or clear, then we know the
1217 // top bits of the result.
1218 if (KnownZero & InSignBit) { // Input sign bit known clear
1219 KnownZero |= NewBits;
1220 KnownOne &= ~NewBits;
1221 } else if (KnownOne & InSignBit) { // Input sign bit known set
1222 KnownOne |= NewBits;
1223 KnownZero &= ~NewBits;
1224 } else { // Input sign bit unknown
1225 KnownZero &= ~NewBits;
1226 KnownOne &= ~NewBits;
1233 MVT::ValueType VT = Op.getValueType();
1234 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1235 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1240 if (ISD::isZEXTLoad(Op.Val)) {
1241 LoadSDNode *LD = cast<LoadSDNode>(Op);
1242 MVT::ValueType VT = LD->getLoadedVT();
1243 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1247 case ISD::ZERO_EXTEND: {
1248 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1249 uint64_t NewBits = (~InMask) & Mask;
1250 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1252 KnownZero |= NewBits & Mask;
1253 KnownOne &= ~NewBits;
1256 case ISD::SIGN_EXTEND: {
1257 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1258 unsigned InBits = MVT::getSizeInBits(InVT);
1259 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1260 uint64_t InSignBit = 1ULL << (InBits-1);
1261 uint64_t NewBits = (~InMask) & Mask;
1262 uint64_t InDemandedBits = Mask & InMask;
1264 // If any of the sign extended bits are demanded, we know that the sign
1267 InDemandedBits |= InSignBit;
1269 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1271 // If the sign bit is known zero or one, the top bits match.
1272 if (KnownZero & InSignBit) {
1273 KnownZero |= NewBits;
1274 KnownOne &= ~NewBits;
1275 } else if (KnownOne & InSignBit) {
1276 KnownOne |= NewBits;
1277 KnownZero &= ~NewBits;
1278 } else { // Otherwise, top bits aren't known.
1279 KnownOne &= ~NewBits;
1280 KnownZero &= ~NewBits;
1284 case ISD::ANY_EXTEND: {
1285 MVT::ValueType VT = Op.getOperand(0).getValueType();
1286 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1287 KnownZero, KnownOne, Depth+1);
1290 case ISD::TRUNCATE: {
1291 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1292 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1293 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1294 KnownZero &= OutMask;
1295 KnownOne &= OutMask;
1298 case ISD::AssertZext: {
1299 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1300 uint64_t InMask = MVT::getIntVTBitMask(VT);
1301 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1303 KnownZero |= (~InMask) & Mask;
1307 // If either the LHS or the RHS are Zero, the result is zero.
1308 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1309 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1310 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1311 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1313 // Output known-0 bits are known if clear or set in both the low clear bits
1314 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1315 // low 3 bits clear.
1316 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1317 CountTrailingZeros_64(~KnownZero2));
1319 KnownZero = (1ULL << KnownZeroOut) - 1;
1324 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1327 // We know that the top bits of C-X are clear if X contains less bits
1328 // than C (i.e. no wrap-around can happen). For example, 20-X is
1329 // positive if we can prove that X is >= 0 and < 16.
1330 MVT::ValueType VT = CLHS->getValueType(0);
1331 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1332 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1333 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1334 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1335 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1337 // If all of the MaskV bits are known to be zero, then we know the output
1338 // top bits are zero, because we now know that the output is from [0-C].
1339 if ((KnownZero & MaskV) == MaskV) {
1340 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1341 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1342 KnownOne = 0; // No one bits known.
1344 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1350 // Allow the target to implement this method for its nodes.
1351 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1352 case ISD::INTRINSIC_WO_CHAIN:
1353 case ISD::INTRINSIC_W_CHAIN:
1354 case ISD::INTRINSIC_VOID:
1355 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1361 /// ComputeNumSignBits - Return the number of times the sign bit of the
1362 /// register is replicated into the other bits. We know that at least 1 bit
1363 /// is always equal to the sign bit (itself), but other cases can give us
1364 /// information. For example, immediately after an "SRA X, 2", we know that
1365 /// the top 3 bits are all equal to each other, so we return 3.
1366 unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1367 MVT::ValueType VT = Op.getValueType();
1368 assert(MVT::isInteger(VT) && "Invalid VT!");
1369 unsigned VTBits = MVT::getSizeInBits(VT);
1373 return 1; // Limit search depth.
1375 switch (Op.getOpcode()) {
1377 case ISD::AssertSext:
1378 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1379 return VTBits-Tmp+1;
1380 case ISD::AssertZext:
1381 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1384 case ISD::Constant: {
1385 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1386 // If negative, invert the bits, then look at it.
1387 if (Val & MVT::getIntVTSignBit(VT))
1390 // Shift the bits so they are the leading bits in the int64_t.
1393 // Return # leading zeros. We use 'min' here in case Val was zero before
1394 // shifting. We don't want to return '64' as for an i32 "0".
1395 return std::min(VTBits, CountLeadingZeros_64(Val));
1398 case ISD::SIGN_EXTEND:
1399 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1400 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1402 case ISD::SIGN_EXTEND_INREG:
1403 // Max of the input and what this extends.
1404 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1407 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1408 return std::max(Tmp, Tmp2);
1411 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1412 // SRA X, C -> adds C sign bits.
1413 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1414 Tmp += C->getValue();
1415 if (Tmp > VTBits) Tmp = VTBits;
1419 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1420 // shl destroys sign bits.
1421 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1422 if (C->getValue() >= VTBits || // Bad shift.
1423 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1424 return Tmp - C->getValue();
1429 case ISD::XOR: // NOT is handled here.
1430 // Logical binary ops preserve the number of sign bits.
1431 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1432 if (Tmp == 1) return 1; // Early out.
1433 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1434 return std::min(Tmp, Tmp2);
1437 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1438 if (Tmp == 1) return 1; // Early out.
1439 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1440 return std::min(Tmp, Tmp2);
1443 // If setcc returns 0/-1, all bits are sign bits.
1444 if (TLI.getSetCCResultContents() ==
1445 TargetLowering::ZeroOrNegativeOneSetCCResult)
1450 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1451 unsigned RotAmt = C->getValue() & (VTBits-1);
1453 // Handle rotate right by N like a rotate left by 32-N.
1454 if (Op.getOpcode() == ISD::ROTR)
1455 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1457 // If we aren't rotating out all of the known-in sign bits, return the
1458 // number that are left. This handles rotl(sext(x), 1) for example.
1459 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1460 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1464 // Add can have at most one carry bit. Thus we know that the output
1465 // is, at worst, one more bit than the inputs.
1466 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1467 if (Tmp == 1) return 1; // Early out.
1469 // Special case decrementing a value (ADD X, -1):
1470 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1471 if (CRHS->isAllOnesValue()) {
1472 uint64_t KnownZero, KnownOne;
1473 uint64_t Mask = MVT::getIntVTBitMask(VT);
1474 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1476 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1478 if ((KnownZero|1) == Mask)
1481 // If we are subtracting one from a positive number, there is no carry
1482 // out of the result.
1483 if (KnownZero & MVT::getIntVTSignBit(VT))
1487 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1488 if (Tmp2 == 1) return 1;
1489 return std::min(Tmp, Tmp2)-1;
1493 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1494 if (Tmp2 == 1) return 1;
1497 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1498 if (CLHS->getValue() == 0) {
1499 uint64_t KnownZero, KnownOne;
1500 uint64_t Mask = MVT::getIntVTBitMask(VT);
1501 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1502 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1504 if ((KnownZero|1) == Mask)
1507 // If the input is known to be positive (the sign bit is known clear),
1508 // the output of the NEG has the same number of sign bits as the input.
1509 if (KnownZero & MVT::getIntVTSignBit(VT))
1512 // Otherwise, we treat this like a SUB.
1515 // Sub can have at most one carry bit. Thus we know that the output
1516 // is, at worst, one more bit than the inputs.
1517 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1518 if (Tmp == 1) return 1; // Early out.
1519 return std::min(Tmp, Tmp2)-1;
1522 // FIXME: it's tricky to do anything useful for this, but it is an important
1523 // case for targets like X86.
1527 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1528 if (Op.getOpcode() == ISD::LOAD) {
1529 LoadSDNode *LD = cast<LoadSDNode>(Op);
1530 unsigned ExtType = LD->getExtensionType();
1533 case ISD::SEXTLOAD: // '17' bits known
1534 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1535 return VTBits-Tmp+1;
1536 case ISD::ZEXTLOAD: // '16' bits known
1537 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1542 // Allow the target to implement this method for its nodes.
1543 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1544 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1545 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1546 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1547 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1548 if (NumBits > 1) return NumBits;
1551 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1552 // use this information.
1553 uint64_t KnownZero, KnownOne;
1554 uint64_t Mask = MVT::getIntVTBitMask(VT);
1555 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1557 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1558 if (KnownZero & SignBit) { // SignBit is 0
1560 } else if (KnownOne & SignBit) { // SignBit is 1;
1567 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1568 // the number of identical bits in the top of the input value.
1571 // Return # leading zeros. We use 'min' here in case Val was zero before
1572 // shifting. We don't want to return '64' as for an i32 "0".
1573 return std::min(VTBits, CountLeadingZeros_64(Mask));
1577 /// getNode - Gets or creates the specified node.
1579 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
1580 FoldingSetNodeID ID;
1581 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
1583 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1584 return SDOperand(E, 0);
1585 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
1586 CSEMap.InsertNode(N, IP);
1588 AllNodes.push_back(N);
1589 return SDOperand(N, 0);
1592 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1593 SDOperand Operand) {
1595 // Constant fold unary operations with an integer constant operand.
1596 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1597 uint64_t Val = C->getValue();
1600 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
1601 case ISD::ANY_EXTEND:
1602 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1603 case ISD::TRUNCATE: return getConstant(Val, VT);
1604 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1605 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
1606 case ISD::BIT_CONVERT:
1607 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
1608 return getConstantFP(BitsToFloat(Val), VT);
1609 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
1610 return getConstantFP(BitsToDouble(Val), VT);
1614 default: assert(0 && "Invalid bswap!"); break;
1615 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1616 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1617 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1622 default: assert(0 && "Invalid ctpop!"); break;
1623 case MVT::i1: return getConstant(Val != 0, VT);
1625 Tmp1 = (unsigned)Val & 0xFF;
1626 return getConstant(CountPopulation_32(Tmp1), VT);
1628 Tmp1 = (unsigned)Val & 0xFFFF;
1629 return getConstant(CountPopulation_32(Tmp1), VT);
1631 return getConstant(CountPopulation_32((unsigned)Val), VT);
1633 return getConstant(CountPopulation_64(Val), VT);
1637 default: assert(0 && "Invalid ctlz!"); break;
1638 case MVT::i1: return getConstant(Val == 0, VT);
1640 Tmp1 = (unsigned)Val & 0xFF;
1641 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1643 Tmp1 = (unsigned)Val & 0xFFFF;
1644 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1646 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1648 return getConstant(CountLeadingZeros_64(Val), VT);
1652 default: assert(0 && "Invalid cttz!"); break;
1653 case MVT::i1: return getConstant(Val == 0, VT);
1655 Tmp1 = (unsigned)Val | 0x100;
1656 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1658 Tmp1 = (unsigned)Val | 0x10000;
1659 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1661 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1663 return getConstant(CountTrailingZeros_64(Val), VT);
1668 // Constant fold unary operations with a floating point constant operand.
1669 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1670 APFloat V = C->getValueAPF(); // make copy
1674 return getConstantFP(V, VT);
1677 return getConstantFP(V, VT);
1679 case ISD::FP_EXTEND:
1680 // This can return overflow, underflow, or inexact; we don't care.
1681 // FIXME need to be more flexible about rounding mode.
1682 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle :
1683 APFloat::IEEEdouble,
1684 APFloat::rmNearestTiesToEven);
1685 return getConstantFP(V, VT);
1686 case ISD::FP_TO_SINT:
1687 case ISD::FP_TO_UINT: {
1689 assert(integerPartWidth >= 64);
1690 // FIXME need to be more flexible about rounding mode.
1691 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1692 Opcode==ISD::FP_TO_SINT,
1693 APFloat::rmTowardZero);
1694 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1696 return getConstant(x, VT);
1698 case ISD::BIT_CONVERT:
1699 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1700 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1701 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1702 return getConstant(V.convertToAPInt().getZExtValue(), VT);
1707 unsigned OpOpcode = Operand.Val->getOpcode();
1709 case ISD::TokenFactor:
1710 return Operand; // Factor of one node? No factor.
1712 case ISD::FP_EXTEND:
1713 assert(MVT::isFloatingPoint(VT) &&
1714 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
1716 case ISD::SIGN_EXTEND:
1717 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1718 "Invalid SIGN_EXTEND!");
1719 if (Operand.getValueType() == VT) return Operand; // noop extension
1720 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
1721 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1722 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1724 case ISD::ZERO_EXTEND:
1725 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1726 "Invalid ZERO_EXTEND!");
1727 if (Operand.getValueType() == VT) return Operand; // noop extension
1728 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
1729 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1730 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1732 case ISD::ANY_EXTEND:
1733 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1734 "Invalid ANY_EXTEND!");
1735 if (Operand.getValueType() == VT) return Operand; // noop extension
1736 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
1737 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1738 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1739 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1742 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1743 "Invalid TRUNCATE!");
1744 if (Operand.getValueType() == VT) return Operand; // noop truncate
1745 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
1746 if (OpOpcode == ISD::TRUNCATE)
1747 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1748 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1749 OpOpcode == ISD::ANY_EXTEND) {
1750 // If the source is smaller than the dest, we still need an extend.
1751 if (Operand.Val->getOperand(0).getValueType() < VT)
1752 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1753 else if (Operand.Val->getOperand(0).getValueType() > VT)
1754 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1756 return Operand.Val->getOperand(0);
1759 case ISD::BIT_CONVERT:
1760 // Basic sanity checking.
1761 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
1762 && "Cannot BIT_CONVERT between types of different sizes!");
1763 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1764 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1765 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1766 if (OpOpcode == ISD::UNDEF)
1767 return getNode(ISD::UNDEF, VT);
1769 case ISD::SCALAR_TO_VECTOR:
1770 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1771 MVT::getVectorElementType(VT) == Operand.getValueType() &&
1772 "Illegal SCALAR_TO_VECTOR node!");
1775 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1776 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1777 Operand.Val->getOperand(0));
1778 if (OpOpcode == ISD::FNEG) // --X -> X
1779 return Operand.Val->getOperand(0);
1782 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1783 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1788 SDVTList VTs = getVTList(VT);
1789 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1790 FoldingSetNodeID ID;
1791 SDOperand Ops[1] = { Operand };
1792 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
1794 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1795 return SDOperand(E, 0);
1796 N = new UnarySDNode(Opcode, VTs, Operand);
1797 CSEMap.InsertNode(N, IP);
1799 N = new UnarySDNode(Opcode, VTs, Operand);
1801 AllNodes.push_back(N);
1802 return SDOperand(N, 0);
1807 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1808 SDOperand N1, SDOperand N2) {
1811 case ISD::TokenFactor:
1812 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1813 N2.getValueType() == MVT::Other && "Invalid token factor!");
1822 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1829 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1836 assert(N1.getValueType() == N2.getValueType() &&
1837 N1.getValueType() == VT && "Binary operator types must match!");
1839 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1840 assert(N1.getValueType() == VT &&
1841 MVT::isFloatingPoint(N1.getValueType()) &&
1842 MVT::isFloatingPoint(N2.getValueType()) &&
1843 "Invalid FCOPYSIGN!");
1850 assert(VT == N1.getValueType() &&
1851 "Shift operators return type must be the same as their first arg");
1852 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1853 VT != MVT::i1 && "Shifts only work on integers");
1855 case ISD::FP_ROUND_INREG: {
1856 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1857 assert(VT == N1.getValueType() && "Not an inreg round!");
1858 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1859 "Cannot FP_ROUND_INREG integer types");
1860 assert(EVT <= VT && "Not rounding down!");
1863 case ISD::AssertSext:
1864 case ISD::AssertZext:
1865 case ISD::SIGN_EXTEND_INREG: {
1866 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1867 assert(VT == N1.getValueType() && "Not an inreg extend!");
1868 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1869 "Cannot *_EXTEND_INREG FP types");
1870 assert(EVT <= VT && "Not extending!");
1877 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1878 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1880 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1881 int64_t Val = N1C->getValue();
1882 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1883 Val <<= 64-FromBits;
1884 Val >>= 64-FromBits;
1885 return getConstant(Val, VT);
1889 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1891 case ISD::ADD: return getConstant(C1 + C2, VT);
1892 case ISD::SUB: return getConstant(C1 - C2, VT);
1893 case ISD::MUL: return getConstant(C1 * C2, VT);
1895 if (C2) return getConstant(C1 / C2, VT);
1898 if (C2) return getConstant(C1 % C2, VT);
1901 if (C2) return getConstant(N1C->getSignExtended() /
1902 N2C->getSignExtended(), VT);
1905 if (C2) return getConstant(N1C->getSignExtended() %
1906 N2C->getSignExtended(), VT);
1908 case ISD::AND : return getConstant(C1 & C2, VT);
1909 case ISD::OR : return getConstant(C1 | C2, VT);
1910 case ISD::XOR : return getConstant(C1 ^ C2, VT);
1911 case ISD::SHL : return getConstant(C1 << C2, VT);
1912 case ISD::SRL : return getConstant(C1 >> C2, VT);
1913 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
1915 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1918 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1922 } else { // Cannonicalize constant to RHS if commutative
1923 if (isCommutativeBinOp(Opcode)) {
1924 std::swap(N1C, N2C);
1930 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1931 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1934 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
1935 APFloat::opStatus s;
1938 s = V1.add(V2, APFloat::rmNearestTiesToEven);
1939 if (s!=APFloat::opInvalidOp)
1940 return getConstantFP(V1, VT);
1943 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
1944 if (s!=APFloat::opInvalidOp)
1945 return getConstantFP(V1, VT);
1948 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
1949 if (s!=APFloat::opInvalidOp)
1950 return getConstantFP(V1, VT);
1953 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
1954 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1955 return getConstantFP(V1, VT);
1958 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
1959 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1960 return getConstantFP(V1, VT);
1962 case ISD::FCOPYSIGN:
1964 return getConstantFP(V1, VT);
1967 } else { // Cannonicalize constant to RHS if commutative
1968 if (isCommutativeBinOp(Opcode)) {
1969 std::swap(N1CFP, N2CFP);
1975 // Canonicalize an UNDEF to the RHS, even over a constant.
1976 if (N1.getOpcode() == ISD::UNDEF) {
1977 if (isCommutativeBinOp(Opcode)) {
1981 case ISD::FP_ROUND_INREG:
1982 case ISD::SIGN_EXTEND_INREG:
1988 return N1; // fold op(undef, arg2) -> undef
1995 if (!MVT::isVector(VT))
1996 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1997 // For vectors, we can't easily build an all zero vector, just return
2004 // Fold a bunch of operators when the RHS is undef.
2005 if (N2.getOpcode() == ISD::UNDEF) {
2021 return N2; // fold op(arg1, undef) -> undef
2026 if (!MVT::isVector(VT))
2027 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2028 // For vectors, we can't easily build an all zero vector, just return
2032 if (!MVT::isVector(VT))
2033 return getConstant(MVT::getIntVTBitMask(VT), VT);
2034 // For vectors, we can't easily build an all one vector, just return
2044 case ISD::TokenFactor:
2045 // Fold trivial token factors.
2046 if (N1.getOpcode() == ISD::EntryToken) return N2;
2047 if (N2.getOpcode() == ISD::EntryToken) return N1;
2051 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2052 // worth handling here.
2053 if (N2C && N2C->getValue() == 0)
2058 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
2059 // worth handling here.
2060 if (N2C && N2C->getValue() == 0)
2063 case ISD::FP_ROUND_INREG:
2064 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
2066 case ISD::SIGN_EXTEND_INREG: {
2067 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2068 if (EVT == VT) return N1; // Not actually extending
2071 case ISD::EXTRACT_VECTOR_ELT:
2072 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2074 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2075 // expanding copies of large vectors from registers.
2076 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2077 N1.getNumOperands() > 0) {
2079 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2080 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2081 N1.getOperand(N2C->getValue() / Factor),
2082 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2085 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2086 // expanding large vector constants.
2087 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2088 return N1.getOperand(N2C->getValue());
2090 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2091 // operations are lowered to scalars.
2092 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2093 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2095 return N1.getOperand(1);
2097 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2100 case ISD::EXTRACT_ELEMENT:
2101 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
2103 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2104 // 64-bit integers into 32-bit parts. Instead of building the extract of
2105 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2106 if (N1.getOpcode() == ISD::BUILD_PAIR)
2107 return N1.getOperand(N2C->getValue());
2109 // EXTRACT_ELEMENT of a constant int is also very common.
2110 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2111 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2112 return getConstant(C->getValue() >> Shift, VT);
2116 // FIXME: figure out how to safely handle things like
2117 // int foo(int x) { return 1 << (x & 255); }
2118 // int bar() { return foo(256); }
2123 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2124 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
2125 return getNode(Opcode, VT, N1, N2.getOperand(0));
2126 else if (N2.getOpcode() == ISD::AND)
2127 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
2128 // If the and is only masking out bits that cannot effect the shift,
2129 // eliminate the and.
2130 unsigned NumBits = MVT::getSizeInBits(VT);
2131 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2132 return getNode(Opcode, VT, N1, N2.getOperand(0));
2138 // Memoize this node if possible.
2140 SDVTList VTs = getVTList(VT);
2141 if (VT != MVT::Flag) {
2142 SDOperand Ops[] = { N1, N2 };
2143 FoldingSetNodeID ID;
2144 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
2146 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2147 return SDOperand(E, 0);
2148 N = new BinarySDNode(Opcode, VTs, N1, N2);
2149 CSEMap.InsertNode(N, IP);
2151 N = new BinarySDNode(Opcode, VTs, N1, N2);
2154 AllNodes.push_back(N);
2155 return SDOperand(N, 0);
2158 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2159 SDOperand N1, SDOperand N2, SDOperand N3) {
2160 // Perform various simplifications.
2161 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2162 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2165 // Use FoldSetCC to simplify SETCC's.
2166 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
2167 if (Simp.Val) return Simp;
2172 if (N1C->getValue())
2173 return N2; // select true, X, Y -> X
2175 return N3; // select false, X, Y -> Y
2177 if (N2 == N3) return N2; // select C, X, X -> X
2181 if (N2C->getValue()) // Unconditional branch
2182 return getNode(ISD::BR, MVT::Other, N1, N3);
2184 return N1; // Never-taken branch
2186 case ISD::VECTOR_SHUFFLE:
2187 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2188 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2189 N3.getOpcode() == ISD::BUILD_VECTOR &&
2190 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2191 "Illegal VECTOR_SHUFFLE node!");
2193 case ISD::BIT_CONVERT:
2194 // Fold bit_convert nodes from a type to themselves.
2195 if (N1.getValueType() == VT)
2200 // Memoize node if it doesn't produce a flag.
2202 SDVTList VTs = getVTList(VT);
2203 if (VT != MVT::Flag) {
2204 SDOperand Ops[] = { N1, N2, N3 };
2205 FoldingSetNodeID ID;
2206 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2208 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2209 return SDOperand(E, 0);
2210 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2211 CSEMap.InsertNode(N, IP);
2213 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2215 AllNodes.push_back(N);
2216 return SDOperand(N, 0);
2219 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2220 SDOperand N1, SDOperand N2, SDOperand N3,
2222 SDOperand Ops[] = { N1, N2, N3, N4 };
2223 return getNode(Opcode, VT, Ops, 4);
2226 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2227 SDOperand N1, SDOperand N2, SDOperand N3,
2228 SDOperand N4, SDOperand N5) {
2229 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2230 return getNode(Opcode, VT, Ops, 5);
2233 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2234 SDOperand Chain, SDOperand Ptr,
2235 const Value *SV, int SVOffset,
2236 bool isVolatile, unsigned Alignment) {
2237 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2239 if (VT != MVT::iPTR) {
2240 Ty = MVT::getTypeForValueType(VT);
2242 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2243 assert(PT && "Value for load must be a pointer");
2244 Ty = PT->getElementType();
2246 assert(Ty && "Could not get type information for load");
2247 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2249 SDVTList VTs = getVTList(VT, MVT::Other);
2250 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2251 SDOperand Ops[] = { Chain, Ptr, Undef };
2252 FoldingSetNodeID ID;
2253 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2254 ID.AddInteger(ISD::UNINDEXED);
2255 ID.AddInteger(ISD::NON_EXTLOAD);
2256 ID.AddInteger((unsigned int)VT);
2258 ID.AddInteger(SVOffset);
2259 ID.AddInteger(Alignment);
2260 ID.AddInteger(isVolatile);
2262 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2263 return SDOperand(E, 0);
2264 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
2265 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2267 CSEMap.InsertNode(N, IP);
2268 AllNodes.push_back(N);
2269 return SDOperand(N, 0);
2272 SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
2273 SDOperand Chain, SDOperand Ptr,
2275 int SVOffset, MVT::ValueType EVT,
2276 bool isVolatile, unsigned Alignment) {
2277 // If they are asking for an extending load from/to the same thing, return a
2280 ExtType = ISD::NON_EXTLOAD;
2282 if (MVT::isVector(VT))
2283 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
2285 assert(EVT < VT && "Should only be an extending load, not truncating!");
2286 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2287 "Cannot sign/zero extend a FP/Vector load!");
2288 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2289 "Cannot convert from FP to Int or Int -> FP!");
2291 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2293 if (VT != MVT::iPTR) {
2294 Ty = MVT::getTypeForValueType(VT);
2296 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2297 assert(PT && "Value for load must be a pointer");
2298 Ty = PT->getElementType();
2300 assert(Ty && "Could not get type information for load");
2301 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2303 SDVTList VTs = getVTList(VT, MVT::Other);
2304 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2305 SDOperand Ops[] = { Chain, Ptr, Undef };
2306 FoldingSetNodeID ID;
2307 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2308 ID.AddInteger(ISD::UNINDEXED);
2309 ID.AddInteger(ExtType);
2310 ID.AddInteger((unsigned int)EVT);
2312 ID.AddInteger(SVOffset);
2313 ID.AddInteger(Alignment);
2314 ID.AddInteger(isVolatile);
2316 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2317 return SDOperand(E, 0);
2318 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
2319 SV, SVOffset, Alignment, isVolatile);
2320 CSEMap.InsertNode(N, IP);
2321 AllNodes.push_back(N);
2322 return SDOperand(N, 0);
2326 SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2327 SDOperand Offset, ISD::MemIndexedMode AM) {
2328 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
2329 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2330 "Load is already a indexed load!");
2331 MVT::ValueType VT = OrigLoad.getValueType();
2332 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
2333 SDOperand Ops[] = { LD->getChain(), Base, Offset };
2334 FoldingSetNodeID ID;
2335 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2337 ID.AddInteger(LD->getExtensionType());
2338 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
2339 ID.AddPointer(LD->getSrcValue());
2340 ID.AddInteger(LD->getSrcValueOffset());
2341 ID.AddInteger(LD->getAlignment());
2342 ID.AddInteger(LD->isVolatile());
2344 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2345 return SDOperand(E, 0);
2346 SDNode *N = new LoadSDNode(Ops, VTs, AM,
2347 LD->getExtensionType(), LD->getLoadedVT(),
2348 LD->getSrcValue(), LD->getSrcValueOffset(),
2349 LD->getAlignment(), LD->isVolatile());
2350 CSEMap.InsertNode(N, IP);
2351 AllNodes.push_back(N);
2352 return SDOperand(N, 0);
2355 SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
2356 SDOperand Ptr, const Value *SV, int SVOffset,
2357 bool isVolatile, unsigned Alignment) {
2358 MVT::ValueType VT = Val.getValueType();
2360 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2362 if (VT != MVT::iPTR) {
2363 Ty = MVT::getTypeForValueType(VT);
2365 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2366 assert(PT && "Value for store must be a pointer");
2367 Ty = PT->getElementType();
2369 assert(Ty && "Could not get type information for store");
2370 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2372 SDVTList VTs = getVTList(MVT::Other);
2373 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2374 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2375 FoldingSetNodeID ID;
2376 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2377 ID.AddInteger(ISD::UNINDEXED);
2378 ID.AddInteger(false);
2379 ID.AddInteger((unsigned int)VT);
2381 ID.AddInteger(SVOffset);
2382 ID.AddInteger(Alignment);
2383 ID.AddInteger(isVolatile);
2385 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2386 return SDOperand(E, 0);
2387 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
2388 VT, SV, SVOffset, Alignment, isVolatile);
2389 CSEMap.InsertNode(N, IP);
2390 AllNodes.push_back(N);
2391 return SDOperand(N, 0);
2394 SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
2395 SDOperand Ptr, const Value *SV,
2396 int SVOffset, MVT::ValueType SVT,
2397 bool isVolatile, unsigned Alignment) {
2398 MVT::ValueType VT = Val.getValueType();
2399 bool isTrunc = VT != SVT;
2401 assert(VT > SVT && "Not a truncation?");
2402 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2403 "Can't do FP-INT conversion!");
2405 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2407 if (VT != MVT::iPTR) {
2408 Ty = MVT::getTypeForValueType(VT);
2410 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2411 assert(PT && "Value for store must be a pointer");
2412 Ty = PT->getElementType();
2414 assert(Ty && "Could not get type information for store");
2415 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2417 SDVTList VTs = getVTList(MVT::Other);
2418 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2419 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2420 FoldingSetNodeID ID;
2421 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2422 ID.AddInteger(ISD::UNINDEXED);
2423 ID.AddInteger(isTrunc);
2424 ID.AddInteger((unsigned int)SVT);
2426 ID.AddInteger(SVOffset);
2427 ID.AddInteger(Alignment);
2428 ID.AddInteger(isVolatile);
2430 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2431 return SDOperand(E, 0);
2432 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, isTrunc,
2433 SVT, SV, SVOffset, Alignment, isVolatile);
2434 CSEMap.InsertNode(N, IP);
2435 AllNodes.push_back(N);
2436 return SDOperand(N, 0);
2440 SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2441 SDOperand Offset, ISD::MemIndexedMode AM) {
2442 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2443 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2444 "Store is already a indexed store!");
2445 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2446 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2447 FoldingSetNodeID ID;
2448 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2450 ID.AddInteger(ST->isTruncatingStore());
2451 ID.AddInteger((unsigned int)(ST->getStoredVT()));
2452 ID.AddPointer(ST->getSrcValue());
2453 ID.AddInteger(ST->getSrcValueOffset());
2454 ID.AddInteger(ST->getAlignment());
2455 ID.AddInteger(ST->isVolatile());
2457 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2458 return SDOperand(E, 0);
2459 SDNode *N = new StoreSDNode(Ops, VTs, AM,
2460 ST->isTruncatingStore(), ST->getStoredVT(),
2461 ST->getSrcValue(), ST->getSrcValueOffset(),
2462 ST->getAlignment(), ST->isVolatile());
2463 CSEMap.InsertNode(N, IP);
2464 AllNodes.push_back(N);
2465 return SDOperand(N, 0);
2468 SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2469 SDOperand Chain, SDOperand Ptr,
2471 SDOperand Ops[] = { Chain, Ptr, SV };
2472 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
2475 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2476 const SDOperand *Ops, unsigned NumOps) {
2478 case 0: return getNode(Opcode, VT);
2479 case 1: return getNode(Opcode, VT, Ops[0]);
2480 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2481 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
2487 case ISD::SELECT_CC: {
2488 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
2489 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2490 "LHS and RHS of condition must have same type!");
2491 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2492 "True and False arms of SelectCC must have same type!");
2493 assert(Ops[2].getValueType() == VT &&
2494 "select_cc node must be of same type as true and false value!");
2498 assert(NumOps == 5 && "BR_CC takes 5 operands!");
2499 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2500 "LHS/RHS of comparison should match types!");
2507 SDVTList VTs = getVTList(VT);
2508 if (VT != MVT::Flag) {
2509 FoldingSetNodeID ID;
2510 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
2512 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2513 return SDOperand(E, 0);
2514 N = new SDNode(Opcode, VTs, Ops, NumOps);
2515 CSEMap.InsertNode(N, IP);
2517 N = new SDNode(Opcode, VTs, Ops, NumOps);
2519 AllNodes.push_back(N);
2520 return SDOperand(N, 0);
2523 SDOperand SelectionDAG::getNode(unsigned Opcode,
2524 std::vector<MVT::ValueType> &ResultTys,
2525 const SDOperand *Ops, unsigned NumOps) {
2526 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2530 SDOperand SelectionDAG::getNode(unsigned Opcode,
2531 const MVT::ValueType *VTs, unsigned NumVTs,
2532 const SDOperand *Ops, unsigned NumOps) {
2534 return getNode(Opcode, VTs[0], Ops, NumOps);
2535 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2538 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2539 const SDOperand *Ops, unsigned NumOps) {
2540 if (VTList.NumVTs == 1)
2541 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
2544 // FIXME: figure out how to safely handle things like
2545 // int foo(int x) { return 1 << (x & 255); }
2546 // int bar() { return foo(256); }
2548 case ISD::SRA_PARTS:
2549 case ISD::SRL_PARTS:
2550 case ISD::SHL_PARTS:
2551 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2552 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
2553 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2554 else if (N3.getOpcode() == ISD::AND)
2555 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2556 // If the and is only masking out bits that cannot effect the shift,
2557 // eliminate the and.
2558 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2559 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2560 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2566 // Memoize the node unless it returns a flag.
2568 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
2569 FoldingSetNodeID ID;
2570 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
2572 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2573 return SDOperand(E, 0);
2575 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2576 else if (NumOps == 2)
2577 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2578 else if (NumOps == 3)
2579 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2581 N = new SDNode(Opcode, VTList, Ops, NumOps);
2582 CSEMap.InsertNode(N, IP);
2585 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2586 else if (NumOps == 2)
2587 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2588 else if (NumOps == 3)
2589 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2591 N = new SDNode(Opcode, VTList, Ops, NumOps);
2593 AllNodes.push_back(N);
2594 return SDOperand(N, 0);
2597 SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
2598 if (!MVT::isExtendedVT(VT))
2599 return makeVTList(SDNode::getValueTypeList(VT), 1);
2601 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2602 E = VTList.end(); I != E; ++I) {
2603 if (I->size() == 1 && (*I)[0] == VT)
2604 return makeVTList(&(*I)[0], 1);
2606 std::vector<MVT::ValueType> V;
2608 VTList.push_front(V);
2609 return makeVTList(&(*VTList.begin())[0], 1);
2612 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
2613 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2614 E = VTList.end(); I != E; ++I) {
2615 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
2616 return makeVTList(&(*I)[0], 2);
2618 std::vector<MVT::ValueType> V;
2621 VTList.push_front(V);
2622 return makeVTList(&(*VTList.begin())[0], 2);
2624 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2625 MVT::ValueType VT3) {
2626 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2627 E = VTList.end(); I != E; ++I) {
2628 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2630 return makeVTList(&(*I)[0], 3);
2632 std::vector<MVT::ValueType> V;
2636 VTList.push_front(V);
2637 return makeVTList(&(*VTList.begin())[0], 3);
2640 SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2642 case 0: assert(0 && "Cannot have nodes without results!");
2643 case 1: return getVTList(VTs[0]);
2644 case 2: return getVTList(VTs[0], VTs[1]);
2645 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2649 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2650 E = VTList.end(); I != E; ++I) {
2651 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2653 bool NoMatch = false;
2654 for (unsigned i = 2; i != NumVTs; ++i)
2655 if (VTs[i] != (*I)[i]) {
2660 return makeVTList(&*I->begin(), NumVTs);
2663 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2664 return makeVTList(&*VTList.begin()->begin(), NumVTs);
2668 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2669 /// specified operands. If the resultant node already exists in the DAG,
2670 /// this does not modify the specified node, instead it returns the node that
2671 /// already exists. If the resultant node does not exist in the DAG, the
2672 /// input node is returned. As a degenerate case, if you specify the same
2673 /// input operands as the node already has, the input node is returned.
2674 SDOperand SelectionDAG::
2675 UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2676 SDNode *N = InN.Val;
2677 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2679 // Check to see if there is no change.
2680 if (Op == N->getOperand(0)) return InN;
2682 // See if the modified node already exists.
2683 void *InsertPos = 0;
2684 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2685 return SDOperand(Existing, InN.ResNo);
2687 // Nope it doesn't. Remove the node from it's current place in the maps.
2689 RemoveNodeFromCSEMaps(N);
2691 // Now we update the operands.
2692 N->OperandList[0].Val->removeUser(N);
2694 N->OperandList[0] = Op;
2696 // If this gets put into a CSE map, add it.
2697 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2701 SDOperand SelectionDAG::
2702 UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2703 SDNode *N = InN.Val;
2704 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2706 // Check to see if there is no change.
2707 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2708 return InN; // No operands changed, just return the input node.
2710 // See if the modified node already exists.
2711 void *InsertPos = 0;
2712 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2713 return SDOperand(Existing, InN.ResNo);
2715 // Nope it doesn't. Remove the node from it's current place in the maps.
2717 RemoveNodeFromCSEMaps(N);
2719 // Now we update the operands.
2720 if (N->OperandList[0] != Op1) {
2721 N->OperandList[0].Val->removeUser(N);
2722 Op1.Val->addUser(N);
2723 N->OperandList[0] = Op1;
2725 if (N->OperandList[1] != Op2) {
2726 N->OperandList[1].Val->removeUser(N);
2727 Op2.Val->addUser(N);
2728 N->OperandList[1] = Op2;
2731 // If this gets put into a CSE map, add it.
2732 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2736 SDOperand SelectionDAG::
2737 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2738 SDOperand Ops[] = { Op1, Op2, Op3 };
2739 return UpdateNodeOperands(N, Ops, 3);
2742 SDOperand SelectionDAG::
2743 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2744 SDOperand Op3, SDOperand Op4) {
2745 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2746 return UpdateNodeOperands(N, Ops, 4);
2749 SDOperand SelectionDAG::
2750 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2751 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2752 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2753 return UpdateNodeOperands(N, Ops, 5);
2757 SDOperand SelectionDAG::
2758 UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
2759 SDNode *N = InN.Val;
2760 assert(N->getNumOperands() == NumOps &&
2761 "Update with wrong number of operands");
2763 // Check to see if there is no change.
2764 bool AnyChange = false;
2765 for (unsigned i = 0; i != NumOps; ++i) {
2766 if (Ops[i] != N->getOperand(i)) {
2772 // No operands changed, just return the input node.
2773 if (!AnyChange) return InN;
2775 // See if the modified node already exists.
2776 void *InsertPos = 0;
2777 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
2778 return SDOperand(Existing, InN.ResNo);
2780 // Nope it doesn't. Remove the node from it's current place in the maps.
2782 RemoveNodeFromCSEMaps(N);
2784 // Now we update the operands.
2785 for (unsigned i = 0; i != NumOps; ++i) {
2786 if (N->OperandList[i] != Ops[i]) {
2787 N->OperandList[i].Val->removeUser(N);
2788 Ops[i].Val->addUser(N);
2789 N->OperandList[i] = Ops[i];
2793 // If this gets put into a CSE map, add it.
2794 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2799 /// MorphNodeTo - This frees the operands of the current node, resets the
2800 /// opcode, types, and operands to the specified value. This should only be
2801 /// used by the SelectionDAG class.
2802 void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2803 const SDOperand *Ops, unsigned NumOps) {
2806 NumValues = L.NumVTs;
2808 // Clear the operands list, updating used nodes to remove this from their
2810 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2811 I->Val->removeUser(this);
2813 // If NumOps is larger than the # of operands we currently have, reallocate
2814 // the operand list.
2815 if (NumOps > NumOperands) {
2816 if (OperandsNeedDelete)
2817 delete [] OperandList;
2818 OperandList = new SDOperand[NumOps];
2819 OperandsNeedDelete = true;
2822 // Assign the new operands.
2823 NumOperands = NumOps;
2825 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2826 OperandList[i] = Ops[i];
2827 SDNode *N = OperandList[i].Val;
2828 N->Uses.push_back(this);
2832 /// SelectNodeTo - These are used for target selectors to *mutate* the
2833 /// specified node to have the specified return type, Target opcode, and
2834 /// operands. Note that target opcodes are stored as
2835 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
2837 /// Note that SelectNodeTo returns the resultant node. If there is already a
2838 /// node of the specified opcode and operands, it returns that node instead of
2839 /// the current one.
2840 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2841 MVT::ValueType VT) {
2842 SDVTList VTs = getVTList(VT);
2843 FoldingSetNodeID ID;
2844 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
2846 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2849 RemoveNodeFromCSEMaps(N);
2851 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
2853 CSEMap.InsertNode(N, IP);
2857 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2858 MVT::ValueType VT, SDOperand Op1) {
2859 // If an identical node already exists, use it.
2860 SDVTList VTs = getVTList(VT);
2861 SDOperand Ops[] = { Op1 };
2863 FoldingSetNodeID ID;
2864 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
2866 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2869 RemoveNodeFromCSEMaps(N);
2870 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
2871 CSEMap.InsertNode(N, IP);
2875 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2876 MVT::ValueType VT, SDOperand Op1,
2878 // If an identical node already exists, use it.
2879 SDVTList VTs = getVTList(VT);
2880 SDOperand Ops[] = { Op1, Op2 };
2882 FoldingSetNodeID ID;
2883 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2885 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2888 RemoveNodeFromCSEMaps(N);
2890 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2892 CSEMap.InsertNode(N, IP); // Memoize the new node.
2896 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2897 MVT::ValueType VT, SDOperand Op1,
2898 SDOperand Op2, SDOperand Op3) {
2899 // If an identical node already exists, use it.
2900 SDVTList VTs = getVTList(VT);
2901 SDOperand Ops[] = { Op1, Op2, Op3 };
2902 FoldingSetNodeID ID;
2903 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
2905 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2908 RemoveNodeFromCSEMaps(N);
2910 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
2912 CSEMap.InsertNode(N, IP); // Memoize the new node.
2916 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2917 MVT::ValueType VT, const SDOperand *Ops,
2919 // If an identical node already exists, use it.
2920 SDVTList VTs = getVTList(VT);
2921 FoldingSetNodeID ID;
2922 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
2924 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2927 RemoveNodeFromCSEMaps(N);
2928 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
2930 CSEMap.InsertNode(N, IP); // Memoize the new node.
2934 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2935 MVT::ValueType VT1, MVT::ValueType VT2,
2936 SDOperand Op1, SDOperand Op2) {
2937 SDVTList VTs = getVTList(VT1, VT2);
2938 FoldingSetNodeID ID;
2939 SDOperand Ops[] = { Op1, Op2 };
2940 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2942 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2945 RemoveNodeFromCSEMaps(N);
2946 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2947 CSEMap.InsertNode(N, IP); // Memoize the new node.
2951 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2952 MVT::ValueType VT1, MVT::ValueType VT2,
2953 SDOperand Op1, SDOperand Op2,
2955 // If an identical node already exists, use it.
2956 SDVTList VTs = getVTList(VT1, VT2);
2957 SDOperand Ops[] = { Op1, Op2, Op3 };
2958 FoldingSetNodeID ID;
2959 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
2961 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2964 RemoveNodeFromCSEMaps(N);
2966 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
2967 CSEMap.InsertNode(N, IP); // Memoize the new node.
2972 /// getTargetNode - These are used for target selectors to create a new node
2973 /// with specified return type(s), target opcode, and operands.
2975 /// Note that getTargetNode returns the resultant node. If there is already a
2976 /// node of the specified opcode and operands, it returns that node instead of
2977 /// the current one.
2978 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2979 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2981 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2983 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2985 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2986 SDOperand Op1, SDOperand Op2) {
2987 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2989 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2990 SDOperand Op1, SDOperand Op2,
2992 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2994 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2995 const SDOperand *Ops, unsigned NumOps) {
2996 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
2998 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2999 MVT::ValueType VT2, SDOperand Op1) {
3000 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3001 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
3003 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3004 MVT::ValueType VT2, SDOperand Op1,
3006 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3007 SDOperand Ops[] = { Op1, Op2 };
3008 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
3010 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3011 MVT::ValueType VT2, SDOperand Op1,
3012 SDOperand Op2, SDOperand Op3) {
3013 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3014 SDOperand Ops[] = { Op1, Op2, Op3 };
3015 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
3017 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3019 const SDOperand *Ops, unsigned NumOps) {
3020 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3021 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
3023 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3024 MVT::ValueType VT2, MVT::ValueType VT3,
3025 SDOperand Op1, SDOperand Op2) {
3026 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3027 SDOperand Ops[] = { Op1, Op2 };
3028 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
3030 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3031 MVT::ValueType VT2, MVT::ValueType VT3,
3032 SDOperand Op1, SDOperand Op2,
3034 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3035 SDOperand Ops[] = { Op1, Op2, Op3 };
3036 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3038 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3039 MVT::ValueType VT2, MVT::ValueType VT3,
3040 const SDOperand *Ops, unsigned NumOps) {
3041 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3042 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
3044 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3045 MVT::ValueType VT2, MVT::ValueType VT3,
3047 const SDOperand *Ops, unsigned NumOps) {
3048 std::vector<MVT::ValueType> VTList;
3049 VTList.push_back(VT1);
3050 VTList.push_back(VT2);
3051 VTList.push_back(VT3);
3052 VTList.push_back(VT4);
3053 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3054 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3057 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3058 /// This can cause recursive merging of nodes in the DAG.
3060 /// This version assumes From/To have a single result value.
3062 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
3063 std::vector<SDNode*> *Deleted) {
3064 SDNode *From = FromN.Val, *To = ToN.Val;
3065 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
3066 "Cannot replace with this method!");
3067 assert(From != To && "Cannot replace uses of with self");
3069 while (!From->use_empty()) {
3070 // Process users until they are all gone.
3071 SDNode *U = *From->use_begin();
3073 // This node is about to morph, remove its old self from the CSE maps.
3074 RemoveNodeFromCSEMaps(U);
3076 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3078 if (I->Val == From) {
3079 From->removeUser(U);
3084 // Now that we have modified U, add it back to the CSE maps. If it already
3085 // exists there, recursively merge the results together.
3086 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3087 ReplaceAllUsesWith(U, Existing, Deleted);
3089 if (Deleted) Deleted->push_back(U);
3090 DeleteNodeNotInCSEMaps(U);
3095 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3096 /// This can cause recursive merging of nodes in the DAG.
3098 /// This version assumes From/To have matching types and numbers of result
3101 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3102 std::vector<SDNode*> *Deleted) {
3103 assert(From != To && "Cannot replace uses of with self");
3104 assert(From->getNumValues() == To->getNumValues() &&
3105 "Cannot use this version of ReplaceAllUsesWith!");
3106 if (From->getNumValues() == 1) { // If possible, use the faster version.
3107 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
3111 while (!From->use_empty()) {
3112 // Process users until they are all gone.
3113 SDNode *U = *From->use_begin();
3115 // This node is about to morph, remove its old self from the CSE maps.
3116 RemoveNodeFromCSEMaps(U);
3118 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3120 if (I->Val == From) {
3121 From->removeUser(U);
3126 // Now that we have modified U, add it back to the CSE maps. If it already
3127 // exists there, recursively merge the results together.
3128 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3129 ReplaceAllUsesWith(U, Existing, Deleted);
3131 if (Deleted) Deleted->push_back(U);
3132 DeleteNodeNotInCSEMaps(U);
3137 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3138 /// This can cause recursive merging of nodes in the DAG.
3140 /// This version can replace From with any result values. To must match the
3141 /// number and types of values returned by From.
3142 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
3143 const SDOperand *To,
3144 std::vector<SDNode*> *Deleted) {
3145 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
3146 // Degenerate case handled above.
3147 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
3151 while (!From->use_empty()) {
3152 // Process users until they are all gone.
3153 SDNode *U = *From->use_begin();
3155 // This node is about to morph, remove its old self from the CSE maps.
3156 RemoveNodeFromCSEMaps(U);
3158 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3160 if (I->Val == From) {
3161 const SDOperand &ToOp = To[I->ResNo];
3162 From->removeUser(U);
3164 ToOp.Val->addUser(U);
3167 // Now that we have modified U, add it back to the CSE maps. If it already
3168 // exists there, recursively merge the results together.
3169 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3170 ReplaceAllUsesWith(U, Existing, Deleted);
3172 if (Deleted) Deleted->push_back(U);
3173 DeleteNodeNotInCSEMaps(U);
3178 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3179 /// uses of other values produced by From.Val alone. The Deleted vector is
3180 /// handled the same was as for ReplaceAllUsesWith.
3181 void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
3182 std::vector<SDNode*> &Deleted) {
3183 assert(From != To && "Cannot replace a value with itself");
3184 // Handle the simple, trivial, case efficiently.
3185 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
3186 ReplaceAllUsesWith(From, To, &Deleted);
3190 // Get all of the users of From.Val. We want these in a nice,
3191 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3192 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
3194 while (!Users.empty()) {
3195 // We know that this user uses some value of From. If it is the right
3196 // value, update it.
3197 SDNode *User = Users.back();
3200 for (SDOperand *Op = User->OperandList,
3201 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
3203 // Okay, we know this user needs to be updated. Remove its old self
3204 // from the CSE maps.
3205 RemoveNodeFromCSEMaps(User);
3207 // Update all operands that match "From".
3208 for (; Op != E; ++Op) {
3210 From.Val->removeUser(User);
3212 To.Val->addUser(User);
3216 // Now that we have modified User, add it back to the CSE maps. If it
3217 // already exists there, recursively merge the results together.
3218 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
3219 unsigned NumDeleted = Deleted.size();
3220 ReplaceAllUsesWith(User, Existing, &Deleted);
3222 // User is now dead.
3223 Deleted.push_back(User);
3224 DeleteNodeNotInCSEMaps(User);
3226 // We have to be careful here, because ReplaceAllUsesWith could have
3227 // deleted a user of From, which means there may be dangling pointers
3228 // in the "Users" setvector. Scan over the deleted node pointers and
3229 // remove them from the setvector.
3230 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
3231 Users.remove(Deleted[i]);
3233 break; // Exit the operand scanning loop.
3240 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3241 /// their allnodes order. It returns the maximum id.
3242 unsigned SelectionDAG::AssignNodeIds() {
3244 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3251 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
3252 /// based on their topological order. It returns the maximum id and a vector
3253 /// of the SDNodes* in assigned order by reference.
3254 unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
3255 unsigned DAGSize = AllNodes.size();
3256 std::vector<unsigned> InDegree(DAGSize);
3257 std::vector<SDNode*> Sources;
3259 // Use a two pass approach to avoid using a std::map which is slow.
3261 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3264 unsigned Degree = N->use_size();
3265 InDegree[N->getNodeId()] = Degree;
3267 Sources.push_back(N);
3271 while (!Sources.empty()) {
3272 SDNode *N = Sources.back();
3274 TopOrder.push_back(N);
3275 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3277 unsigned Degree = --InDegree[P->getNodeId()];
3279 Sources.push_back(P);
3283 // Second pass, assign the actual topological order as node ids.
3285 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3287 (*TI)->setNodeId(Id++);
3294 //===----------------------------------------------------------------------===//
3296 //===----------------------------------------------------------------------===//
3298 // Out-of-line virtual method to give class a home.
3299 void SDNode::ANCHOR() {}
3300 void UnarySDNode::ANCHOR() {}
3301 void BinarySDNode::ANCHOR() {}
3302 void TernarySDNode::ANCHOR() {}
3303 void HandleSDNode::ANCHOR() {}
3304 void StringSDNode::ANCHOR() {}
3305 void ConstantSDNode::ANCHOR() {}
3306 void ConstantFPSDNode::ANCHOR() {}
3307 void GlobalAddressSDNode::ANCHOR() {}
3308 void FrameIndexSDNode::ANCHOR() {}
3309 void JumpTableSDNode::ANCHOR() {}
3310 void ConstantPoolSDNode::ANCHOR() {}
3311 void BasicBlockSDNode::ANCHOR() {}
3312 void SrcValueSDNode::ANCHOR() {}
3313 void RegisterSDNode::ANCHOR() {}
3314 void ExternalSymbolSDNode::ANCHOR() {}
3315 void CondCodeSDNode::ANCHOR() {}
3316 void VTSDNode::ANCHOR() {}
3317 void LoadSDNode::ANCHOR() {}
3318 void StoreSDNode::ANCHOR() {}
3320 HandleSDNode::~HandleSDNode() {
3321 SDVTList VTs = { 0, 0 };
3322 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
3325 GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3326 MVT::ValueType VT, int o)
3327 : SDNode(isa<GlobalVariable>(GA) &&
3328 cast<GlobalVariable>(GA)->isThreadLocal() ?
3330 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3332 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3333 getSDVTList(VT)), Offset(o) {
3334 TheGlobal = const_cast<GlobalValue*>(GA);
3337 /// Profile - Gather unique data for the node.
3339 void SDNode::Profile(FoldingSetNodeID &ID) {
3340 AddNodeIDNode(ID, this);
3343 /// getValueTypeList - Return a pointer to the specified value type.
3345 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
3346 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3351 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3352 /// indicated value. This method ignores uses of other values defined by this
3354 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
3355 assert(Value < getNumValues() && "Bad value!");
3357 // If there is only one value, this is easy.
3358 if (getNumValues() == 1)
3359 return use_size() == NUses;
3360 if (use_size() < NUses) return false;
3362 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3364 SmallPtrSet<SDNode*, 32> UsersHandled;
3366 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3368 if (User->getNumOperands() == 1 ||
3369 UsersHandled.insert(User)) // First time we've seen this?
3370 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3371 if (User->getOperand(i) == TheValue) {
3373 return false; // too many uses
3378 // Found exactly the right number of uses?
3383 /// hasAnyUseOfValue - Return true if there are any use of the indicated
3384 /// value. This method ignores uses of other values defined by this operation.
3385 bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3386 assert(Value < getNumValues() && "Bad value!");
3388 if (use_size() == 0) return false;
3390 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3392 SmallPtrSet<SDNode*, 32> UsersHandled;
3394 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3396 if (User->getNumOperands() == 1 ||
3397 UsersHandled.insert(User)) // First time we've seen this?
3398 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3399 if (User->getOperand(i) == TheValue) {
3408 /// isOnlyUse - Return true if this node is the only use of N.
3410 bool SDNode::isOnlyUse(SDNode *N) const {
3412 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3423 /// isOperand - Return true if this node is an operand of N.
3425 bool SDOperand::isOperand(SDNode *N) const {
3426 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3427 if (*this == N->getOperand(i))
3432 bool SDNode::isOperand(SDNode *N) const {
3433 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3434 if (this == N->OperandList[i].Val)
3439 static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
3440 SmallPtrSet<SDNode *, 32> &Visited) {
3441 if (found || !Visited.insert(N))
3444 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3445 SDNode *Op = N->getOperand(i).Val;
3450 findPredecessor(Op, P, found, Visited);
3454 /// isPredecessor - Return true if this node is a predecessor of N. This node
3455 /// is either an operand of N or it can be reached by recursively traversing
3456 /// up the operands.
3457 /// NOTE: this is an expensive method. Use it carefully.
3458 bool SDNode::isPredecessor(SDNode *N) const {
3459 SmallPtrSet<SDNode *, 32> Visited;
3461 findPredecessor(N, this, found, Visited);
3465 uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3466 assert(Num < NumOperands && "Invalid child # of SDNode!");
3467 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3470 std::string SDNode::getOperationName(const SelectionDAG *G) const {
3471 switch (getOpcode()) {
3473 if (getOpcode() < ISD::BUILTIN_OP_END)
3474 return "<<Unknown DAG Node>>";
3477 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
3478 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
3479 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
3481 TargetLowering &TLI = G->getTargetLoweringInfo();
3483 TLI.getTargetNodeName(getOpcode());
3484 if (Name) return Name;
3487 return "<<Unknown Target Node>>";
3490 case ISD::PCMARKER: return "PCMarker";
3491 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
3492 case ISD::SRCVALUE: return "SrcValue";
3493 case ISD::EntryToken: return "EntryToken";
3494 case ISD::TokenFactor: return "TokenFactor";
3495 case ISD::AssertSext: return "AssertSext";
3496 case ISD::AssertZext: return "AssertZext";
3498 case ISD::STRING: return "String";
3499 case ISD::BasicBlock: return "BasicBlock";
3500 case ISD::VALUETYPE: return "ValueType";
3501 case ISD::Register: return "Register";
3503 case ISD::Constant: return "Constant";
3504 case ISD::ConstantFP: return "ConstantFP";
3505 case ISD::GlobalAddress: return "GlobalAddress";
3506 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
3507 case ISD::FrameIndex: return "FrameIndex";
3508 case ISD::JumpTable: return "JumpTable";
3509 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
3510 case ISD::RETURNADDR: return "RETURNADDR";
3511 case ISD::FRAMEADDR: return "FRAMEADDR";
3512 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
3513 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3514 case ISD::EHSELECTION: return "EHSELECTION";
3515 case ISD::EH_RETURN: return "EH_RETURN";
3516 case ISD::ConstantPool: return "ConstantPool";
3517 case ISD::ExternalSymbol: return "ExternalSymbol";
3518 case ISD::INTRINSIC_WO_CHAIN: {
3519 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3520 return Intrinsic::getName((Intrinsic::ID)IID);
3522 case ISD::INTRINSIC_VOID:
3523 case ISD::INTRINSIC_W_CHAIN: {
3524 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
3525 return Intrinsic::getName((Intrinsic::ID)IID);
3528 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
3529 case ISD::TargetConstant: return "TargetConstant";
3530 case ISD::TargetConstantFP:return "TargetConstantFP";
3531 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
3532 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
3533 case ISD::TargetFrameIndex: return "TargetFrameIndex";
3534 case ISD::TargetJumpTable: return "TargetJumpTable";
3535 case ISD::TargetConstantPool: return "TargetConstantPool";
3536 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
3538 case ISD::CopyToReg: return "CopyToReg";
3539 case ISD::CopyFromReg: return "CopyFromReg";
3540 case ISD::UNDEF: return "undef";
3541 case ISD::MERGE_VALUES: return "merge_values";
3542 case ISD::INLINEASM: return "inlineasm";
3543 case ISD::LABEL: return "label";
3544 case ISD::HANDLENODE: return "handlenode";
3545 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
3546 case ISD::CALL: return "call";
3549 case ISD::FABS: return "fabs";
3550 case ISD::FNEG: return "fneg";
3551 case ISD::FSQRT: return "fsqrt";
3552 case ISD::FSIN: return "fsin";
3553 case ISD::FCOS: return "fcos";
3554 case ISD::FPOWI: return "fpowi";
3557 case ISD::ADD: return "add";
3558 case ISD::SUB: return "sub";
3559 case ISD::MUL: return "mul";
3560 case ISD::MULHU: return "mulhu";
3561 case ISD::MULHS: return "mulhs";
3562 case ISD::SDIV: return "sdiv";
3563 case ISD::UDIV: return "udiv";
3564 case ISD::SREM: return "srem";
3565 case ISD::UREM: return "urem";
3566 case ISD::AND: return "and";
3567 case ISD::OR: return "or";
3568 case ISD::XOR: return "xor";
3569 case ISD::SHL: return "shl";
3570 case ISD::SRA: return "sra";
3571 case ISD::SRL: return "srl";
3572 case ISD::ROTL: return "rotl";
3573 case ISD::ROTR: return "rotr";
3574 case ISD::FADD: return "fadd";
3575 case ISD::FSUB: return "fsub";
3576 case ISD::FMUL: return "fmul";
3577 case ISD::FDIV: return "fdiv";
3578 case ISD::FREM: return "frem";
3579 case ISD::FCOPYSIGN: return "fcopysign";
3581 case ISD::SETCC: return "setcc";
3582 case ISD::SELECT: return "select";
3583 case ISD::SELECT_CC: return "select_cc";
3584 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
3585 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
3586 case ISD::CONCAT_VECTORS: return "concat_vectors";
3587 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
3588 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
3589 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
3590 case ISD::CARRY_FALSE: return "carry_false";
3591 case ISD::ADDC: return "addc";
3592 case ISD::ADDE: return "adde";
3593 case ISD::SUBC: return "subc";
3594 case ISD::SUBE: return "sube";
3595 case ISD::SHL_PARTS: return "shl_parts";
3596 case ISD::SRA_PARTS: return "sra_parts";
3597 case ISD::SRL_PARTS: return "srl_parts";
3599 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3600 case ISD::INSERT_SUBREG: return "insert_subreg";
3602 // Conversion operators.
3603 case ISD::SIGN_EXTEND: return "sign_extend";
3604 case ISD::ZERO_EXTEND: return "zero_extend";
3605 case ISD::ANY_EXTEND: return "any_extend";
3606 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
3607 case ISD::TRUNCATE: return "truncate";
3608 case ISD::FP_ROUND: return "fp_round";
3609 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
3610 case ISD::FP_EXTEND: return "fp_extend";
3612 case ISD::SINT_TO_FP: return "sint_to_fp";
3613 case ISD::UINT_TO_FP: return "uint_to_fp";
3614 case ISD::FP_TO_SINT: return "fp_to_sint";
3615 case ISD::FP_TO_UINT: return "fp_to_uint";
3616 case ISD::BIT_CONVERT: return "bit_convert";
3618 // Control flow instructions
3619 case ISD::BR: return "br";
3620 case ISD::BRIND: return "brind";
3621 case ISD::BR_JT: return "br_jt";
3622 case ISD::BRCOND: return "brcond";
3623 case ISD::BR_CC: return "br_cc";
3624 case ISD::RET: return "ret";
3625 case ISD::CALLSEQ_START: return "callseq_start";
3626 case ISD::CALLSEQ_END: return "callseq_end";
3629 case ISD::LOAD: return "load";
3630 case ISD::STORE: return "store";
3631 case ISD::VAARG: return "vaarg";
3632 case ISD::VACOPY: return "vacopy";
3633 case ISD::VAEND: return "vaend";
3634 case ISD::VASTART: return "vastart";
3635 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
3636 case ISD::EXTRACT_ELEMENT: return "extract_element";
3637 case ISD::BUILD_PAIR: return "build_pair";
3638 case ISD::STACKSAVE: return "stacksave";
3639 case ISD::STACKRESTORE: return "stackrestore";
3641 // Block memory operations.
3642 case ISD::MEMSET: return "memset";
3643 case ISD::MEMCPY: return "memcpy";
3644 case ISD::MEMMOVE: return "memmove";
3647 case ISD::BSWAP: return "bswap";
3648 case ISD::CTPOP: return "ctpop";
3649 case ISD::CTTZ: return "cttz";
3650 case ISD::CTLZ: return "ctlz";
3653 case ISD::LOCATION: return "location";
3654 case ISD::DEBUG_LOC: return "debug_loc";
3657 case ISD::TRAMPOLINE: return "trampoline";
3660 switch (cast<CondCodeSDNode>(this)->get()) {
3661 default: assert(0 && "Unknown setcc condition!");
3662 case ISD::SETOEQ: return "setoeq";
3663 case ISD::SETOGT: return "setogt";
3664 case ISD::SETOGE: return "setoge";
3665 case ISD::SETOLT: return "setolt";
3666 case ISD::SETOLE: return "setole";
3667 case ISD::SETONE: return "setone";
3669 case ISD::SETO: return "seto";
3670 case ISD::SETUO: return "setuo";
3671 case ISD::SETUEQ: return "setue";
3672 case ISD::SETUGT: return "setugt";
3673 case ISD::SETUGE: return "setuge";
3674 case ISD::SETULT: return "setult";
3675 case ISD::SETULE: return "setule";
3676 case ISD::SETUNE: return "setune";
3678 case ISD::SETEQ: return "seteq";
3679 case ISD::SETGT: return "setgt";
3680 case ISD::SETGE: return "setge";
3681 case ISD::SETLT: return "setlt";
3682 case ISD::SETLE: return "setle";
3683 case ISD::SETNE: return "setne";
3688 const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
3697 return "<post-inc>";
3699 return "<post-dec>";
3703 void SDNode::dump() const { dump(0); }
3704 void SDNode::dump(const SelectionDAG *G) const {
3705 cerr << (void*)this << ": ";
3707 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
3709 if (getValueType(i) == MVT::Other)
3712 cerr << MVT::getValueTypeString(getValueType(i));
3714 cerr << " = " << getOperationName(G);
3717 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
3718 if (i) cerr << ", ";
3719 cerr << (void*)getOperand(i).Val;
3720 if (unsigned RN = getOperand(i).ResNo)
3724 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
3725 cerr << "<" << CSDN->getValue() << ">";
3726 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
3727 cerr << "<" << (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle ?
3728 CSDN->getValueAPF().convertToFloat() :
3729 CSDN->getValueAPF().convertToDouble()) << ">";
3730 } else if (const GlobalAddressSDNode *GADN =
3731 dyn_cast<GlobalAddressSDNode>(this)) {
3732 int offset = GADN->getOffset();
3734 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
3736 cerr << " + " << offset;
3738 cerr << " " << offset;
3739 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
3740 cerr << "<" << FIDN->getIndex() << ">";
3741 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
3742 cerr << "<" << JTDN->getIndex() << ">";
3743 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
3744 int offset = CP->getOffset();
3745 if (CP->isMachineConstantPoolEntry())
3746 cerr << "<" << *CP->getMachineCPVal() << ">";
3748 cerr << "<" << *CP->getConstVal() << ">";
3750 cerr << " + " << offset;
3752 cerr << " " << offset;
3753 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
3755 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3757 cerr << LBB->getName() << " ";
3758 cerr << (const void*)BBDN->getBasicBlock() << ">";
3759 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
3760 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
3761 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
3763 cerr << " #" << R->getReg();
3765 } else if (const ExternalSymbolSDNode *ES =
3766 dyn_cast<ExternalSymbolSDNode>(this)) {
3767 cerr << "'" << ES->getSymbol() << "'";
3768 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3770 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
3772 cerr << "<null:" << M->getOffset() << ">";
3773 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
3774 cerr << ":" << MVT::getValueTypeString(N->getVT());
3775 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
3777 switch (LD->getExtensionType()) {
3778 default: doExt = false; break;
3780 cerr << " <anyext ";
3790 cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
3792 const char *AM = getIndexedModeName(LD->getAddressingMode());
3795 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
3796 if (ST->isTruncatingStore())
3798 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
3800 const char *AM = getIndexedModeName(ST->getAddressingMode());
3806 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
3807 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3808 if (N->getOperand(i).Val->hasOneUse())
3809 DumpNodes(N->getOperand(i).Val, indent+2, G);
3811 cerr << "\n" << std::string(indent+2, ' ')
3812 << (void*)N->getOperand(i).Val << ": <multiple use>";
3815 cerr << "\n" << std::string(indent, ' ');
3819 void SelectionDAG::dump() const {
3820 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
3821 std::vector<const SDNode*> Nodes;
3822 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3826 std::sort(Nodes.begin(), Nodes.end());
3828 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
3829 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
3830 DumpNodes(Nodes[i], 2, this);
3833 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
3838 const Type *ConstantPoolSDNode::getType() const {
3839 if (isMachineConstantPoolEntry())
3840 return Val.MachineCPVal->getType();
3841 return Val.ConstVal->getType();