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 ID.AddAPFloat(cast<ConstantFPSDNode>(N)->getValueAPF());
328 case ISD::TargetGlobalAddress:
329 case ISD::GlobalAddress:
330 case ISD::TargetGlobalTLSAddress:
331 case ISD::GlobalTLSAddress: {
332 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
333 ID.AddPointer(GA->getGlobal());
334 ID.AddInteger(GA->getOffset());
337 case ISD::BasicBlock:
338 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
341 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
343 case ISD::SRCVALUE: {
344 SrcValueSDNode *SV = cast<SrcValueSDNode>(N);
345 ID.AddPointer(SV->getValue());
346 ID.AddInteger(SV->getOffset());
349 case ISD::FrameIndex:
350 case ISD::TargetFrameIndex:
351 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
354 case ISD::TargetJumpTable:
355 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
357 case ISD::ConstantPool:
358 case ISD::TargetConstantPool: {
359 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
360 ID.AddInteger(CP->getAlignment());
361 ID.AddInteger(CP->getOffset());
362 if (CP->isMachineConstantPoolEntry())
363 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
365 ID.AddPointer(CP->getConstVal());
369 LoadSDNode *LD = cast<LoadSDNode>(N);
370 ID.AddInteger(LD->getAddressingMode());
371 ID.AddInteger(LD->getExtensionType());
372 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
373 ID.AddPointer(LD->getSrcValue());
374 ID.AddInteger(LD->getSrcValueOffset());
375 ID.AddInteger(LD->getAlignment());
376 ID.AddInteger(LD->isVolatile());
380 StoreSDNode *ST = cast<StoreSDNode>(N);
381 ID.AddInteger(ST->getAddressingMode());
382 ID.AddInteger(ST->isTruncatingStore());
383 ID.AddInteger((unsigned int)(ST->getStoredVT()));
384 ID.AddPointer(ST->getSrcValue());
385 ID.AddInteger(ST->getSrcValueOffset());
386 ID.AddInteger(ST->getAlignment());
387 ID.AddInteger(ST->isVolatile());
393 //===----------------------------------------------------------------------===//
394 // SelectionDAG Class
395 //===----------------------------------------------------------------------===//
397 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
399 void SelectionDAG::RemoveDeadNodes() {
400 // Create a dummy node (which is not added to allnodes), that adds a reference
401 // to the root node, preventing it from being deleted.
402 HandleSDNode Dummy(getRoot());
404 SmallVector<SDNode*, 128> DeadNodes;
406 // Add all obviously-dead nodes to the DeadNodes worklist.
407 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
409 DeadNodes.push_back(I);
411 // Process the worklist, deleting the nodes and adding their uses to the
413 while (!DeadNodes.empty()) {
414 SDNode *N = DeadNodes.back();
415 DeadNodes.pop_back();
417 // Take the node out of the appropriate CSE map.
418 RemoveNodeFromCSEMaps(N);
420 // Next, brutally remove the operand list. This is safe to do, as there are
421 // no cycles in the graph.
422 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
423 SDNode *Operand = I->Val;
424 Operand->removeUser(N);
426 // Now that we removed this operand, see if there are no uses of it left.
427 if (Operand->use_empty())
428 DeadNodes.push_back(Operand);
430 if (N->OperandsNeedDelete)
431 delete[] N->OperandList;
435 // Finally, remove N itself.
439 // If the root changed (e.g. it was a dead load, update the root).
440 setRoot(Dummy.getValue());
443 void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
444 SmallVector<SDNode*, 16> DeadNodes;
445 DeadNodes.push_back(N);
447 // Process the worklist, deleting the nodes and adding their uses to the
449 while (!DeadNodes.empty()) {
450 SDNode *N = DeadNodes.back();
451 DeadNodes.pop_back();
453 // Take the node out of the appropriate CSE map.
454 RemoveNodeFromCSEMaps(N);
456 // Next, brutally remove the operand list. This is safe to do, as there are
457 // no cycles in the graph.
458 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
459 SDNode *Operand = I->Val;
460 Operand->removeUser(N);
462 // Now that we removed this operand, see if there are no uses of it left.
463 if (Operand->use_empty())
464 DeadNodes.push_back(Operand);
466 if (N->OperandsNeedDelete)
467 delete[] N->OperandList;
471 // Finally, remove N itself.
472 Deleted.push_back(N);
477 void SelectionDAG::DeleteNode(SDNode *N) {
478 assert(N->use_empty() && "Cannot delete a node that is not dead!");
480 // First take this out of the appropriate CSE map.
481 RemoveNodeFromCSEMaps(N);
483 // Finally, remove uses due to operands of this node, remove from the
484 // AllNodes list, and delete the node.
485 DeleteNodeNotInCSEMaps(N);
488 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
490 // Remove it from the AllNodes list.
493 // Drop all of the operands and decrement used nodes use counts.
494 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
495 I->Val->removeUser(N);
496 if (N->OperandsNeedDelete)
497 delete[] N->OperandList;
504 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
505 /// correspond to it. This is useful when we're about to delete or repurpose
506 /// the node. We don't want future request for structurally identical nodes
507 /// to return N anymore.
508 void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
510 switch (N->getOpcode()) {
511 case ISD::HANDLENODE: return; // noop.
513 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
516 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
517 "Cond code doesn't exist!");
518 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
519 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
521 case ISD::ExternalSymbol:
522 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
524 case ISD::TargetExternalSymbol:
526 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
529 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
530 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
533 // Remove it from the CSE Map.
534 Erased = CSEMap.RemoveNode(N);
538 // Verify that the node was actually in one of the CSE maps, unless it has a
539 // flag result (which cannot be CSE'd) or is one of the special cases that are
540 // not subject to CSE.
541 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
542 !N->isTargetOpcode()) {
545 assert(0 && "Node is not in map!");
550 /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
551 /// has been taken out and modified in some way. If the specified node already
552 /// exists in the CSE maps, do not modify the maps, but return the existing node
553 /// instead. If it doesn't exist, add it and return null.
555 SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
556 assert(N->getNumOperands() && "This is a leaf node!");
557 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
558 return 0; // Never add these nodes.
560 // Check that remaining values produced are not flags.
561 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
562 if (N->getValueType(i) == MVT::Flag)
563 return 0; // Never CSE anything that produces a flag.
565 SDNode *New = CSEMap.GetOrInsertNode(N);
566 if (New != N) return New; // Node already existed.
570 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
571 /// were replaced with those specified. If this node is never memoized,
572 /// return null, otherwise return a pointer to the slot it would take. If a
573 /// node already exists with these operands, the slot will be non-null.
574 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
576 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
577 return 0; // Never add these nodes.
579 // Check that remaining values produced are not flags.
580 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
581 if (N->getValueType(i) == MVT::Flag)
582 return 0; // Never CSE anything that produces a flag.
584 SDOperand Ops[] = { Op };
586 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
587 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
590 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
591 /// were replaced with those specified. If this node is never memoized,
592 /// return null, otherwise return a pointer to the slot it would take. If a
593 /// node already exists with these operands, the slot will be non-null.
594 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
595 SDOperand Op1, SDOperand Op2,
597 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
598 return 0; // Never add these nodes.
600 // Check that remaining values produced are not flags.
601 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
602 if (N->getValueType(i) == MVT::Flag)
603 return 0; // Never CSE anything that produces a flag.
605 SDOperand Ops[] = { Op1, Op2 };
607 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
608 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
612 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
613 /// were replaced with those specified. If this node is never memoized,
614 /// return null, otherwise return a pointer to the slot it would take. If a
615 /// node already exists with these operands, the slot will be non-null.
616 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
617 const SDOperand *Ops,unsigned NumOps,
619 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
620 return 0; // Never add these nodes.
622 // Check that remaining values produced are not flags.
623 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
624 if (N->getValueType(i) == MVT::Flag)
625 return 0; // Never CSE anything that produces a flag.
628 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
630 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
631 ID.AddInteger(LD->getAddressingMode());
632 ID.AddInteger(LD->getExtensionType());
633 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
634 ID.AddPointer(LD->getSrcValue());
635 ID.AddInteger(LD->getSrcValueOffset());
636 ID.AddInteger(LD->getAlignment());
637 ID.AddInteger(LD->isVolatile());
638 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
639 ID.AddInteger(ST->getAddressingMode());
640 ID.AddInteger(ST->isTruncatingStore());
641 ID.AddInteger((unsigned int)(ST->getStoredVT()));
642 ID.AddPointer(ST->getSrcValue());
643 ID.AddInteger(ST->getSrcValueOffset());
644 ID.AddInteger(ST->getAlignment());
645 ID.AddInteger(ST->isVolatile());
648 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
652 SelectionDAG::~SelectionDAG() {
653 while (!AllNodes.empty()) {
654 SDNode *N = AllNodes.begin();
655 N->SetNextInBucket(0);
656 if (N->OperandsNeedDelete)
657 delete [] N->OperandList;
660 AllNodes.pop_front();
664 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
665 if (Op.getValueType() == VT) return Op;
666 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
667 return getNode(ISD::AND, Op.getValueType(), Op,
668 getConstant(Imm, Op.getValueType()));
671 SDOperand SelectionDAG::getString(const std::string &Val) {
672 StringSDNode *&N = StringNodes[Val];
674 N = new StringSDNode(Val);
675 AllNodes.push_back(N);
677 return SDOperand(N, 0);
680 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
681 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
682 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
684 // Mask out any bits that are not valid for this constant.
685 Val &= MVT::getIntVTBitMask(VT);
687 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
689 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
692 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
693 return SDOperand(E, 0);
694 SDNode *N = new ConstantSDNode(isT, Val, VT);
695 CSEMap.InsertNode(N, IP);
696 AllNodes.push_back(N);
697 return SDOperand(N, 0);
700 SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
702 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
704 MVT::ValueType EltVT =
705 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
707 // Do the map lookup using the actual bit pattern for the floating point
708 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
709 // we don't have issues with SNANs.
710 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
712 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
716 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
717 if (!MVT::isVector(VT))
718 return SDOperand(N, 0);
720 N = new ConstantFPSDNode(isTarget, V, EltVT);
721 CSEMap.InsertNode(N, IP);
722 AllNodes.push_back(N);
725 SDOperand Result(N, 0);
726 if (MVT::isVector(VT)) {
727 SmallVector<SDOperand, 8> Ops;
728 Ops.assign(MVT::getVectorNumElements(VT), Result);
729 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
734 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
736 MVT::ValueType EltVT =
737 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
739 return getConstantFP(APFloat((float)Val), VT, isTarget);
741 return getConstantFP(APFloat(Val), VT, isTarget);
744 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
745 MVT::ValueType VT, int Offset,
747 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
749 if (GVar && GVar->isThreadLocal())
750 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
752 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
754 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
756 ID.AddInteger(Offset);
758 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
759 return SDOperand(E, 0);
760 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
761 CSEMap.InsertNode(N, IP);
762 AllNodes.push_back(N);
763 return SDOperand(N, 0);
766 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
768 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
770 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
773 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
774 return SDOperand(E, 0);
775 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
776 CSEMap.InsertNode(N, IP);
777 AllNodes.push_back(N);
778 return SDOperand(N, 0);
781 SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
782 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
784 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
787 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
788 return SDOperand(E, 0);
789 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
790 CSEMap.InsertNode(N, IP);
791 AllNodes.push_back(N);
792 return SDOperand(N, 0);
795 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
796 unsigned Alignment, int Offset,
798 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
800 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
801 ID.AddInteger(Alignment);
802 ID.AddInteger(Offset);
805 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
806 return SDOperand(E, 0);
807 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
808 CSEMap.InsertNode(N, IP);
809 AllNodes.push_back(N);
810 return SDOperand(N, 0);
814 SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
816 unsigned Alignment, int Offset,
818 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
820 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
821 ID.AddInteger(Alignment);
822 ID.AddInteger(Offset);
823 C->AddSelectionDAGCSEId(ID);
825 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
826 return SDOperand(E, 0);
827 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
828 CSEMap.InsertNode(N, IP);
829 AllNodes.push_back(N);
830 return SDOperand(N, 0);
834 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
836 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
839 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
840 return SDOperand(E, 0);
841 SDNode *N = new BasicBlockSDNode(MBB);
842 CSEMap.InsertNode(N, IP);
843 AllNodes.push_back(N);
844 return SDOperand(N, 0);
847 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
848 if ((unsigned)VT >= ValueTypeNodes.size())
849 ValueTypeNodes.resize(VT+1);
850 if (ValueTypeNodes[VT] == 0) {
851 ValueTypeNodes[VT] = new VTSDNode(VT);
852 AllNodes.push_back(ValueTypeNodes[VT]);
855 return SDOperand(ValueTypeNodes[VT], 0);
858 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
859 SDNode *&N = ExternalSymbols[Sym];
860 if (N) return SDOperand(N, 0);
861 N = new ExternalSymbolSDNode(false, Sym, VT);
862 AllNodes.push_back(N);
863 return SDOperand(N, 0);
866 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
868 SDNode *&N = TargetExternalSymbols[Sym];
869 if (N) return SDOperand(N, 0);
870 N = new ExternalSymbolSDNode(true, Sym, VT);
871 AllNodes.push_back(N);
872 return SDOperand(N, 0);
875 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
876 if ((unsigned)Cond >= CondCodeNodes.size())
877 CondCodeNodes.resize(Cond+1);
879 if (CondCodeNodes[Cond] == 0) {
880 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
881 AllNodes.push_back(CondCodeNodes[Cond]);
883 return SDOperand(CondCodeNodes[Cond], 0);
886 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
888 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
889 ID.AddInteger(RegNo);
891 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
892 return SDOperand(E, 0);
893 SDNode *N = new RegisterSDNode(RegNo, VT);
894 CSEMap.InsertNode(N, IP);
895 AllNodes.push_back(N);
896 return SDOperand(N, 0);
899 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
900 assert((!V || isa<PointerType>(V->getType())) &&
901 "SrcValue is not a pointer?");
904 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
906 ID.AddInteger(Offset);
908 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
909 return SDOperand(E, 0);
910 SDNode *N = new SrcValueSDNode(V, Offset);
911 CSEMap.InsertNode(N, IP);
912 AllNodes.push_back(N);
913 return SDOperand(N, 0);
916 SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
917 SDOperand N2, ISD::CondCode Cond) {
918 // These setcc operations always fold.
922 case ISD::SETFALSE2: return getConstant(0, VT);
924 case ISD::SETTRUE2: return getConstant(1, VT);
936 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
940 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
941 uint64_t C2 = N2C->getValue();
942 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
943 uint64_t C1 = N1C->getValue();
945 // Sign extend the operands if required
946 if (ISD::isSignedIntSetCC(Cond)) {
947 C1 = N1C->getSignExtended();
948 C2 = N2C->getSignExtended();
952 default: assert(0 && "Unknown integer setcc!");
953 case ISD::SETEQ: return getConstant(C1 == C2, VT);
954 case ISD::SETNE: return getConstant(C1 != C2, VT);
955 case ISD::SETULT: return getConstant(C1 < C2, VT);
956 case ISD::SETUGT: return getConstant(C1 > C2, VT);
957 case ISD::SETULE: return getConstant(C1 <= C2, VT);
958 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
959 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
960 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
961 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
962 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
966 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
967 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
969 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
972 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
973 return getNode(ISD::UNDEF, VT);
975 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
976 case ISD::SETNE: if (R==APFloat::cmpUnordered)
977 return getNode(ISD::UNDEF, VT);
979 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
980 R==APFloat::cmpLessThan, VT);
981 case ISD::SETLT: if (R==APFloat::cmpUnordered)
982 return getNode(ISD::UNDEF, VT);
984 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
985 case ISD::SETGT: if (R==APFloat::cmpUnordered)
986 return getNode(ISD::UNDEF, VT);
988 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
989 case ISD::SETLE: if (R==APFloat::cmpUnordered)
990 return getNode(ISD::UNDEF, VT);
992 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
993 R==APFloat::cmpEqual, VT);
994 case ISD::SETGE: if (R==APFloat::cmpUnordered)
995 return getNode(ISD::UNDEF, VT);
997 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
998 R==APFloat::cmpEqual, VT);
999 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1000 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1001 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1002 R==APFloat::cmpEqual, VT);
1003 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1004 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1005 R==APFloat::cmpLessThan, VT);
1006 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1007 R==APFloat::cmpUnordered, VT);
1008 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1009 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
1012 // Ensure that the constant occurs on the RHS.
1013 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
1016 // Could not fold it.
1020 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1021 /// this predicate to simplify operations downstream. Mask is known to be zero
1022 /// for bits that V cannot have.
1023 bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1024 unsigned Depth) const {
1025 // The masks are not wide enough to represent this type! Should use APInt.
1026 if (Op.getValueType() == MVT::i128)
1029 uint64_t KnownZero, KnownOne;
1030 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1031 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1032 return (KnownZero & Mask) == Mask;
1035 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
1036 /// known to be either zero or one and return them in the KnownZero/KnownOne
1037 /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1039 void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1040 uint64_t &KnownZero, uint64_t &KnownOne,
1041 unsigned Depth) const {
1042 KnownZero = KnownOne = 0; // Don't know anything.
1043 if (Depth == 6 || Mask == 0)
1044 return; // Limit search depth.
1046 // The masks are not wide enough to represent this type! Should use APInt.
1047 if (Op.getValueType() == MVT::i128)
1050 uint64_t KnownZero2, KnownOne2;
1052 switch (Op.getOpcode()) {
1054 // We know all of the bits for a constant!
1055 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1056 KnownZero = ~KnownOne & Mask;
1059 // If either the LHS or the RHS are Zero, the result is zero.
1060 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1062 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1063 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1064 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1066 // Output known-1 bits are only known if set in both the LHS & RHS.
1067 KnownOne &= KnownOne2;
1068 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1069 KnownZero |= KnownZero2;
1072 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1074 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1075 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1076 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1078 // Output known-0 bits are only known if clear in both the LHS & RHS.
1079 KnownZero &= KnownZero2;
1080 // Output known-1 are known to be set if set in either the LHS | RHS.
1081 KnownOne |= KnownOne2;
1084 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1085 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1086 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1087 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1089 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1090 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1091 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1092 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1093 KnownZero = KnownZeroOut;
1097 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1098 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1099 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1100 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1102 // Only known if known in both the LHS and RHS.
1103 KnownOne &= KnownOne2;
1104 KnownZero &= KnownZero2;
1106 case ISD::SELECT_CC:
1107 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1108 ComputeMaskedBits(Op.getOperand(2), 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;
1117 // If we know the result of a setcc has the top bits zero, use this info.
1118 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1119 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1122 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1123 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1124 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1125 KnownZero, KnownOne, Depth+1);
1126 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1127 KnownZero <<= SA->getValue();
1128 KnownOne <<= SA->getValue();
1129 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1133 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1134 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1135 MVT::ValueType VT = Op.getValueType();
1136 unsigned ShAmt = SA->getValue();
1138 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1139 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1140 KnownZero, KnownOne, Depth+1);
1141 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1142 KnownZero &= TypeMask;
1143 KnownOne &= TypeMask;
1144 KnownZero >>= ShAmt;
1147 uint64_t HighBits = (1ULL << ShAmt)-1;
1148 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1149 KnownZero |= HighBits; // High bits known zero.
1153 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1154 MVT::ValueType VT = Op.getValueType();
1155 unsigned ShAmt = SA->getValue();
1157 // Compute the new bits that are at the top now.
1158 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1160 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1161 // If any of the demanded bits are produced by the sign extension, we also
1162 // demand the input sign bit.
1163 uint64_t HighBits = (1ULL << ShAmt)-1;
1164 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1165 if (HighBits & Mask)
1166 InDemandedMask |= MVT::getIntVTSignBit(VT);
1168 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1170 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1171 KnownZero &= TypeMask;
1172 KnownOne &= TypeMask;
1173 KnownZero >>= ShAmt;
1176 // Handle the sign bits.
1177 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1178 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1180 if (KnownZero & SignBit) {
1181 KnownZero |= HighBits; // New bits are known zero.
1182 } else if (KnownOne & SignBit) {
1183 KnownOne |= HighBits; // New bits are known one.
1187 case ISD::SIGN_EXTEND_INREG: {
1188 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1190 // Sign extension. Compute the demanded bits in the result that are not
1191 // present in the input.
1192 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1194 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1195 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1197 // If the sign extended bits are demanded, we know that the sign
1200 InputDemandedBits |= InSignBit;
1202 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1203 KnownZero, KnownOne, Depth+1);
1204 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1206 // If the sign bit of the input is known set or clear, then we know the
1207 // top bits of the result.
1208 if (KnownZero & InSignBit) { // Input sign bit known clear
1209 KnownZero |= NewBits;
1210 KnownOne &= ~NewBits;
1211 } else if (KnownOne & InSignBit) { // Input sign bit known set
1212 KnownOne |= NewBits;
1213 KnownZero &= ~NewBits;
1214 } else { // Input sign bit unknown
1215 KnownZero &= ~NewBits;
1216 KnownOne &= ~NewBits;
1223 MVT::ValueType VT = Op.getValueType();
1224 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1225 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1230 if (ISD::isZEXTLoad(Op.Val)) {
1231 LoadSDNode *LD = cast<LoadSDNode>(Op);
1232 MVT::ValueType VT = LD->getLoadedVT();
1233 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1237 case ISD::ZERO_EXTEND: {
1238 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1239 uint64_t NewBits = (~InMask) & Mask;
1240 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1242 KnownZero |= NewBits & Mask;
1243 KnownOne &= ~NewBits;
1246 case ISD::SIGN_EXTEND: {
1247 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1248 unsigned InBits = MVT::getSizeInBits(InVT);
1249 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1250 uint64_t InSignBit = 1ULL << (InBits-1);
1251 uint64_t NewBits = (~InMask) & Mask;
1252 uint64_t InDemandedBits = Mask & InMask;
1254 // If any of the sign extended bits are demanded, we know that the sign
1257 InDemandedBits |= InSignBit;
1259 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1261 // If the sign bit is known zero or one, the top bits match.
1262 if (KnownZero & InSignBit) {
1263 KnownZero |= NewBits;
1264 KnownOne &= ~NewBits;
1265 } else if (KnownOne & InSignBit) {
1266 KnownOne |= NewBits;
1267 KnownZero &= ~NewBits;
1268 } else { // Otherwise, top bits aren't known.
1269 KnownOne &= ~NewBits;
1270 KnownZero &= ~NewBits;
1274 case ISD::ANY_EXTEND: {
1275 MVT::ValueType VT = Op.getOperand(0).getValueType();
1276 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1277 KnownZero, KnownOne, Depth+1);
1280 case ISD::TRUNCATE: {
1281 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1282 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1283 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1284 KnownZero &= OutMask;
1285 KnownOne &= OutMask;
1288 case ISD::AssertZext: {
1289 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1290 uint64_t InMask = MVT::getIntVTBitMask(VT);
1291 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1293 KnownZero |= (~InMask) & Mask;
1297 // If either the LHS or the RHS are Zero, the result is zero.
1298 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1299 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1300 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1301 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1303 // Output known-0 bits are known if clear or set in both the low clear bits
1304 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1305 // low 3 bits clear.
1306 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1307 CountTrailingZeros_64(~KnownZero2));
1309 KnownZero = (1ULL << KnownZeroOut) - 1;
1314 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1317 // We know that the top bits of C-X are clear if X contains less bits
1318 // than C (i.e. no wrap-around can happen). For example, 20-X is
1319 // positive if we can prove that X is >= 0 and < 16.
1320 MVT::ValueType VT = CLHS->getValueType(0);
1321 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1322 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1323 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1324 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1325 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1327 // If all of the MaskV bits are known to be zero, then we know the output
1328 // top bits are zero, because we now know that the output is from [0-C].
1329 if ((KnownZero & MaskV) == MaskV) {
1330 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1331 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1332 KnownOne = 0; // No one bits known.
1334 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1340 // Allow the target to implement this method for its nodes.
1341 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1342 case ISD::INTRINSIC_WO_CHAIN:
1343 case ISD::INTRINSIC_W_CHAIN:
1344 case ISD::INTRINSIC_VOID:
1345 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1351 /// ComputeNumSignBits - Return the number of times the sign bit of the
1352 /// register is replicated into the other bits. We know that at least 1 bit
1353 /// is always equal to the sign bit (itself), but other cases can give us
1354 /// information. For example, immediately after an "SRA X, 2", we know that
1355 /// the top 3 bits are all equal to each other, so we return 3.
1356 unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1357 MVT::ValueType VT = Op.getValueType();
1358 assert(MVT::isInteger(VT) && "Invalid VT!");
1359 unsigned VTBits = MVT::getSizeInBits(VT);
1363 return 1; // Limit search depth.
1365 switch (Op.getOpcode()) {
1367 case ISD::AssertSext:
1368 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1369 return VTBits-Tmp+1;
1370 case ISD::AssertZext:
1371 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1374 case ISD::Constant: {
1375 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1376 // If negative, invert the bits, then look at it.
1377 if (Val & MVT::getIntVTSignBit(VT))
1380 // Shift the bits so they are the leading bits in the int64_t.
1383 // Return # leading zeros. We use 'min' here in case Val was zero before
1384 // shifting. We don't want to return '64' as for an i32 "0".
1385 return std::min(VTBits, CountLeadingZeros_64(Val));
1388 case ISD::SIGN_EXTEND:
1389 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1390 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1392 case ISD::SIGN_EXTEND_INREG:
1393 // Max of the input and what this extends.
1394 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1397 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1398 return std::max(Tmp, Tmp2);
1401 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1402 // SRA X, C -> adds C sign bits.
1403 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1404 Tmp += C->getValue();
1405 if (Tmp > VTBits) Tmp = VTBits;
1409 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1410 // shl destroys sign bits.
1411 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1412 if (C->getValue() >= VTBits || // Bad shift.
1413 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1414 return Tmp - C->getValue();
1419 case ISD::XOR: // NOT is handled here.
1420 // Logical binary ops preserve the number of sign bits.
1421 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1422 if (Tmp == 1) return 1; // Early out.
1423 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1424 return std::min(Tmp, Tmp2);
1427 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1428 if (Tmp == 1) return 1; // Early out.
1429 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1430 return std::min(Tmp, Tmp2);
1433 // If setcc returns 0/-1, all bits are sign bits.
1434 if (TLI.getSetCCResultContents() ==
1435 TargetLowering::ZeroOrNegativeOneSetCCResult)
1440 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1441 unsigned RotAmt = C->getValue() & (VTBits-1);
1443 // Handle rotate right by N like a rotate left by 32-N.
1444 if (Op.getOpcode() == ISD::ROTR)
1445 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1447 // If we aren't rotating out all of the known-in sign bits, return the
1448 // number that are left. This handles rotl(sext(x), 1) for example.
1449 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1450 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1454 // Add can have at most one carry bit. Thus we know that the output
1455 // is, at worst, one more bit than the inputs.
1456 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1457 if (Tmp == 1) return 1; // Early out.
1459 // Special case decrementing a value (ADD X, -1):
1460 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1461 if (CRHS->isAllOnesValue()) {
1462 uint64_t KnownZero, KnownOne;
1463 uint64_t Mask = MVT::getIntVTBitMask(VT);
1464 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1466 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1468 if ((KnownZero|1) == Mask)
1471 // If we are subtracting one from a positive number, there is no carry
1472 // out of the result.
1473 if (KnownZero & MVT::getIntVTSignBit(VT))
1477 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1478 if (Tmp2 == 1) return 1;
1479 return std::min(Tmp, Tmp2)-1;
1483 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1484 if (Tmp2 == 1) return 1;
1487 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1488 if (CLHS->getValue() == 0) {
1489 uint64_t KnownZero, KnownOne;
1490 uint64_t Mask = MVT::getIntVTBitMask(VT);
1491 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1492 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1494 if ((KnownZero|1) == Mask)
1497 // If the input is known to be positive (the sign bit is known clear),
1498 // the output of the NEG has the same number of sign bits as the input.
1499 if (KnownZero & MVT::getIntVTSignBit(VT))
1502 // Otherwise, we treat this like a SUB.
1505 // Sub can have at most one carry bit. Thus we know that the output
1506 // is, at worst, one more bit than the inputs.
1507 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1508 if (Tmp == 1) return 1; // Early out.
1509 return std::min(Tmp, Tmp2)-1;
1512 // FIXME: it's tricky to do anything useful for this, but it is an important
1513 // case for targets like X86.
1517 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1518 if (Op.getOpcode() == ISD::LOAD) {
1519 LoadSDNode *LD = cast<LoadSDNode>(Op);
1520 unsigned ExtType = LD->getExtensionType();
1523 case ISD::SEXTLOAD: // '17' bits known
1524 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1525 return VTBits-Tmp+1;
1526 case ISD::ZEXTLOAD: // '16' bits known
1527 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1532 // Allow the target to implement this method for its nodes.
1533 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1534 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1535 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1536 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1537 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1538 if (NumBits > 1) return NumBits;
1541 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1542 // use this information.
1543 uint64_t KnownZero, KnownOne;
1544 uint64_t Mask = MVT::getIntVTBitMask(VT);
1545 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1547 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1548 if (KnownZero & SignBit) { // SignBit is 0
1550 } else if (KnownOne & SignBit) { // SignBit is 1;
1557 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1558 // the number of identical bits in the top of the input value.
1561 // Return # leading zeros. We use 'min' here in case Val was zero before
1562 // shifting. We don't want to return '64' as for an i32 "0".
1563 return std::min(VTBits, CountLeadingZeros_64(Mask));
1567 /// getNode - Gets or creates the specified node.
1569 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
1570 FoldingSetNodeID ID;
1571 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
1573 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1574 return SDOperand(E, 0);
1575 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
1576 CSEMap.InsertNode(N, IP);
1578 AllNodes.push_back(N);
1579 return SDOperand(N, 0);
1582 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1583 SDOperand Operand) {
1585 // Constant fold unary operations with an integer constant operand.
1586 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1587 uint64_t Val = C->getValue();
1590 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
1591 case ISD::ANY_EXTEND:
1592 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1593 case ISD::TRUNCATE: return getConstant(Val, VT);
1594 case ISD::UINT_TO_FP:
1595 case ISD::SINT_TO_FP: {
1596 const uint64_t zero[] = {0, 0};
1597 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
1598 (void)apf.convertFromZeroExtendedInteger(&Val,
1599 MVT::getSizeInBits(Operand.getValueType()),
1600 Opcode==ISD::SINT_TO_FP,
1601 APFloat::rmNearestTiesToEven);
1602 return getConstantFP(apf, VT);
1604 case ISD::BIT_CONVERT:
1605 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
1606 return getConstantFP(BitsToFloat(Val), VT);
1607 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
1608 return getConstantFP(BitsToDouble(Val), VT);
1612 default: assert(0 && "Invalid bswap!"); break;
1613 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1614 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1615 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1620 default: assert(0 && "Invalid ctpop!"); break;
1621 case MVT::i1: return getConstant(Val != 0, VT);
1623 Tmp1 = (unsigned)Val & 0xFF;
1624 return getConstant(CountPopulation_32(Tmp1), VT);
1626 Tmp1 = (unsigned)Val & 0xFFFF;
1627 return getConstant(CountPopulation_32(Tmp1), VT);
1629 return getConstant(CountPopulation_32((unsigned)Val), VT);
1631 return getConstant(CountPopulation_64(Val), VT);
1635 default: assert(0 && "Invalid ctlz!"); break;
1636 case MVT::i1: return getConstant(Val == 0, VT);
1638 Tmp1 = (unsigned)Val & 0xFF;
1639 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1641 Tmp1 = (unsigned)Val & 0xFFFF;
1642 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1644 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1646 return getConstant(CountLeadingZeros_64(Val), VT);
1650 default: assert(0 && "Invalid cttz!"); break;
1651 case MVT::i1: return getConstant(Val == 0, VT);
1653 Tmp1 = (unsigned)Val | 0x100;
1654 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1656 Tmp1 = (unsigned)Val | 0x10000;
1657 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1659 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1661 return getConstant(CountTrailingZeros_64(Val), VT);
1666 // Constant fold unary operations with a floating point constant operand.
1667 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1668 APFloat V = C->getValueAPF(); // make copy
1672 return getConstantFP(V, VT);
1675 return getConstantFP(V, VT);
1677 case ISD::FP_EXTEND:
1678 // This can return overflow, underflow, or inexact; we don't care.
1679 // FIXME need to be more flexible about rounding mode.
1680 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle :
1681 VT==MVT::f64 ? APFloat::IEEEdouble :
1682 VT==MVT::f80 ? APFloat::x87DoubleExtended :
1683 VT==MVT::f128 ? APFloat::IEEEquad :
1685 APFloat::rmNearestTiesToEven);
1686 return getConstantFP(V, VT);
1687 case ISD::FP_TO_SINT:
1688 case ISD::FP_TO_UINT: {
1690 assert(integerPartWidth >= 64);
1691 // FIXME need to be more flexible about rounding mode.
1692 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1693 Opcode==ISD::FP_TO_SINT,
1694 APFloat::rmTowardZero);
1695 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1697 return getConstant(x, VT);
1699 case ISD::BIT_CONVERT:
1700 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1701 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1702 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1703 return getConstant(V.convertToAPInt().getZExtValue(), VT);
1708 unsigned OpOpcode = Operand.Val->getOpcode();
1710 case ISD::TokenFactor:
1711 return Operand; // Factor of one node? No factor.
1713 case ISD::FP_EXTEND:
1714 assert(MVT::isFloatingPoint(VT) &&
1715 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
1717 case ISD::SIGN_EXTEND:
1718 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1719 "Invalid SIGN_EXTEND!");
1720 if (Operand.getValueType() == VT) return Operand; // noop extension
1721 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
1722 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1723 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1725 case ISD::ZERO_EXTEND:
1726 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1727 "Invalid ZERO_EXTEND!");
1728 if (Operand.getValueType() == VT) return Operand; // noop extension
1729 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
1730 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1731 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1733 case ISD::ANY_EXTEND:
1734 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1735 "Invalid ANY_EXTEND!");
1736 if (Operand.getValueType() == VT) return Operand; // noop extension
1737 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
1738 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1739 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1740 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1743 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1744 "Invalid TRUNCATE!");
1745 if (Operand.getValueType() == VT) return Operand; // noop truncate
1746 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
1747 if (OpOpcode == ISD::TRUNCATE)
1748 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1749 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1750 OpOpcode == ISD::ANY_EXTEND) {
1751 // If the source is smaller than the dest, we still need an extend.
1752 if (Operand.Val->getOperand(0).getValueType() < VT)
1753 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1754 else if (Operand.Val->getOperand(0).getValueType() > VT)
1755 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1757 return Operand.Val->getOperand(0);
1760 case ISD::BIT_CONVERT:
1761 // Basic sanity checking.
1762 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
1763 && "Cannot BIT_CONVERT between types of different sizes!");
1764 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1765 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1766 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1767 if (OpOpcode == ISD::UNDEF)
1768 return getNode(ISD::UNDEF, VT);
1770 case ISD::SCALAR_TO_VECTOR:
1771 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1772 MVT::getVectorElementType(VT) == Operand.getValueType() &&
1773 "Illegal SCALAR_TO_VECTOR node!");
1776 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1777 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1778 Operand.Val->getOperand(0));
1779 if (OpOpcode == ISD::FNEG) // --X -> X
1780 return Operand.Val->getOperand(0);
1783 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1784 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1789 SDVTList VTs = getVTList(VT);
1790 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1791 FoldingSetNodeID ID;
1792 SDOperand Ops[1] = { Operand };
1793 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
1795 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1796 return SDOperand(E, 0);
1797 N = new UnarySDNode(Opcode, VTs, Operand);
1798 CSEMap.InsertNode(N, IP);
1800 N = new UnarySDNode(Opcode, VTs, Operand);
1802 AllNodes.push_back(N);
1803 return SDOperand(N, 0);
1808 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1809 SDOperand N1, SDOperand N2) {
1812 case ISD::TokenFactor:
1813 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1814 N2.getValueType() == MVT::Other && "Invalid token factor!");
1823 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1830 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1837 assert(N1.getValueType() == N2.getValueType() &&
1838 N1.getValueType() == VT && "Binary operator types must match!");
1840 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1841 assert(N1.getValueType() == VT &&
1842 MVT::isFloatingPoint(N1.getValueType()) &&
1843 MVT::isFloatingPoint(N2.getValueType()) &&
1844 "Invalid FCOPYSIGN!");
1851 assert(VT == N1.getValueType() &&
1852 "Shift operators return type must be the same as their first arg");
1853 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1854 VT != MVT::i1 && "Shifts only work on integers");
1856 case ISD::FP_ROUND_INREG: {
1857 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1858 assert(VT == N1.getValueType() && "Not an inreg round!");
1859 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1860 "Cannot FP_ROUND_INREG integer types");
1861 assert(EVT <= VT && "Not rounding down!");
1864 case ISD::AssertSext:
1865 case ISD::AssertZext:
1866 case ISD::SIGN_EXTEND_INREG: {
1867 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1868 assert(VT == N1.getValueType() && "Not an inreg extend!");
1869 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1870 "Cannot *_EXTEND_INREG FP types");
1871 assert(EVT <= VT && "Not extending!");
1878 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1879 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1881 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1882 int64_t Val = N1C->getValue();
1883 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1884 Val <<= 64-FromBits;
1885 Val >>= 64-FromBits;
1886 return getConstant(Val, VT);
1890 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1892 case ISD::ADD: return getConstant(C1 + C2, VT);
1893 case ISD::SUB: return getConstant(C1 - C2, VT);
1894 case ISD::MUL: return getConstant(C1 * C2, VT);
1896 if (C2) return getConstant(C1 / C2, VT);
1899 if (C2) return getConstant(C1 % C2, VT);
1902 if (C2) return getConstant(N1C->getSignExtended() /
1903 N2C->getSignExtended(), VT);
1906 if (C2) return getConstant(N1C->getSignExtended() %
1907 N2C->getSignExtended(), VT);
1909 case ISD::AND : return getConstant(C1 & C2, VT);
1910 case ISD::OR : return getConstant(C1 | C2, VT);
1911 case ISD::XOR : return getConstant(C1 ^ C2, VT);
1912 case ISD::SHL : return getConstant(C1 << C2, VT);
1913 case ISD::SRL : return getConstant(C1 >> C2, VT);
1914 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
1916 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1919 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1923 } else { // Cannonicalize constant to RHS if commutative
1924 if (isCommutativeBinOp(Opcode)) {
1925 std::swap(N1C, N2C);
1931 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1932 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1935 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
1936 APFloat::opStatus s;
1939 s = V1.add(V2, APFloat::rmNearestTiesToEven);
1940 if (s!=APFloat::opInvalidOp)
1941 return getConstantFP(V1, VT);
1944 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
1945 if (s!=APFloat::opInvalidOp)
1946 return getConstantFP(V1, VT);
1949 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
1950 if (s!=APFloat::opInvalidOp)
1951 return getConstantFP(V1, VT);
1954 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
1955 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1956 return getConstantFP(V1, VT);
1959 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
1960 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1961 return getConstantFP(V1, VT);
1963 case ISD::FCOPYSIGN:
1965 return getConstantFP(V1, VT);
1968 } else { // Cannonicalize constant to RHS if commutative
1969 if (isCommutativeBinOp(Opcode)) {
1970 std::swap(N1CFP, N2CFP);
1976 // Canonicalize an UNDEF to the RHS, even over a constant.
1977 if (N1.getOpcode() == ISD::UNDEF) {
1978 if (isCommutativeBinOp(Opcode)) {
1982 case ISD::FP_ROUND_INREG:
1983 case ISD::SIGN_EXTEND_INREG:
1989 return N1; // fold op(undef, arg2) -> undef
1996 if (!MVT::isVector(VT))
1997 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1998 // For vectors, we can't easily build an all zero vector, just return
2005 // Fold a bunch of operators when the RHS is undef.
2006 if (N2.getOpcode() == ISD::UNDEF) {
2022 return N2; // fold op(arg1, undef) -> undef
2027 if (!MVT::isVector(VT))
2028 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2029 // For vectors, we can't easily build an all zero vector, just return
2033 if (!MVT::isVector(VT))
2034 return getConstant(MVT::getIntVTBitMask(VT), VT);
2035 // For vectors, we can't easily build an all one vector, just return
2045 case ISD::TokenFactor:
2046 // Fold trivial token factors.
2047 if (N1.getOpcode() == ISD::EntryToken) return N2;
2048 if (N2.getOpcode() == ISD::EntryToken) return N1;
2052 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2053 // worth handling here.
2054 if (N2C && N2C->getValue() == 0)
2059 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
2060 // worth handling here.
2061 if (N2C && N2C->getValue() == 0)
2064 case ISD::FP_ROUND_INREG:
2065 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
2067 case ISD::SIGN_EXTEND_INREG: {
2068 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2069 if (EVT == VT) return N1; // Not actually extending
2072 case ISD::EXTRACT_VECTOR_ELT:
2073 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2075 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2076 // expanding copies of large vectors from registers.
2077 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2078 N1.getNumOperands() > 0) {
2080 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2081 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2082 N1.getOperand(N2C->getValue() / Factor),
2083 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2086 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2087 // expanding large vector constants.
2088 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2089 return N1.getOperand(N2C->getValue());
2091 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2092 // operations are lowered to scalars.
2093 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2094 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2096 return N1.getOperand(1);
2098 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2101 case ISD::EXTRACT_ELEMENT:
2102 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
2104 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2105 // 64-bit integers into 32-bit parts. Instead of building the extract of
2106 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2107 if (N1.getOpcode() == ISD::BUILD_PAIR)
2108 return N1.getOperand(N2C->getValue());
2110 // EXTRACT_ELEMENT of a constant int is also very common.
2111 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2112 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2113 return getConstant(C->getValue() >> Shift, VT);
2117 // FIXME: figure out how to safely handle things like
2118 // int foo(int x) { return 1 << (x & 255); }
2119 // int bar() { return foo(256); }
2124 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2125 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
2126 return getNode(Opcode, VT, N1, N2.getOperand(0));
2127 else if (N2.getOpcode() == ISD::AND)
2128 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
2129 // If the and is only masking out bits that cannot effect the shift,
2130 // eliminate the and.
2131 unsigned NumBits = MVT::getSizeInBits(VT);
2132 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2133 return getNode(Opcode, VT, N1, N2.getOperand(0));
2139 // Memoize this node if possible.
2141 SDVTList VTs = getVTList(VT);
2142 if (VT != MVT::Flag) {
2143 SDOperand Ops[] = { N1, N2 };
2144 FoldingSetNodeID ID;
2145 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
2147 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2148 return SDOperand(E, 0);
2149 N = new BinarySDNode(Opcode, VTs, N1, N2);
2150 CSEMap.InsertNode(N, IP);
2152 N = new BinarySDNode(Opcode, VTs, N1, N2);
2155 AllNodes.push_back(N);
2156 return SDOperand(N, 0);
2159 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2160 SDOperand N1, SDOperand N2, SDOperand N3) {
2161 // Perform various simplifications.
2162 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2163 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2166 // Use FoldSetCC to simplify SETCC's.
2167 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
2168 if (Simp.Val) return Simp;
2173 if (N1C->getValue())
2174 return N2; // select true, X, Y -> X
2176 return N3; // select false, X, Y -> Y
2178 if (N2 == N3) return N2; // select C, X, X -> X
2182 if (N2C->getValue()) // Unconditional branch
2183 return getNode(ISD::BR, MVT::Other, N1, N3);
2185 return N1; // Never-taken branch
2187 case ISD::VECTOR_SHUFFLE:
2188 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2189 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2190 N3.getOpcode() == ISD::BUILD_VECTOR &&
2191 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2192 "Illegal VECTOR_SHUFFLE node!");
2194 case ISD::BIT_CONVERT:
2195 // Fold bit_convert nodes from a type to themselves.
2196 if (N1.getValueType() == VT)
2201 // Memoize node if it doesn't produce a flag.
2203 SDVTList VTs = getVTList(VT);
2204 if (VT != MVT::Flag) {
2205 SDOperand Ops[] = { N1, N2, N3 };
2206 FoldingSetNodeID ID;
2207 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2209 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2210 return SDOperand(E, 0);
2211 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2212 CSEMap.InsertNode(N, IP);
2214 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2216 AllNodes.push_back(N);
2217 return SDOperand(N, 0);
2220 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2221 SDOperand N1, SDOperand N2, SDOperand N3,
2223 SDOperand Ops[] = { N1, N2, N3, N4 };
2224 return getNode(Opcode, VT, Ops, 4);
2227 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2228 SDOperand N1, SDOperand N2, SDOperand N3,
2229 SDOperand N4, SDOperand N5) {
2230 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2231 return getNode(Opcode, VT, Ops, 5);
2234 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2235 SDOperand Chain, SDOperand Ptr,
2236 const Value *SV, int SVOffset,
2237 bool isVolatile, unsigned Alignment) {
2238 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2240 if (VT != MVT::iPTR) {
2241 Ty = MVT::getTypeForValueType(VT);
2243 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2244 assert(PT && "Value for load must be a pointer");
2245 Ty = PT->getElementType();
2247 assert(Ty && "Could not get type information for load");
2248 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2250 SDVTList VTs = getVTList(VT, MVT::Other);
2251 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2252 SDOperand Ops[] = { Chain, Ptr, Undef };
2253 FoldingSetNodeID ID;
2254 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2255 ID.AddInteger(ISD::UNINDEXED);
2256 ID.AddInteger(ISD::NON_EXTLOAD);
2257 ID.AddInteger((unsigned int)VT);
2259 ID.AddInteger(SVOffset);
2260 ID.AddInteger(Alignment);
2261 ID.AddInteger(isVolatile);
2263 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2264 return SDOperand(E, 0);
2265 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
2266 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2268 CSEMap.InsertNode(N, IP);
2269 AllNodes.push_back(N);
2270 return SDOperand(N, 0);
2273 SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
2274 SDOperand Chain, SDOperand Ptr,
2276 int SVOffset, MVT::ValueType EVT,
2277 bool isVolatile, unsigned Alignment) {
2278 // If they are asking for an extending load from/to the same thing, return a
2281 ExtType = ISD::NON_EXTLOAD;
2283 if (MVT::isVector(VT))
2284 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
2286 assert(EVT < VT && "Should only be an extending load, not truncating!");
2287 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2288 "Cannot sign/zero extend a FP/Vector load!");
2289 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2290 "Cannot convert from FP to Int or Int -> FP!");
2292 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2294 if (VT != MVT::iPTR) {
2295 Ty = MVT::getTypeForValueType(VT);
2297 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2298 assert(PT && "Value for load must be a pointer");
2299 Ty = PT->getElementType();
2301 assert(Ty && "Could not get type information for load");
2302 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2304 SDVTList VTs = getVTList(VT, MVT::Other);
2305 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2306 SDOperand Ops[] = { Chain, Ptr, Undef };
2307 FoldingSetNodeID ID;
2308 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2309 ID.AddInteger(ISD::UNINDEXED);
2310 ID.AddInteger(ExtType);
2311 ID.AddInteger((unsigned int)EVT);
2313 ID.AddInteger(SVOffset);
2314 ID.AddInteger(Alignment);
2315 ID.AddInteger(isVolatile);
2317 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2318 return SDOperand(E, 0);
2319 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
2320 SV, SVOffset, Alignment, isVolatile);
2321 CSEMap.InsertNode(N, IP);
2322 AllNodes.push_back(N);
2323 return SDOperand(N, 0);
2327 SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2328 SDOperand Offset, ISD::MemIndexedMode AM) {
2329 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
2330 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2331 "Load is already a indexed load!");
2332 MVT::ValueType VT = OrigLoad.getValueType();
2333 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
2334 SDOperand Ops[] = { LD->getChain(), Base, Offset };
2335 FoldingSetNodeID ID;
2336 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2338 ID.AddInteger(LD->getExtensionType());
2339 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
2340 ID.AddPointer(LD->getSrcValue());
2341 ID.AddInteger(LD->getSrcValueOffset());
2342 ID.AddInteger(LD->getAlignment());
2343 ID.AddInteger(LD->isVolatile());
2345 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2346 return SDOperand(E, 0);
2347 SDNode *N = new LoadSDNode(Ops, VTs, AM,
2348 LD->getExtensionType(), LD->getLoadedVT(),
2349 LD->getSrcValue(), LD->getSrcValueOffset(),
2350 LD->getAlignment(), LD->isVolatile());
2351 CSEMap.InsertNode(N, IP);
2352 AllNodes.push_back(N);
2353 return SDOperand(N, 0);
2356 SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
2357 SDOperand Ptr, const Value *SV, int SVOffset,
2358 bool isVolatile, unsigned Alignment) {
2359 MVT::ValueType VT = Val.getValueType();
2361 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2363 if (VT != MVT::iPTR) {
2364 Ty = MVT::getTypeForValueType(VT);
2366 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2367 assert(PT && "Value for store must be a pointer");
2368 Ty = PT->getElementType();
2370 assert(Ty && "Could not get type information for store");
2371 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2373 SDVTList VTs = getVTList(MVT::Other);
2374 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2375 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2376 FoldingSetNodeID ID;
2377 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2378 ID.AddInteger(ISD::UNINDEXED);
2379 ID.AddInteger(false);
2380 ID.AddInteger((unsigned int)VT);
2382 ID.AddInteger(SVOffset);
2383 ID.AddInteger(Alignment);
2384 ID.AddInteger(isVolatile);
2386 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2387 return SDOperand(E, 0);
2388 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
2389 VT, SV, SVOffset, Alignment, isVolatile);
2390 CSEMap.InsertNode(N, IP);
2391 AllNodes.push_back(N);
2392 return SDOperand(N, 0);
2395 SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
2396 SDOperand Ptr, const Value *SV,
2397 int SVOffset, MVT::ValueType SVT,
2398 bool isVolatile, unsigned Alignment) {
2399 MVT::ValueType VT = Val.getValueType();
2400 bool isTrunc = VT != SVT;
2402 assert(VT > SVT && "Not a truncation?");
2403 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2404 "Can't do FP-INT conversion!");
2406 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2408 if (VT != MVT::iPTR) {
2409 Ty = MVT::getTypeForValueType(VT);
2411 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2412 assert(PT && "Value for store must be a pointer");
2413 Ty = PT->getElementType();
2415 assert(Ty && "Could not get type information for store");
2416 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2418 SDVTList VTs = getVTList(MVT::Other);
2419 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2420 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2421 FoldingSetNodeID ID;
2422 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2423 ID.AddInteger(ISD::UNINDEXED);
2424 ID.AddInteger(isTrunc);
2425 ID.AddInteger((unsigned int)SVT);
2427 ID.AddInteger(SVOffset);
2428 ID.AddInteger(Alignment);
2429 ID.AddInteger(isVolatile);
2431 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2432 return SDOperand(E, 0);
2433 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, isTrunc,
2434 SVT, SV, SVOffset, Alignment, isVolatile);
2435 CSEMap.InsertNode(N, IP);
2436 AllNodes.push_back(N);
2437 return SDOperand(N, 0);
2441 SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2442 SDOperand Offset, ISD::MemIndexedMode AM) {
2443 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2444 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2445 "Store is already a indexed store!");
2446 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2447 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2448 FoldingSetNodeID ID;
2449 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2451 ID.AddInteger(ST->isTruncatingStore());
2452 ID.AddInteger((unsigned int)(ST->getStoredVT()));
2453 ID.AddPointer(ST->getSrcValue());
2454 ID.AddInteger(ST->getSrcValueOffset());
2455 ID.AddInteger(ST->getAlignment());
2456 ID.AddInteger(ST->isVolatile());
2458 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2459 return SDOperand(E, 0);
2460 SDNode *N = new StoreSDNode(Ops, VTs, AM,
2461 ST->isTruncatingStore(), ST->getStoredVT(),
2462 ST->getSrcValue(), ST->getSrcValueOffset(),
2463 ST->getAlignment(), ST->isVolatile());
2464 CSEMap.InsertNode(N, IP);
2465 AllNodes.push_back(N);
2466 return SDOperand(N, 0);
2469 SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2470 SDOperand Chain, SDOperand Ptr,
2472 SDOperand Ops[] = { Chain, Ptr, SV };
2473 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
2476 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2477 const SDOperand *Ops, unsigned NumOps) {
2479 case 0: return getNode(Opcode, VT);
2480 case 1: return getNode(Opcode, VT, Ops[0]);
2481 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2482 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
2488 case ISD::SELECT_CC: {
2489 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
2490 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2491 "LHS and RHS of condition must have same type!");
2492 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2493 "True and False arms of SelectCC must have same type!");
2494 assert(Ops[2].getValueType() == VT &&
2495 "select_cc node must be of same type as true and false value!");
2499 assert(NumOps == 5 && "BR_CC takes 5 operands!");
2500 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2501 "LHS/RHS of comparison should match types!");
2508 SDVTList VTs = getVTList(VT);
2509 if (VT != MVT::Flag) {
2510 FoldingSetNodeID ID;
2511 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
2513 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2514 return SDOperand(E, 0);
2515 N = new SDNode(Opcode, VTs, Ops, NumOps);
2516 CSEMap.InsertNode(N, IP);
2518 N = new SDNode(Opcode, VTs, Ops, NumOps);
2520 AllNodes.push_back(N);
2521 return SDOperand(N, 0);
2524 SDOperand SelectionDAG::getNode(unsigned Opcode,
2525 std::vector<MVT::ValueType> &ResultTys,
2526 const SDOperand *Ops, unsigned NumOps) {
2527 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2531 SDOperand SelectionDAG::getNode(unsigned Opcode,
2532 const MVT::ValueType *VTs, unsigned NumVTs,
2533 const SDOperand *Ops, unsigned NumOps) {
2535 return getNode(Opcode, VTs[0], Ops, NumOps);
2536 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2539 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2540 const SDOperand *Ops, unsigned NumOps) {
2541 if (VTList.NumVTs == 1)
2542 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
2545 // FIXME: figure out how to safely handle things like
2546 // int foo(int x) { return 1 << (x & 255); }
2547 // int bar() { return foo(256); }
2549 case ISD::SRA_PARTS:
2550 case ISD::SRL_PARTS:
2551 case ISD::SHL_PARTS:
2552 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2553 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
2554 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2555 else if (N3.getOpcode() == ISD::AND)
2556 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2557 // If the and is only masking out bits that cannot effect the shift,
2558 // eliminate the and.
2559 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2560 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2561 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2567 // Memoize the node unless it returns a flag.
2569 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
2570 FoldingSetNodeID ID;
2571 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
2573 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2574 return SDOperand(E, 0);
2576 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2577 else if (NumOps == 2)
2578 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2579 else if (NumOps == 3)
2580 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2582 N = new SDNode(Opcode, VTList, Ops, NumOps);
2583 CSEMap.InsertNode(N, IP);
2586 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2587 else if (NumOps == 2)
2588 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2589 else if (NumOps == 3)
2590 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2592 N = new SDNode(Opcode, VTList, Ops, NumOps);
2594 AllNodes.push_back(N);
2595 return SDOperand(N, 0);
2598 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2599 return getNode(Opcode, VTList, 0, 0);
2602 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2604 SDOperand Ops[] = { N1 };
2605 return getNode(Opcode, VTList, Ops, 1);
2608 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2609 SDOperand N1, SDOperand N2) {
2610 SDOperand Ops[] = { N1, N2 };
2611 return getNode(Opcode, VTList, Ops, 2);
2614 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2615 SDOperand N1, SDOperand N2, SDOperand N3) {
2616 SDOperand Ops[] = { N1, N2, N3 };
2617 return getNode(Opcode, VTList, Ops, 3);
2620 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2621 SDOperand N1, SDOperand N2, SDOperand N3,
2623 SDOperand Ops[] = { N1, N2, N3, N4 };
2624 return getNode(Opcode, VTList, Ops, 4);
2627 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2628 SDOperand N1, SDOperand N2, SDOperand N3,
2629 SDOperand N4, SDOperand N5) {
2630 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2631 return getNode(Opcode, VTList, Ops, 5);
2634 SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
2635 if (!MVT::isExtendedVT(VT))
2636 return makeVTList(SDNode::getValueTypeList(VT), 1);
2638 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2639 E = VTList.end(); I != E; ++I) {
2640 if (I->size() == 1 && (*I)[0] == VT)
2641 return makeVTList(&(*I)[0], 1);
2643 std::vector<MVT::ValueType> V;
2645 VTList.push_front(V);
2646 return makeVTList(&(*VTList.begin())[0], 1);
2649 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
2650 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2651 E = VTList.end(); I != E; ++I) {
2652 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
2653 return makeVTList(&(*I)[0], 2);
2655 std::vector<MVT::ValueType> V;
2658 VTList.push_front(V);
2659 return makeVTList(&(*VTList.begin())[0], 2);
2661 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2662 MVT::ValueType VT3) {
2663 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2664 E = VTList.end(); I != E; ++I) {
2665 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2667 return makeVTList(&(*I)[0], 3);
2669 std::vector<MVT::ValueType> V;
2673 VTList.push_front(V);
2674 return makeVTList(&(*VTList.begin())[0], 3);
2677 SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2679 case 0: assert(0 && "Cannot have nodes without results!");
2680 case 1: return getVTList(VTs[0]);
2681 case 2: return getVTList(VTs[0], VTs[1]);
2682 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2686 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2687 E = VTList.end(); I != E; ++I) {
2688 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2690 bool NoMatch = false;
2691 for (unsigned i = 2; i != NumVTs; ++i)
2692 if (VTs[i] != (*I)[i]) {
2697 return makeVTList(&*I->begin(), NumVTs);
2700 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2701 return makeVTList(&*VTList.begin()->begin(), NumVTs);
2705 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2706 /// specified operands. If the resultant node already exists in the DAG,
2707 /// this does not modify the specified node, instead it returns the node that
2708 /// already exists. If the resultant node does not exist in the DAG, the
2709 /// input node is returned. As a degenerate case, if you specify the same
2710 /// input operands as the node already has, the input node is returned.
2711 SDOperand SelectionDAG::
2712 UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2713 SDNode *N = InN.Val;
2714 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2716 // Check to see if there is no change.
2717 if (Op == N->getOperand(0)) return InN;
2719 // See if the modified node already exists.
2720 void *InsertPos = 0;
2721 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2722 return SDOperand(Existing, InN.ResNo);
2724 // Nope it doesn't. Remove the node from it's current place in the maps.
2726 RemoveNodeFromCSEMaps(N);
2728 // Now we update the operands.
2729 N->OperandList[0].Val->removeUser(N);
2731 N->OperandList[0] = Op;
2733 // If this gets put into a CSE map, add it.
2734 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2738 SDOperand SelectionDAG::
2739 UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2740 SDNode *N = InN.Val;
2741 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2743 // Check to see if there is no change.
2744 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2745 return InN; // No operands changed, just return the input node.
2747 // See if the modified node already exists.
2748 void *InsertPos = 0;
2749 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2750 return SDOperand(Existing, InN.ResNo);
2752 // Nope it doesn't. Remove the node from it's current place in the maps.
2754 RemoveNodeFromCSEMaps(N);
2756 // Now we update the operands.
2757 if (N->OperandList[0] != Op1) {
2758 N->OperandList[0].Val->removeUser(N);
2759 Op1.Val->addUser(N);
2760 N->OperandList[0] = Op1;
2762 if (N->OperandList[1] != Op2) {
2763 N->OperandList[1].Val->removeUser(N);
2764 Op2.Val->addUser(N);
2765 N->OperandList[1] = Op2;
2768 // If this gets put into a CSE map, add it.
2769 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2773 SDOperand SelectionDAG::
2774 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2775 SDOperand Ops[] = { Op1, Op2, Op3 };
2776 return UpdateNodeOperands(N, Ops, 3);
2779 SDOperand SelectionDAG::
2780 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2781 SDOperand Op3, SDOperand Op4) {
2782 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2783 return UpdateNodeOperands(N, Ops, 4);
2786 SDOperand SelectionDAG::
2787 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2788 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2789 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2790 return UpdateNodeOperands(N, Ops, 5);
2794 SDOperand SelectionDAG::
2795 UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
2796 SDNode *N = InN.Val;
2797 assert(N->getNumOperands() == NumOps &&
2798 "Update with wrong number of operands");
2800 // Check to see if there is no change.
2801 bool AnyChange = false;
2802 for (unsigned i = 0; i != NumOps; ++i) {
2803 if (Ops[i] != N->getOperand(i)) {
2809 // No operands changed, just return the input node.
2810 if (!AnyChange) return InN;
2812 // See if the modified node already exists.
2813 void *InsertPos = 0;
2814 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
2815 return SDOperand(Existing, InN.ResNo);
2817 // Nope it doesn't. Remove the node from it's current place in the maps.
2819 RemoveNodeFromCSEMaps(N);
2821 // Now we update the operands.
2822 for (unsigned i = 0; i != NumOps; ++i) {
2823 if (N->OperandList[i] != Ops[i]) {
2824 N->OperandList[i].Val->removeUser(N);
2825 Ops[i].Val->addUser(N);
2826 N->OperandList[i] = Ops[i];
2830 // If this gets put into a CSE map, add it.
2831 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2836 /// MorphNodeTo - This frees the operands of the current node, resets the
2837 /// opcode, types, and operands to the specified value. This should only be
2838 /// used by the SelectionDAG class.
2839 void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2840 const SDOperand *Ops, unsigned NumOps) {
2843 NumValues = L.NumVTs;
2845 // Clear the operands list, updating used nodes to remove this from their
2847 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2848 I->Val->removeUser(this);
2850 // If NumOps is larger than the # of operands we currently have, reallocate
2851 // the operand list.
2852 if (NumOps > NumOperands) {
2853 if (OperandsNeedDelete)
2854 delete [] OperandList;
2855 OperandList = new SDOperand[NumOps];
2856 OperandsNeedDelete = true;
2859 // Assign the new operands.
2860 NumOperands = NumOps;
2862 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2863 OperandList[i] = Ops[i];
2864 SDNode *N = OperandList[i].Val;
2865 N->Uses.push_back(this);
2869 /// SelectNodeTo - These are used for target selectors to *mutate* the
2870 /// specified node to have the specified return type, Target opcode, and
2871 /// operands. Note that target opcodes are stored as
2872 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
2874 /// Note that SelectNodeTo returns the resultant node. If there is already a
2875 /// node of the specified opcode and operands, it returns that node instead of
2876 /// the current one.
2877 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2878 MVT::ValueType VT) {
2879 SDVTList VTs = getVTList(VT);
2880 FoldingSetNodeID ID;
2881 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
2883 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2886 RemoveNodeFromCSEMaps(N);
2888 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
2890 CSEMap.InsertNode(N, IP);
2894 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2895 MVT::ValueType VT, SDOperand Op1) {
2896 // If an identical node already exists, use it.
2897 SDVTList VTs = getVTList(VT);
2898 SDOperand Ops[] = { Op1 };
2900 FoldingSetNodeID ID;
2901 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
2903 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2906 RemoveNodeFromCSEMaps(N);
2907 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
2908 CSEMap.InsertNode(N, IP);
2912 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2913 MVT::ValueType VT, SDOperand Op1,
2915 // If an identical node already exists, use it.
2916 SDVTList VTs = getVTList(VT);
2917 SDOperand Ops[] = { Op1, Op2 };
2919 FoldingSetNodeID ID;
2920 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2922 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2925 RemoveNodeFromCSEMaps(N);
2927 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2929 CSEMap.InsertNode(N, IP); // Memoize the new node.
2933 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2934 MVT::ValueType VT, SDOperand Op1,
2935 SDOperand Op2, SDOperand Op3) {
2936 // If an identical node already exists, use it.
2937 SDVTList VTs = getVTList(VT);
2938 SDOperand Ops[] = { Op1, Op2, Op3 };
2939 FoldingSetNodeID ID;
2940 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
2942 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2945 RemoveNodeFromCSEMaps(N);
2947 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
2949 CSEMap.InsertNode(N, IP); // Memoize the new node.
2953 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2954 MVT::ValueType VT, const SDOperand *Ops,
2956 // If an identical node already exists, use it.
2957 SDVTList VTs = getVTList(VT);
2958 FoldingSetNodeID ID;
2959 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
2961 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2964 RemoveNodeFromCSEMaps(N);
2965 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
2967 CSEMap.InsertNode(N, IP); // Memoize the new node.
2971 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2972 MVT::ValueType VT1, MVT::ValueType VT2,
2973 SDOperand Op1, SDOperand Op2) {
2974 SDVTList VTs = getVTList(VT1, VT2);
2975 FoldingSetNodeID ID;
2976 SDOperand Ops[] = { Op1, Op2 };
2977 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2979 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2982 RemoveNodeFromCSEMaps(N);
2983 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2984 CSEMap.InsertNode(N, IP); // Memoize the new node.
2988 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2989 MVT::ValueType VT1, MVT::ValueType VT2,
2990 SDOperand Op1, SDOperand Op2,
2992 // If an identical node already exists, use it.
2993 SDVTList VTs = getVTList(VT1, VT2);
2994 SDOperand Ops[] = { Op1, Op2, Op3 };
2995 FoldingSetNodeID ID;
2996 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
2998 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3001 RemoveNodeFromCSEMaps(N);
3003 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3004 CSEMap.InsertNode(N, IP); // Memoize the new node.
3009 /// getTargetNode - These are used for target selectors to create a new node
3010 /// with specified return type(s), target opcode, and operands.
3012 /// Note that getTargetNode returns the resultant node. If there is already a
3013 /// node of the specified opcode and operands, it returns that node instead of
3014 /// the current one.
3015 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3016 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3018 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3020 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3022 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3023 SDOperand Op1, SDOperand Op2) {
3024 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3026 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3027 SDOperand Op1, SDOperand Op2,
3029 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3031 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3032 const SDOperand *Ops, unsigned NumOps) {
3033 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
3035 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3036 MVT::ValueType VT2) {
3037 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3039 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3041 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3042 MVT::ValueType VT2, SDOperand Op1) {
3043 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3044 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
3046 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3047 MVT::ValueType VT2, SDOperand Op1,
3049 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3050 SDOperand Ops[] = { Op1, Op2 };
3051 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
3053 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3054 MVT::ValueType VT2, SDOperand Op1,
3055 SDOperand Op2, SDOperand Op3) {
3056 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3057 SDOperand Ops[] = { Op1, Op2, Op3 };
3058 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
3060 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3062 const SDOperand *Ops, unsigned NumOps) {
3063 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3064 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
3066 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3067 MVT::ValueType VT2, MVT::ValueType VT3,
3068 SDOperand Op1, SDOperand Op2) {
3069 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3070 SDOperand Ops[] = { Op1, Op2 };
3071 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
3073 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3074 MVT::ValueType VT2, MVT::ValueType VT3,
3075 SDOperand Op1, SDOperand Op2,
3077 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3078 SDOperand Ops[] = { Op1, Op2, Op3 };
3079 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3081 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3082 MVT::ValueType VT2, MVT::ValueType VT3,
3083 const SDOperand *Ops, unsigned NumOps) {
3084 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3085 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
3087 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3088 MVT::ValueType VT2, MVT::ValueType VT3,
3090 const SDOperand *Ops, unsigned NumOps) {
3091 std::vector<MVT::ValueType> VTList;
3092 VTList.push_back(VT1);
3093 VTList.push_back(VT2);
3094 VTList.push_back(VT3);
3095 VTList.push_back(VT4);
3096 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3097 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3099 SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3100 std::vector<MVT::ValueType> &ResultTys,
3101 const SDOperand *Ops, unsigned NumOps) {
3102 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3103 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3107 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3108 /// This can cause recursive merging of nodes in the DAG.
3110 /// This version assumes From/To have a single result value.
3112 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
3113 std::vector<SDNode*> *Deleted) {
3114 SDNode *From = FromN.Val, *To = ToN.Val;
3115 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
3116 "Cannot replace with this method!");
3117 assert(From != To && "Cannot replace uses of with self");
3119 while (!From->use_empty()) {
3120 // Process users until they are all gone.
3121 SDNode *U = *From->use_begin();
3123 // This node is about to morph, remove its old self from the CSE maps.
3124 RemoveNodeFromCSEMaps(U);
3126 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3128 if (I->Val == From) {
3129 From->removeUser(U);
3134 // Now that we have modified U, add it back to the CSE maps. If it already
3135 // exists there, recursively merge the results together.
3136 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3137 ReplaceAllUsesWith(U, Existing, Deleted);
3139 if (Deleted) Deleted->push_back(U);
3140 DeleteNodeNotInCSEMaps(U);
3145 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3146 /// This can cause recursive merging of nodes in the DAG.
3148 /// This version assumes From/To have matching types and numbers of result
3151 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3152 std::vector<SDNode*> *Deleted) {
3153 assert(From != To && "Cannot replace uses of with self");
3154 assert(From->getNumValues() == To->getNumValues() &&
3155 "Cannot use this version of ReplaceAllUsesWith!");
3156 if (From->getNumValues() == 1) { // If possible, use the faster version.
3157 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
3161 while (!From->use_empty()) {
3162 // Process users until they are all gone.
3163 SDNode *U = *From->use_begin();
3165 // This node is about to morph, remove its old self from the CSE maps.
3166 RemoveNodeFromCSEMaps(U);
3168 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3170 if (I->Val == From) {
3171 From->removeUser(U);
3176 // Now that we have modified U, add it back to the CSE maps. If it already
3177 // exists there, recursively merge the results together.
3178 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3179 ReplaceAllUsesWith(U, Existing, Deleted);
3181 if (Deleted) Deleted->push_back(U);
3182 DeleteNodeNotInCSEMaps(U);
3187 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3188 /// This can cause recursive merging of nodes in the DAG.
3190 /// This version can replace From with any result values. To must match the
3191 /// number and types of values returned by From.
3192 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
3193 const SDOperand *To,
3194 std::vector<SDNode*> *Deleted) {
3195 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
3196 // Degenerate case handled above.
3197 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
3201 while (!From->use_empty()) {
3202 // Process users until they are all gone.
3203 SDNode *U = *From->use_begin();
3205 // This node is about to morph, remove its old self from the CSE maps.
3206 RemoveNodeFromCSEMaps(U);
3208 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3210 if (I->Val == From) {
3211 const SDOperand &ToOp = To[I->ResNo];
3212 From->removeUser(U);
3214 ToOp.Val->addUser(U);
3217 // Now that we have modified U, add it back to the CSE maps. If it already
3218 // exists there, recursively merge the results together.
3219 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3220 ReplaceAllUsesWith(U, Existing, Deleted);
3222 if (Deleted) Deleted->push_back(U);
3223 DeleteNodeNotInCSEMaps(U);
3228 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3229 /// uses of other values produced by From.Val alone. The Deleted vector is
3230 /// handled the same was as for ReplaceAllUsesWith.
3231 void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
3232 std::vector<SDNode*> &Deleted) {
3233 assert(From != To && "Cannot replace a value with itself");
3234 // Handle the simple, trivial, case efficiently.
3235 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
3236 ReplaceAllUsesWith(From, To, &Deleted);
3240 // Get all of the users of From.Val. We want these in a nice,
3241 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3242 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
3244 while (!Users.empty()) {
3245 // We know that this user uses some value of From. If it is the right
3246 // value, update it.
3247 SDNode *User = Users.back();
3250 for (SDOperand *Op = User->OperandList,
3251 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
3253 // Okay, we know this user needs to be updated. Remove its old self
3254 // from the CSE maps.
3255 RemoveNodeFromCSEMaps(User);
3257 // Update all operands that match "From".
3258 for (; Op != E; ++Op) {
3260 From.Val->removeUser(User);
3262 To.Val->addUser(User);
3266 // Now that we have modified User, add it back to the CSE maps. If it
3267 // already exists there, recursively merge the results together.
3268 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
3269 unsigned NumDeleted = Deleted.size();
3270 ReplaceAllUsesWith(User, Existing, &Deleted);
3272 // User is now dead.
3273 Deleted.push_back(User);
3274 DeleteNodeNotInCSEMaps(User);
3276 // We have to be careful here, because ReplaceAllUsesWith could have
3277 // deleted a user of From, which means there may be dangling pointers
3278 // in the "Users" setvector. Scan over the deleted node pointers and
3279 // remove them from the setvector.
3280 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
3281 Users.remove(Deleted[i]);
3283 break; // Exit the operand scanning loop.
3290 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3291 /// their allnodes order. It returns the maximum id.
3292 unsigned SelectionDAG::AssignNodeIds() {
3294 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3301 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
3302 /// based on their topological order. It returns the maximum id and a vector
3303 /// of the SDNodes* in assigned order by reference.
3304 unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
3305 unsigned DAGSize = AllNodes.size();
3306 std::vector<unsigned> InDegree(DAGSize);
3307 std::vector<SDNode*> Sources;
3309 // Use a two pass approach to avoid using a std::map which is slow.
3311 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3314 unsigned Degree = N->use_size();
3315 InDegree[N->getNodeId()] = Degree;
3317 Sources.push_back(N);
3321 while (!Sources.empty()) {
3322 SDNode *N = Sources.back();
3324 TopOrder.push_back(N);
3325 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3327 unsigned Degree = --InDegree[P->getNodeId()];
3329 Sources.push_back(P);
3333 // Second pass, assign the actual topological order as node ids.
3335 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3337 (*TI)->setNodeId(Id++);
3344 //===----------------------------------------------------------------------===//
3346 //===----------------------------------------------------------------------===//
3348 // Out-of-line virtual method to give class a home.
3349 void SDNode::ANCHOR() {}
3350 void UnarySDNode::ANCHOR() {}
3351 void BinarySDNode::ANCHOR() {}
3352 void TernarySDNode::ANCHOR() {}
3353 void HandleSDNode::ANCHOR() {}
3354 void StringSDNode::ANCHOR() {}
3355 void ConstantSDNode::ANCHOR() {}
3356 void ConstantFPSDNode::ANCHOR() {}
3357 void GlobalAddressSDNode::ANCHOR() {}
3358 void FrameIndexSDNode::ANCHOR() {}
3359 void JumpTableSDNode::ANCHOR() {}
3360 void ConstantPoolSDNode::ANCHOR() {}
3361 void BasicBlockSDNode::ANCHOR() {}
3362 void SrcValueSDNode::ANCHOR() {}
3363 void RegisterSDNode::ANCHOR() {}
3364 void ExternalSymbolSDNode::ANCHOR() {}
3365 void CondCodeSDNode::ANCHOR() {}
3366 void VTSDNode::ANCHOR() {}
3367 void LoadSDNode::ANCHOR() {}
3368 void StoreSDNode::ANCHOR() {}
3370 HandleSDNode::~HandleSDNode() {
3371 SDVTList VTs = { 0, 0 };
3372 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
3375 GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3376 MVT::ValueType VT, int o)
3377 : SDNode(isa<GlobalVariable>(GA) &&
3378 cast<GlobalVariable>(GA)->isThreadLocal() ?
3380 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3382 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3383 getSDVTList(VT)), Offset(o) {
3384 TheGlobal = const_cast<GlobalValue*>(GA);
3387 /// Profile - Gather unique data for the node.
3389 void SDNode::Profile(FoldingSetNodeID &ID) {
3390 AddNodeIDNode(ID, this);
3393 /// getValueTypeList - Return a pointer to the specified value type.
3395 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
3396 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3401 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3402 /// indicated value. This method ignores uses of other values defined by this
3404 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
3405 assert(Value < getNumValues() && "Bad value!");
3407 // If there is only one value, this is easy.
3408 if (getNumValues() == 1)
3409 return use_size() == NUses;
3410 if (use_size() < NUses) return false;
3412 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3414 SmallPtrSet<SDNode*, 32> UsersHandled;
3416 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3418 if (User->getNumOperands() == 1 ||
3419 UsersHandled.insert(User)) // First time we've seen this?
3420 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3421 if (User->getOperand(i) == TheValue) {
3423 return false; // too many uses
3428 // Found exactly the right number of uses?
3433 /// hasAnyUseOfValue - Return true if there are any use of the indicated
3434 /// value. This method ignores uses of other values defined by this operation.
3435 bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3436 assert(Value < getNumValues() && "Bad value!");
3438 if (use_size() == 0) return false;
3440 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3442 SmallPtrSet<SDNode*, 32> UsersHandled;
3444 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3446 if (User->getNumOperands() == 1 ||
3447 UsersHandled.insert(User)) // First time we've seen this?
3448 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3449 if (User->getOperand(i) == TheValue) {
3458 /// isOnlyUse - Return true if this node is the only use of N.
3460 bool SDNode::isOnlyUse(SDNode *N) const {
3462 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3473 /// isOperand - Return true if this node is an operand of N.
3475 bool SDOperand::isOperand(SDNode *N) const {
3476 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3477 if (*this == N->getOperand(i))
3482 bool SDNode::isOperand(SDNode *N) const {
3483 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3484 if (this == N->OperandList[i].Val)
3489 static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
3490 SmallPtrSet<SDNode *, 32> &Visited) {
3491 if (found || !Visited.insert(N))
3494 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3495 SDNode *Op = N->getOperand(i).Val;
3500 findPredecessor(Op, P, found, Visited);
3504 /// isPredecessor - Return true if this node is a predecessor of N. This node
3505 /// is either an operand of N or it can be reached by recursively traversing
3506 /// up the operands.
3507 /// NOTE: this is an expensive method. Use it carefully.
3508 bool SDNode::isPredecessor(SDNode *N) const {
3509 SmallPtrSet<SDNode *, 32> Visited;
3511 findPredecessor(N, this, found, Visited);
3515 uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3516 assert(Num < NumOperands && "Invalid child # of SDNode!");
3517 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3520 std::string SDNode::getOperationName(const SelectionDAG *G) const {
3521 switch (getOpcode()) {
3523 if (getOpcode() < ISD::BUILTIN_OP_END)
3524 return "<<Unknown DAG Node>>";
3527 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
3528 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
3529 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
3531 TargetLowering &TLI = G->getTargetLoweringInfo();
3533 TLI.getTargetNodeName(getOpcode());
3534 if (Name) return Name;
3537 return "<<Unknown Target Node>>";
3540 case ISD::PCMARKER: return "PCMarker";
3541 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
3542 case ISD::SRCVALUE: return "SrcValue";
3543 case ISD::EntryToken: return "EntryToken";
3544 case ISD::TokenFactor: return "TokenFactor";
3545 case ISD::AssertSext: return "AssertSext";
3546 case ISD::AssertZext: return "AssertZext";
3548 case ISD::STRING: return "String";
3549 case ISD::BasicBlock: return "BasicBlock";
3550 case ISD::VALUETYPE: return "ValueType";
3551 case ISD::Register: return "Register";
3553 case ISD::Constant: return "Constant";
3554 case ISD::ConstantFP: return "ConstantFP";
3555 case ISD::GlobalAddress: return "GlobalAddress";
3556 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
3557 case ISD::FrameIndex: return "FrameIndex";
3558 case ISD::JumpTable: return "JumpTable";
3559 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
3560 case ISD::RETURNADDR: return "RETURNADDR";
3561 case ISD::FRAMEADDR: return "FRAMEADDR";
3562 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
3563 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3564 case ISD::EHSELECTION: return "EHSELECTION";
3565 case ISD::EH_RETURN: return "EH_RETURN";
3566 case ISD::ConstantPool: return "ConstantPool";
3567 case ISD::ExternalSymbol: return "ExternalSymbol";
3568 case ISD::INTRINSIC_WO_CHAIN: {
3569 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3570 return Intrinsic::getName((Intrinsic::ID)IID);
3572 case ISD::INTRINSIC_VOID:
3573 case ISD::INTRINSIC_W_CHAIN: {
3574 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
3575 return Intrinsic::getName((Intrinsic::ID)IID);
3578 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
3579 case ISD::TargetConstant: return "TargetConstant";
3580 case ISD::TargetConstantFP:return "TargetConstantFP";
3581 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
3582 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
3583 case ISD::TargetFrameIndex: return "TargetFrameIndex";
3584 case ISD::TargetJumpTable: return "TargetJumpTable";
3585 case ISD::TargetConstantPool: return "TargetConstantPool";
3586 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
3588 case ISD::CopyToReg: return "CopyToReg";
3589 case ISD::CopyFromReg: return "CopyFromReg";
3590 case ISD::UNDEF: return "undef";
3591 case ISD::MERGE_VALUES: return "merge_values";
3592 case ISD::INLINEASM: return "inlineasm";
3593 case ISD::LABEL: return "label";
3594 case ISD::HANDLENODE: return "handlenode";
3595 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
3596 case ISD::CALL: return "call";
3599 case ISD::FABS: return "fabs";
3600 case ISD::FNEG: return "fneg";
3601 case ISD::FSQRT: return "fsqrt";
3602 case ISD::FSIN: return "fsin";
3603 case ISD::FCOS: return "fcos";
3604 case ISD::FPOWI: return "fpowi";
3605 case ISD::FPOW: return "fpow";
3608 case ISD::ADD: return "add";
3609 case ISD::SUB: return "sub";
3610 case ISD::MUL: return "mul";
3611 case ISD::MULHU: return "mulhu";
3612 case ISD::MULHS: return "mulhs";
3613 case ISD::SDIV: return "sdiv";
3614 case ISD::UDIV: return "udiv";
3615 case ISD::SREM: return "srem";
3616 case ISD::UREM: return "urem";
3617 case ISD::SMUL_LOHI: return "smul_lohi";
3618 case ISD::UMUL_LOHI: return "umul_lohi";
3619 case ISD::SDIVREM: return "sdivrem";
3620 case ISD::UDIVREM: return "divrem";
3621 case ISD::AND: return "and";
3622 case ISD::OR: return "or";
3623 case ISD::XOR: return "xor";
3624 case ISD::SHL: return "shl";
3625 case ISD::SRA: return "sra";
3626 case ISD::SRL: return "srl";
3627 case ISD::ROTL: return "rotl";
3628 case ISD::ROTR: return "rotr";
3629 case ISD::FADD: return "fadd";
3630 case ISD::FSUB: return "fsub";
3631 case ISD::FMUL: return "fmul";
3632 case ISD::FDIV: return "fdiv";
3633 case ISD::FREM: return "frem";
3634 case ISD::FCOPYSIGN: return "fcopysign";
3636 case ISD::SETCC: return "setcc";
3637 case ISD::SELECT: return "select";
3638 case ISD::SELECT_CC: return "select_cc";
3639 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
3640 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
3641 case ISD::CONCAT_VECTORS: return "concat_vectors";
3642 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
3643 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
3644 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
3645 case ISD::CARRY_FALSE: return "carry_false";
3646 case ISD::ADDC: return "addc";
3647 case ISD::ADDE: return "adde";
3648 case ISD::SUBC: return "subc";
3649 case ISD::SUBE: return "sube";
3650 case ISD::SHL_PARTS: return "shl_parts";
3651 case ISD::SRA_PARTS: return "sra_parts";
3652 case ISD::SRL_PARTS: return "srl_parts";
3654 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3655 case ISD::INSERT_SUBREG: return "insert_subreg";
3657 // Conversion operators.
3658 case ISD::SIGN_EXTEND: return "sign_extend";
3659 case ISD::ZERO_EXTEND: return "zero_extend";
3660 case ISD::ANY_EXTEND: return "any_extend";
3661 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
3662 case ISD::TRUNCATE: return "truncate";
3663 case ISD::FP_ROUND: return "fp_round";
3664 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
3665 case ISD::FP_EXTEND: return "fp_extend";
3667 case ISD::SINT_TO_FP: return "sint_to_fp";
3668 case ISD::UINT_TO_FP: return "uint_to_fp";
3669 case ISD::FP_TO_SINT: return "fp_to_sint";
3670 case ISD::FP_TO_UINT: return "fp_to_uint";
3671 case ISD::BIT_CONVERT: return "bit_convert";
3673 // Control flow instructions
3674 case ISD::BR: return "br";
3675 case ISD::BRIND: return "brind";
3676 case ISD::BR_JT: return "br_jt";
3677 case ISD::BRCOND: return "brcond";
3678 case ISD::BR_CC: return "br_cc";
3679 case ISD::RET: return "ret";
3680 case ISD::CALLSEQ_START: return "callseq_start";
3681 case ISD::CALLSEQ_END: return "callseq_end";
3684 case ISD::LOAD: return "load";
3685 case ISD::STORE: return "store";
3686 case ISD::VAARG: return "vaarg";
3687 case ISD::VACOPY: return "vacopy";
3688 case ISD::VAEND: return "vaend";
3689 case ISD::VASTART: return "vastart";
3690 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
3691 case ISD::EXTRACT_ELEMENT: return "extract_element";
3692 case ISD::BUILD_PAIR: return "build_pair";
3693 case ISD::STACKSAVE: return "stacksave";
3694 case ISD::STACKRESTORE: return "stackrestore";
3696 // Block memory operations.
3697 case ISD::MEMSET: return "memset";
3698 case ISD::MEMCPY: return "memcpy";
3699 case ISD::MEMMOVE: return "memmove";
3702 case ISD::BSWAP: return "bswap";
3703 case ISD::CTPOP: return "ctpop";
3704 case ISD::CTTZ: return "cttz";
3705 case ISD::CTLZ: return "ctlz";
3708 case ISD::LOCATION: return "location";
3709 case ISD::DEBUG_LOC: return "debug_loc";
3712 case ISD::TRAMPOLINE: return "trampoline";
3715 switch (cast<CondCodeSDNode>(this)->get()) {
3716 default: assert(0 && "Unknown setcc condition!");
3717 case ISD::SETOEQ: return "setoeq";
3718 case ISD::SETOGT: return "setogt";
3719 case ISD::SETOGE: return "setoge";
3720 case ISD::SETOLT: return "setolt";
3721 case ISD::SETOLE: return "setole";
3722 case ISD::SETONE: return "setone";
3724 case ISD::SETO: return "seto";
3725 case ISD::SETUO: return "setuo";
3726 case ISD::SETUEQ: return "setue";
3727 case ISD::SETUGT: return "setugt";
3728 case ISD::SETUGE: return "setuge";
3729 case ISD::SETULT: return "setult";
3730 case ISD::SETULE: return "setule";
3731 case ISD::SETUNE: return "setune";
3733 case ISD::SETEQ: return "seteq";
3734 case ISD::SETGT: return "setgt";
3735 case ISD::SETGE: return "setge";
3736 case ISD::SETLT: return "setlt";
3737 case ISD::SETLE: return "setle";
3738 case ISD::SETNE: return "setne";
3743 const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
3752 return "<post-inc>";
3754 return "<post-dec>";
3758 void SDNode::dump() const { dump(0); }
3759 void SDNode::dump(const SelectionDAG *G) const {
3760 cerr << (void*)this << ": ";
3762 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
3764 if (getValueType(i) == MVT::Other)
3767 cerr << MVT::getValueTypeString(getValueType(i));
3769 cerr << " = " << getOperationName(G);
3772 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
3773 if (i) cerr << ", ";
3774 cerr << (void*)getOperand(i).Val;
3775 if (unsigned RN = getOperand(i).ResNo)
3779 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
3780 cerr << "<" << CSDN->getValue() << ">";
3781 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
3782 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
3783 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
3784 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
3785 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
3787 cerr << "<APFloat(";
3788 CSDN->getValueAPF().convertToAPInt().dump();
3791 } else if (const GlobalAddressSDNode *GADN =
3792 dyn_cast<GlobalAddressSDNode>(this)) {
3793 int offset = GADN->getOffset();
3795 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
3797 cerr << " + " << offset;
3799 cerr << " " << offset;
3800 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
3801 cerr << "<" << FIDN->getIndex() << ">";
3802 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
3803 cerr << "<" << JTDN->getIndex() << ">";
3804 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
3805 int offset = CP->getOffset();
3806 if (CP->isMachineConstantPoolEntry())
3807 cerr << "<" << *CP->getMachineCPVal() << ">";
3809 cerr << "<" << *CP->getConstVal() << ">";
3811 cerr << " + " << offset;
3813 cerr << " " << offset;
3814 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
3816 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3818 cerr << LBB->getName() << " ";
3819 cerr << (const void*)BBDN->getBasicBlock() << ">";
3820 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
3821 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
3822 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
3824 cerr << " #" << R->getReg();
3826 } else if (const ExternalSymbolSDNode *ES =
3827 dyn_cast<ExternalSymbolSDNode>(this)) {
3828 cerr << "'" << ES->getSymbol() << "'";
3829 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3831 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
3833 cerr << "<null:" << M->getOffset() << ">";
3834 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
3835 cerr << ":" << MVT::getValueTypeString(N->getVT());
3836 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
3838 switch (LD->getExtensionType()) {
3839 default: doExt = false; break;
3841 cerr << " <anyext ";
3851 cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
3853 const char *AM = getIndexedModeName(LD->getAddressingMode());
3856 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
3857 if (ST->isTruncatingStore())
3859 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
3861 const char *AM = getIndexedModeName(ST->getAddressingMode());
3867 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
3868 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3869 if (N->getOperand(i).Val->hasOneUse())
3870 DumpNodes(N->getOperand(i).Val, indent+2, G);
3872 cerr << "\n" << std::string(indent+2, ' ')
3873 << (void*)N->getOperand(i).Val << ": <multiple use>";
3876 cerr << "\n" << std::string(indent, ' ');
3880 void SelectionDAG::dump() const {
3881 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
3882 std::vector<const SDNode*> Nodes;
3883 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3887 std::sort(Nodes.begin(), Nodes.end());
3889 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
3890 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
3891 DumpNodes(Nodes[i], 2, this);
3894 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
3899 const Type *ConstantPoolSDNode::getType() const {
3900 if (isMachineConstantPoolEntry())
3901 return Val.MachineCPVal->getType();
3902 return Val.ConstVal->getType();