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/CodeGen/MachineFrameInfo.h"
23 #include "llvm/Support/MathExtras.h"
24 #include "llvm/Target/MRegisterInfo.h"
25 #include "llvm/Target/TargetData.h"
26 #include "llvm/Target/TargetLowering.h"
27 #include "llvm/Target/TargetInstrInfo.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/ADT/SetVector.h"
30 #include "llvm/ADT/SmallPtrSet.h"
31 #include "llvm/ADT/SmallSet.h"
32 #include "llvm/ADT/SmallVector.h"
33 #include "llvm/ADT/StringExtras.h"
38 /// makeVTList - Return an instance of the SDVTList struct initialized with the
39 /// specified members.
40 static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
41 SDVTList Res = {VTs, NumVTs};
45 //===----------------------------------------------------------------------===//
46 // ConstantFPSDNode Class
47 //===----------------------------------------------------------------------===//
49 /// isExactlyValue - We don't rely on operator== working on double values, as
50 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
51 /// As such, this method can be used to do an exact bit-for-bit comparison of
52 /// two floating point values.
53 bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
54 return Value.bitwiseIsEqual(V);
57 bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
59 // convert modifies in place, so make a copy.
60 APFloat Val2 = APFloat(Val);
63 return false; // These can't be represented as floating point!
65 // FIXME rounding mode needs to be more flexible
67 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
68 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) ==
71 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
72 &Val2.getSemantics() == &APFloat::IEEEdouble ||
73 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
75 // TODO: Figure out how to test if we can use a shorter type instead!
83 //===----------------------------------------------------------------------===//
85 //===----------------------------------------------------------------------===//
87 /// isBuildVectorAllOnes - Return true if the specified node is a
88 /// BUILD_VECTOR where all of the elements are ~0 or undef.
89 bool ISD::isBuildVectorAllOnes(const SDNode *N) {
90 // Look through a bit convert.
91 if (N->getOpcode() == ISD::BIT_CONVERT)
92 N = N->getOperand(0).Val;
94 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
96 unsigned i = 0, e = N->getNumOperands();
98 // Skip over all of the undef values.
99 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
102 // Do not accept an all-undef vector.
103 if (i == e) return false;
105 // Do not accept build_vectors that aren't all constants or which have non-~0
107 SDOperand NotZero = N->getOperand(i);
108 if (isa<ConstantSDNode>(NotZero)) {
109 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
111 } else if (isa<ConstantFPSDNode>(NotZero)) {
112 MVT::ValueType VT = NotZero.getValueType();
114 if (((cast<ConstantFPSDNode>(NotZero)->getValueAPF().
115 convertToAPInt().getZExtValue())) != (uint64_t)-1)
118 if ((uint32_t)cast<ConstantFPSDNode>(NotZero)->
119 getValueAPF().convertToAPInt().getZExtValue() !=
126 // Okay, we have at least one ~0 value, check to see if the rest match or are
128 for (++i; i != e; ++i)
129 if (N->getOperand(i) != NotZero &&
130 N->getOperand(i).getOpcode() != ISD::UNDEF)
136 /// isBuildVectorAllZeros - Return true if the specified node is a
137 /// BUILD_VECTOR where all of the elements are 0 or undef.
138 bool ISD::isBuildVectorAllZeros(const SDNode *N) {
139 // Look through a bit convert.
140 if (N->getOpcode() == ISD::BIT_CONVERT)
141 N = N->getOperand(0).Val;
143 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
145 unsigned i = 0, e = N->getNumOperands();
147 // Skip over all of the undef values.
148 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
151 // Do not accept an all-undef vector.
152 if (i == e) return false;
154 // Do not accept build_vectors that aren't all constants or which have non-~0
156 SDOperand Zero = N->getOperand(i);
157 if (isa<ConstantSDNode>(Zero)) {
158 if (!cast<ConstantSDNode>(Zero)->isNullValue())
160 } else if (isa<ConstantFPSDNode>(Zero)) {
161 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
166 // Okay, we have at least one ~0 value, check to see if the rest match or are
168 for (++i; i != e; ++i)
169 if (N->getOperand(i) != Zero &&
170 N->getOperand(i).getOpcode() != ISD::UNDEF)
175 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
176 /// when given the operation for (X op Y).
177 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
178 // To perform this operation, we just need to swap the L and G bits of the
180 unsigned OldL = (Operation >> 2) & 1;
181 unsigned OldG = (Operation >> 1) & 1;
182 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
183 (OldL << 1) | // New G bit
184 (OldG << 2)); // New L bit.
187 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
188 /// 'op' is a valid SetCC operation.
189 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
190 unsigned Operation = Op;
192 Operation ^= 7; // Flip L, G, E bits, but not U.
194 Operation ^= 15; // Flip all of the condition bits.
195 if (Operation > ISD::SETTRUE2)
196 Operation &= ~8; // Don't let N and U bits get set.
197 return ISD::CondCode(Operation);
201 /// isSignedOp - For an integer comparison, return 1 if the comparison is a
202 /// signed operation and 2 if the result is an unsigned comparison. Return zero
203 /// if the operation does not depend on the sign of the input (setne and seteq).
204 static int isSignedOp(ISD::CondCode Opcode) {
206 default: assert(0 && "Illegal integer setcc operation!");
208 case ISD::SETNE: return 0;
212 case ISD::SETGE: return 1;
216 case ISD::SETUGE: return 2;
220 /// getSetCCOrOperation - Return the result of a logical OR between different
221 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
222 /// returns SETCC_INVALID if it is not possible to represent the resultant
224 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
226 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
227 // Cannot fold a signed integer setcc with an unsigned integer setcc.
228 return ISD::SETCC_INVALID;
230 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
232 // If the N and U bits get set then the resultant comparison DOES suddenly
233 // care about orderedness, and is true when ordered.
234 if (Op > ISD::SETTRUE2)
235 Op &= ~16; // Clear the U bit if the N bit is set.
237 // Canonicalize illegal integer setcc's.
238 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
241 return ISD::CondCode(Op);
244 /// getSetCCAndOperation - Return the result of a logical AND between different
245 /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
246 /// function returns zero if it is not possible to represent the resultant
248 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
250 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
251 // Cannot fold a signed setcc with an unsigned setcc.
252 return ISD::SETCC_INVALID;
254 // Combine all of the condition bits.
255 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
257 // Canonicalize illegal integer setcc's.
261 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
262 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
263 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
264 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
271 const TargetMachine &SelectionDAG::getTarget() const {
272 return TLI.getTargetMachine();
275 //===----------------------------------------------------------------------===//
276 // SDNode Profile Support
277 //===----------------------------------------------------------------------===//
279 /// AddNodeIDOpcode - Add the node opcode to the NodeID data.
281 static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
285 /// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
286 /// solely with their pointer.
287 void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
288 ID.AddPointer(VTList.VTs);
291 /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
293 static void AddNodeIDOperands(FoldingSetNodeID &ID,
294 const SDOperand *Ops, unsigned NumOps) {
295 for (; NumOps; --NumOps, ++Ops) {
296 ID.AddPointer(Ops->Val);
297 ID.AddInteger(Ops->ResNo);
301 static void AddNodeIDNode(FoldingSetNodeID &ID,
302 unsigned short OpC, SDVTList VTList,
303 const SDOperand *OpList, unsigned N) {
304 AddNodeIDOpcode(ID, OpC);
305 AddNodeIDValueTypes(ID, VTList);
306 AddNodeIDOperands(ID, OpList, N);
309 /// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
311 static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
312 AddNodeIDOpcode(ID, N->getOpcode());
313 // Add the return value info.
314 AddNodeIDValueTypes(ID, N->getVTList());
315 // Add the operand info.
316 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
318 // Handle SDNode leafs with special info.
319 switch (N->getOpcode()) {
320 default: break; // Normal nodes don't need extra info.
321 case ISD::TargetConstant:
323 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
325 case ISD::TargetConstantFP:
326 case ISD::ConstantFP: {
327 ID.AddAPFloat(cast<ConstantFPSDNode>(N)->getValueAPF());
330 case ISD::TargetGlobalAddress:
331 case ISD::GlobalAddress:
332 case ISD::TargetGlobalTLSAddress:
333 case ISD::GlobalTLSAddress: {
334 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
335 ID.AddPointer(GA->getGlobal());
336 ID.AddInteger(GA->getOffset());
339 case ISD::BasicBlock:
340 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
343 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
345 case ISD::SRCVALUE: {
346 SrcValueSDNode *SV = cast<SrcValueSDNode>(N);
347 ID.AddPointer(SV->getValue());
348 ID.AddInteger(SV->getOffset());
351 case ISD::FrameIndex:
352 case ISD::TargetFrameIndex:
353 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
356 case ISD::TargetJumpTable:
357 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
359 case ISD::ConstantPool:
360 case ISD::TargetConstantPool: {
361 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
362 ID.AddInteger(CP->getAlignment());
363 ID.AddInteger(CP->getOffset());
364 if (CP->isMachineConstantPoolEntry())
365 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
367 ID.AddPointer(CP->getConstVal());
371 LoadSDNode *LD = cast<LoadSDNode>(N);
372 ID.AddInteger(LD->getAddressingMode());
373 ID.AddInteger(LD->getExtensionType());
374 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
375 ID.AddPointer(LD->getSrcValue());
376 ID.AddInteger(LD->getSrcValueOffset());
377 ID.AddInteger(LD->getAlignment());
378 ID.AddInteger(LD->isVolatile());
382 StoreSDNode *ST = cast<StoreSDNode>(N);
383 ID.AddInteger(ST->getAddressingMode());
384 ID.AddInteger(ST->isTruncatingStore());
385 ID.AddInteger((unsigned int)(ST->getStoredVT()));
386 ID.AddPointer(ST->getSrcValue());
387 ID.AddInteger(ST->getSrcValueOffset());
388 ID.AddInteger(ST->getAlignment());
389 ID.AddInteger(ST->isVolatile());
395 //===----------------------------------------------------------------------===//
396 // SelectionDAG Class
397 //===----------------------------------------------------------------------===//
399 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
401 void SelectionDAG::RemoveDeadNodes() {
402 // Create a dummy node (which is not added to allnodes), that adds a reference
403 // to the root node, preventing it from being deleted.
404 HandleSDNode Dummy(getRoot());
406 SmallVector<SDNode*, 128> DeadNodes;
408 // Add all obviously-dead nodes to the DeadNodes worklist.
409 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
411 DeadNodes.push_back(I);
413 // Process the worklist, deleting the nodes and adding their uses to the
415 while (!DeadNodes.empty()) {
416 SDNode *N = DeadNodes.back();
417 DeadNodes.pop_back();
419 // Take the node out of the appropriate CSE map.
420 RemoveNodeFromCSEMaps(N);
422 // Next, brutally remove the operand list. This is safe to do, as there are
423 // no cycles in the graph.
424 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
425 SDNode *Operand = I->Val;
426 Operand->removeUser(N);
428 // Now that we removed this operand, see if there are no uses of it left.
429 if (Operand->use_empty())
430 DeadNodes.push_back(Operand);
432 if (N->OperandsNeedDelete)
433 delete[] N->OperandList;
437 // Finally, remove N itself.
441 // If the root changed (e.g. it was a dead load, update the root).
442 setRoot(Dummy.getValue());
445 void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
446 SmallVector<SDNode*, 16> DeadNodes;
447 DeadNodes.push_back(N);
449 // Process the worklist, deleting the nodes and adding their uses to the
451 while (!DeadNodes.empty()) {
452 SDNode *N = DeadNodes.back();
453 DeadNodes.pop_back();
455 // Take the node out of the appropriate CSE map.
456 RemoveNodeFromCSEMaps(N);
458 // Next, brutally remove the operand list. This is safe to do, as there are
459 // no cycles in the graph.
460 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
461 SDNode *Operand = I->Val;
462 Operand->removeUser(N);
464 // Now that we removed this operand, see if there are no uses of it left.
465 if (Operand->use_empty())
466 DeadNodes.push_back(Operand);
468 if (N->OperandsNeedDelete)
469 delete[] N->OperandList;
473 // Finally, remove N itself.
474 Deleted.push_back(N);
479 void SelectionDAG::DeleteNode(SDNode *N) {
480 assert(N->use_empty() && "Cannot delete a node that is not dead!");
482 // First take this out of the appropriate CSE map.
483 RemoveNodeFromCSEMaps(N);
485 // Finally, remove uses due to operands of this node, remove from the
486 // AllNodes list, and delete the node.
487 DeleteNodeNotInCSEMaps(N);
490 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
492 // Remove it from the AllNodes list.
495 // Drop all of the operands and decrement used nodes use counts.
496 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
497 I->Val->removeUser(N);
498 if (N->OperandsNeedDelete)
499 delete[] N->OperandList;
506 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
507 /// correspond to it. This is useful when we're about to delete or repurpose
508 /// the node. We don't want future request for structurally identical nodes
509 /// to return N anymore.
510 void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
512 switch (N->getOpcode()) {
513 case ISD::HANDLENODE: return; // noop.
515 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
518 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
519 "Cond code doesn't exist!");
520 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
521 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
523 case ISD::ExternalSymbol:
524 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
526 case ISD::TargetExternalSymbol:
528 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
530 case ISD::VALUETYPE: {
531 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
532 if (MVT::isExtendedVT(VT)) {
533 Erased = ExtendedValueTypeNodes.erase(VT);
535 Erased = ValueTypeNodes[VT] != 0;
536 ValueTypeNodes[VT] = 0;
541 // Remove it from the CSE Map.
542 Erased = CSEMap.RemoveNode(N);
546 // Verify that the node was actually in one of the CSE maps, unless it has a
547 // flag result (which cannot be CSE'd) or is one of the special cases that are
548 // not subject to CSE.
549 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
550 !N->isTargetOpcode()) {
553 assert(0 && "Node is not in map!");
558 /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
559 /// has been taken out and modified in some way. If the specified node already
560 /// exists in the CSE maps, do not modify the maps, but return the existing node
561 /// instead. If it doesn't exist, add it and return null.
563 SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
564 assert(N->getNumOperands() && "This is a leaf node!");
565 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
566 return 0; // Never add these nodes.
568 // Check that remaining values produced are not flags.
569 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
570 if (N->getValueType(i) == MVT::Flag)
571 return 0; // Never CSE anything that produces a flag.
573 SDNode *New = CSEMap.GetOrInsertNode(N);
574 if (New != N) return New; // Node already existed.
578 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
579 /// were replaced with those specified. If this node is never memoized,
580 /// return null, otherwise return a pointer to the slot it would take. If a
581 /// node already exists with these operands, the slot will be non-null.
582 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
584 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
585 return 0; // Never add these nodes.
587 // Check that remaining values produced are not flags.
588 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
589 if (N->getValueType(i) == MVT::Flag)
590 return 0; // Never CSE anything that produces a flag.
592 SDOperand Ops[] = { Op };
594 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
595 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
598 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
599 /// were replaced with those specified. If this node is never memoized,
600 /// return null, otherwise return a pointer to the slot it would take. If a
601 /// node already exists with these operands, the slot will be non-null.
602 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
603 SDOperand Op1, SDOperand Op2,
605 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
606 return 0; // Never add these nodes.
608 // Check that remaining values produced are not flags.
609 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
610 if (N->getValueType(i) == MVT::Flag)
611 return 0; // Never CSE anything that produces a flag.
613 SDOperand Ops[] = { Op1, Op2 };
615 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
616 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
620 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
621 /// were replaced with those specified. If this node is never memoized,
622 /// return null, otherwise return a pointer to the slot it would take. If a
623 /// node already exists with these operands, the slot will be non-null.
624 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
625 const SDOperand *Ops,unsigned NumOps,
627 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
628 return 0; // Never add these nodes.
630 // Check that remaining values produced are not flags.
631 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
632 if (N->getValueType(i) == MVT::Flag)
633 return 0; // Never CSE anything that produces a flag.
636 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
638 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
639 ID.AddInteger(LD->getAddressingMode());
640 ID.AddInteger(LD->getExtensionType());
641 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
642 ID.AddPointer(LD->getSrcValue());
643 ID.AddInteger(LD->getSrcValueOffset());
644 ID.AddInteger(LD->getAlignment());
645 ID.AddInteger(LD->isVolatile());
646 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
647 ID.AddInteger(ST->getAddressingMode());
648 ID.AddInteger(ST->isTruncatingStore());
649 ID.AddInteger((unsigned int)(ST->getStoredVT()));
650 ID.AddPointer(ST->getSrcValue());
651 ID.AddInteger(ST->getSrcValueOffset());
652 ID.AddInteger(ST->getAlignment());
653 ID.AddInteger(ST->isVolatile());
656 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
660 SelectionDAG::~SelectionDAG() {
661 while (!AllNodes.empty()) {
662 SDNode *N = AllNodes.begin();
663 N->SetNextInBucket(0);
664 if (N->OperandsNeedDelete)
665 delete [] N->OperandList;
668 AllNodes.pop_front();
672 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
673 if (Op.getValueType() == VT) return Op;
674 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
675 return getNode(ISD::AND, Op.getValueType(), Op,
676 getConstant(Imm, Op.getValueType()));
679 SDOperand SelectionDAG::getString(const std::string &Val) {
680 StringSDNode *&N = StringNodes[Val];
682 N = new StringSDNode(Val);
683 AllNodes.push_back(N);
685 return SDOperand(N, 0);
688 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
689 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
691 MVT::ValueType EltVT =
692 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
694 // Mask out any bits that are not valid for this constant.
695 Val &= MVT::getIntVTBitMask(EltVT);
697 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
699 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
703 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
704 if (!MVT::isVector(VT))
705 return SDOperand(N, 0);
707 N = new ConstantSDNode(isT, Val, EltVT);
708 CSEMap.InsertNode(N, IP);
709 AllNodes.push_back(N);
712 SDOperand Result(N, 0);
713 if (MVT::isVector(VT)) {
714 SmallVector<SDOperand, 8> Ops;
715 Ops.assign(MVT::getVectorNumElements(VT), Result);
716 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
721 SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
723 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
725 MVT::ValueType EltVT =
726 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
728 // Do the map lookup using the actual bit pattern for the floating point
729 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
730 // we don't have issues with SNANs.
731 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
733 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
737 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
738 if (!MVT::isVector(VT))
739 return SDOperand(N, 0);
741 N = new ConstantFPSDNode(isTarget, V, EltVT);
742 CSEMap.InsertNode(N, IP);
743 AllNodes.push_back(N);
746 SDOperand Result(N, 0);
747 if (MVT::isVector(VT)) {
748 SmallVector<SDOperand, 8> Ops;
749 Ops.assign(MVT::getVectorNumElements(VT), Result);
750 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
755 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
757 MVT::ValueType EltVT =
758 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
760 return getConstantFP(APFloat((float)Val), VT, isTarget);
762 return getConstantFP(APFloat(Val), VT, isTarget);
765 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
766 MVT::ValueType VT, int Offset,
768 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
770 if (GVar && GVar->isThreadLocal())
771 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
773 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
775 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
777 ID.AddInteger(Offset);
779 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
780 return SDOperand(E, 0);
781 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
782 CSEMap.InsertNode(N, IP);
783 AllNodes.push_back(N);
784 return SDOperand(N, 0);
787 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
789 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
791 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
794 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
795 return SDOperand(E, 0);
796 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
797 CSEMap.InsertNode(N, IP);
798 AllNodes.push_back(N);
799 return SDOperand(N, 0);
802 SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
803 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
805 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
808 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
809 return SDOperand(E, 0);
810 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
811 CSEMap.InsertNode(N, IP);
812 AllNodes.push_back(N);
813 return SDOperand(N, 0);
816 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
817 unsigned Alignment, int Offset,
819 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
821 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
822 ID.AddInteger(Alignment);
823 ID.AddInteger(Offset);
826 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
827 return SDOperand(E, 0);
828 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
829 CSEMap.InsertNode(N, IP);
830 AllNodes.push_back(N);
831 return SDOperand(N, 0);
835 SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
837 unsigned Alignment, int Offset,
839 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
841 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
842 ID.AddInteger(Alignment);
843 ID.AddInteger(Offset);
844 C->AddSelectionDAGCSEId(ID);
846 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
847 return SDOperand(E, 0);
848 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
849 CSEMap.InsertNode(N, IP);
850 AllNodes.push_back(N);
851 return SDOperand(N, 0);
855 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
857 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
860 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
861 return SDOperand(E, 0);
862 SDNode *N = new BasicBlockSDNode(MBB);
863 CSEMap.InsertNode(N, IP);
864 AllNodes.push_back(N);
865 return SDOperand(N, 0);
868 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
869 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
870 ValueTypeNodes.resize(VT+1);
872 SDNode *&N = MVT::isExtendedVT(VT) ?
873 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
875 if (N) return SDOperand(N, 0);
876 N = new VTSDNode(VT);
877 AllNodes.push_back(N);
878 return SDOperand(N, 0);
881 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
882 SDNode *&N = ExternalSymbols[Sym];
883 if (N) return SDOperand(N, 0);
884 N = new ExternalSymbolSDNode(false, Sym, VT);
885 AllNodes.push_back(N);
886 return SDOperand(N, 0);
889 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
891 SDNode *&N = TargetExternalSymbols[Sym];
892 if (N) return SDOperand(N, 0);
893 N = new ExternalSymbolSDNode(true, Sym, VT);
894 AllNodes.push_back(N);
895 return SDOperand(N, 0);
898 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
899 if ((unsigned)Cond >= CondCodeNodes.size())
900 CondCodeNodes.resize(Cond+1);
902 if (CondCodeNodes[Cond] == 0) {
903 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
904 AllNodes.push_back(CondCodeNodes[Cond]);
906 return SDOperand(CondCodeNodes[Cond], 0);
909 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
911 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
912 ID.AddInteger(RegNo);
914 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
915 return SDOperand(E, 0);
916 SDNode *N = new RegisterSDNode(RegNo, VT);
917 CSEMap.InsertNode(N, IP);
918 AllNodes.push_back(N);
919 return SDOperand(N, 0);
922 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
923 assert((!V || isa<PointerType>(V->getType())) &&
924 "SrcValue is not a pointer?");
927 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
929 ID.AddInteger(Offset);
931 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
932 return SDOperand(E, 0);
933 SDNode *N = new SrcValueSDNode(V, Offset);
934 CSEMap.InsertNode(N, IP);
935 AllNodes.push_back(N);
936 return SDOperand(N, 0);
939 /// CreateStackTemporary - Create a stack temporary, suitable for holding the
940 /// specified value type.
941 SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
942 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
943 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
944 const Type *Ty = MVT::getTypeForValueType(VT);
945 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
946 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
947 return getFrameIndex(FrameIdx, TLI.getPointerTy());
951 SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
952 SDOperand N2, ISD::CondCode Cond) {
953 // These setcc operations always fold.
957 case ISD::SETFALSE2: return getConstant(0, VT);
959 case ISD::SETTRUE2: return getConstant(1, VT);
971 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
975 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
976 uint64_t C2 = N2C->getValue();
977 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
978 uint64_t C1 = N1C->getValue();
980 // Sign extend the operands if required
981 if (ISD::isSignedIntSetCC(Cond)) {
982 C1 = N1C->getSignExtended();
983 C2 = N2C->getSignExtended();
987 default: assert(0 && "Unknown integer setcc!");
988 case ISD::SETEQ: return getConstant(C1 == C2, VT);
989 case ISD::SETNE: return getConstant(C1 != C2, VT);
990 case ISD::SETULT: return getConstant(C1 < C2, VT);
991 case ISD::SETUGT: return getConstant(C1 > C2, VT);
992 case ISD::SETULE: return getConstant(C1 <= C2, VT);
993 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
994 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
995 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
996 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
997 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
1001 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
1002 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
1003 // No compile time operations on this type yet.
1004 if (N1C->getValueType(0) == MVT::ppcf128)
1007 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
1010 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1011 return getNode(ISD::UNDEF, VT);
1013 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1014 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1015 return getNode(ISD::UNDEF, VT);
1017 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
1018 R==APFloat::cmpLessThan, VT);
1019 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1020 return getNode(ISD::UNDEF, VT);
1022 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1023 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1024 return getNode(ISD::UNDEF, VT);
1026 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1027 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1028 return getNode(ISD::UNDEF, VT);
1030 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
1031 R==APFloat::cmpEqual, VT);
1032 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1033 return getNode(ISD::UNDEF, VT);
1035 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
1036 R==APFloat::cmpEqual, VT);
1037 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1038 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1039 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1040 R==APFloat::cmpEqual, VT);
1041 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1042 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1043 R==APFloat::cmpLessThan, VT);
1044 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1045 R==APFloat::cmpUnordered, VT);
1046 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1047 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
1050 // Ensure that the constant occurs on the RHS.
1051 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
1054 // Could not fold it.
1058 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1059 /// this predicate to simplify operations downstream. Mask is known to be zero
1060 /// for bits that V cannot have.
1061 bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1062 unsigned Depth) const {
1063 // The masks are not wide enough to represent this type! Should use APInt.
1064 if (Op.getValueType() == MVT::i128)
1067 uint64_t KnownZero, KnownOne;
1068 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1069 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1070 return (KnownZero & Mask) == Mask;
1073 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
1074 /// known to be either zero or one and return them in the KnownZero/KnownOne
1075 /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1077 void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1078 uint64_t &KnownZero, uint64_t &KnownOne,
1079 unsigned Depth) const {
1080 KnownZero = KnownOne = 0; // Don't know anything.
1081 if (Depth == 6 || Mask == 0)
1082 return; // Limit search depth.
1084 // The masks are not wide enough to represent this type! Should use APInt.
1085 if (Op.getValueType() == MVT::i128)
1088 uint64_t KnownZero2, KnownOne2;
1090 switch (Op.getOpcode()) {
1092 // We know all of the bits for a constant!
1093 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1094 KnownZero = ~KnownOne & Mask;
1097 // If either the LHS or the RHS are Zero, the result is zero.
1098 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1100 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1101 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1102 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1104 // Output known-1 bits are only known if set in both the LHS & RHS.
1105 KnownOne &= KnownOne2;
1106 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1107 KnownZero |= KnownZero2;
1110 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1112 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1113 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1114 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1116 // Output known-0 bits are only known if clear in both the LHS & RHS.
1117 KnownZero &= KnownZero2;
1118 // Output known-1 are known to be set if set in either the LHS | RHS.
1119 KnownOne |= KnownOne2;
1122 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1123 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1124 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1125 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1127 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1128 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1129 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1130 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1131 KnownZero = KnownZeroOut;
1135 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1136 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1137 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1138 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1140 // Only known if known in both the LHS and RHS.
1141 KnownOne &= KnownOne2;
1142 KnownZero &= KnownZero2;
1144 case ISD::SELECT_CC:
1145 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1146 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1147 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1148 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1150 // Only known if known in both the LHS and RHS.
1151 KnownOne &= KnownOne2;
1152 KnownZero &= KnownZero2;
1155 // If we know the result of a setcc has the top bits zero, use this info.
1156 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1157 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1160 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1161 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1162 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1163 KnownZero, KnownOne, Depth+1);
1164 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1165 KnownZero <<= SA->getValue();
1166 KnownOne <<= SA->getValue();
1167 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1171 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1172 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1173 MVT::ValueType VT = Op.getValueType();
1174 unsigned ShAmt = SA->getValue();
1176 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1177 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1178 KnownZero, KnownOne, Depth+1);
1179 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1180 KnownZero &= TypeMask;
1181 KnownOne &= TypeMask;
1182 KnownZero >>= ShAmt;
1185 uint64_t HighBits = (1ULL << ShAmt)-1;
1186 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1187 KnownZero |= HighBits; // High bits known zero.
1191 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1192 MVT::ValueType VT = Op.getValueType();
1193 unsigned ShAmt = SA->getValue();
1195 // Compute the new bits that are at the top now.
1196 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1198 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1199 // If any of the demanded bits are produced by the sign extension, we also
1200 // demand the input sign bit.
1201 uint64_t HighBits = (1ULL << ShAmt)-1;
1202 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1203 if (HighBits & Mask)
1204 InDemandedMask |= MVT::getIntVTSignBit(VT);
1206 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1208 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1209 KnownZero &= TypeMask;
1210 KnownOne &= TypeMask;
1211 KnownZero >>= ShAmt;
1214 // Handle the sign bits.
1215 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1216 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1218 if (KnownZero & SignBit) {
1219 KnownZero |= HighBits; // New bits are known zero.
1220 } else if (KnownOne & SignBit) {
1221 KnownOne |= HighBits; // New bits are known one.
1225 case ISD::SIGN_EXTEND_INREG: {
1226 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1228 // Sign extension. Compute the demanded bits in the result that are not
1229 // present in the input.
1230 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1232 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1233 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1235 // If the sign extended bits are demanded, we know that the sign
1238 InputDemandedBits |= InSignBit;
1240 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1241 KnownZero, KnownOne, Depth+1);
1242 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1244 // If the sign bit of the input is known set or clear, then we know the
1245 // top bits of the result.
1246 if (KnownZero & InSignBit) { // Input sign bit known clear
1247 KnownZero |= NewBits;
1248 KnownOne &= ~NewBits;
1249 } else if (KnownOne & InSignBit) { // Input sign bit known set
1250 KnownOne |= NewBits;
1251 KnownZero &= ~NewBits;
1252 } else { // Input sign bit unknown
1253 KnownZero &= ~NewBits;
1254 KnownOne &= ~NewBits;
1261 MVT::ValueType VT = Op.getValueType();
1262 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1263 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1268 if (ISD::isZEXTLoad(Op.Val)) {
1269 LoadSDNode *LD = cast<LoadSDNode>(Op);
1270 MVT::ValueType VT = LD->getLoadedVT();
1271 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1275 case ISD::ZERO_EXTEND: {
1276 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1277 uint64_t NewBits = (~InMask) & Mask;
1278 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1280 KnownZero |= NewBits & Mask;
1281 KnownOne &= ~NewBits;
1284 case ISD::SIGN_EXTEND: {
1285 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1286 unsigned InBits = MVT::getSizeInBits(InVT);
1287 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1288 uint64_t InSignBit = 1ULL << (InBits-1);
1289 uint64_t NewBits = (~InMask) & Mask;
1290 uint64_t InDemandedBits = Mask & InMask;
1292 // If any of the sign extended bits are demanded, we know that the sign
1295 InDemandedBits |= InSignBit;
1297 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1299 // If the sign bit is known zero or one, the top bits match.
1300 if (KnownZero & InSignBit) {
1301 KnownZero |= NewBits;
1302 KnownOne &= ~NewBits;
1303 } else if (KnownOne & InSignBit) {
1304 KnownOne |= NewBits;
1305 KnownZero &= ~NewBits;
1306 } else { // Otherwise, top bits aren't known.
1307 KnownOne &= ~NewBits;
1308 KnownZero &= ~NewBits;
1312 case ISD::ANY_EXTEND: {
1313 MVT::ValueType VT = Op.getOperand(0).getValueType();
1314 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1315 KnownZero, KnownOne, Depth+1);
1318 case ISD::TRUNCATE: {
1319 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1320 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1321 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1322 KnownZero &= OutMask;
1323 KnownOne &= OutMask;
1326 case ISD::AssertZext: {
1327 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1328 uint64_t InMask = MVT::getIntVTBitMask(VT);
1329 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1331 KnownZero |= (~InMask) & Mask;
1335 // If either the LHS or the RHS are Zero, the result is zero.
1336 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1337 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1338 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1339 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1341 // Output known-0 bits are known if clear or set in both the low clear bits
1342 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1343 // low 3 bits clear.
1344 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1345 CountTrailingZeros_64(~KnownZero2));
1347 KnownZero = (1ULL << KnownZeroOut) - 1;
1352 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1355 // We know that the top bits of C-X are clear if X contains less bits
1356 // than C (i.e. no wrap-around can happen). For example, 20-X is
1357 // positive if we can prove that X is >= 0 and < 16.
1358 MVT::ValueType VT = CLHS->getValueType(0);
1359 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1360 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1361 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1362 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1363 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1365 // If all of the MaskV bits are known to be zero, then we know the output
1366 // top bits are zero, because we now know that the output is from [0-C].
1367 if ((KnownZero & MaskV) == MaskV) {
1368 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1369 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1370 KnownOne = 0; // No one bits known.
1372 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1378 // Allow the target to implement this method for its nodes.
1379 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1380 case ISD::INTRINSIC_WO_CHAIN:
1381 case ISD::INTRINSIC_W_CHAIN:
1382 case ISD::INTRINSIC_VOID:
1383 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1389 /// ComputeNumSignBits - Return the number of times the sign bit of the
1390 /// register is replicated into the other bits. We know that at least 1 bit
1391 /// is always equal to the sign bit (itself), but other cases can give us
1392 /// information. For example, immediately after an "SRA X, 2", we know that
1393 /// the top 3 bits are all equal to each other, so we return 3.
1394 unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1395 MVT::ValueType VT = Op.getValueType();
1396 assert(MVT::isInteger(VT) && "Invalid VT!");
1397 unsigned VTBits = MVT::getSizeInBits(VT);
1401 return 1; // Limit search depth.
1403 switch (Op.getOpcode()) {
1405 case ISD::AssertSext:
1406 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1407 return VTBits-Tmp+1;
1408 case ISD::AssertZext:
1409 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1412 case ISD::Constant: {
1413 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1414 // If negative, invert the bits, then look at it.
1415 if (Val & MVT::getIntVTSignBit(VT))
1418 // Shift the bits so they are the leading bits in the int64_t.
1421 // Return # leading zeros. We use 'min' here in case Val was zero before
1422 // shifting. We don't want to return '64' as for an i32 "0".
1423 return std::min(VTBits, CountLeadingZeros_64(Val));
1426 case ISD::SIGN_EXTEND:
1427 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1428 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1430 case ISD::SIGN_EXTEND_INREG:
1431 // Max of the input and what this extends.
1432 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1435 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1436 return std::max(Tmp, Tmp2);
1439 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1440 // SRA X, C -> adds C sign bits.
1441 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1442 Tmp += C->getValue();
1443 if (Tmp > VTBits) Tmp = VTBits;
1447 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1448 // shl destroys sign bits.
1449 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1450 if (C->getValue() >= VTBits || // Bad shift.
1451 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1452 return Tmp - C->getValue();
1457 case ISD::XOR: // NOT is handled here.
1458 // Logical binary ops preserve the number of sign bits.
1459 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1460 if (Tmp == 1) return 1; // Early out.
1461 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1462 return std::min(Tmp, Tmp2);
1465 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1466 if (Tmp == 1) return 1; // Early out.
1467 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1468 return std::min(Tmp, Tmp2);
1471 // If setcc returns 0/-1, all bits are sign bits.
1472 if (TLI.getSetCCResultContents() ==
1473 TargetLowering::ZeroOrNegativeOneSetCCResult)
1478 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1479 unsigned RotAmt = C->getValue() & (VTBits-1);
1481 // Handle rotate right by N like a rotate left by 32-N.
1482 if (Op.getOpcode() == ISD::ROTR)
1483 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1485 // If we aren't rotating out all of the known-in sign bits, return the
1486 // number that are left. This handles rotl(sext(x), 1) for example.
1487 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1488 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1492 // Add can have at most one carry bit. Thus we know that the output
1493 // is, at worst, one more bit than the inputs.
1494 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1495 if (Tmp == 1) return 1; // Early out.
1497 // Special case decrementing a value (ADD X, -1):
1498 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1499 if (CRHS->isAllOnesValue()) {
1500 uint64_t KnownZero, KnownOne;
1501 uint64_t Mask = MVT::getIntVTBitMask(VT);
1502 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1504 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1506 if ((KnownZero|1) == Mask)
1509 // If we are subtracting one from a positive number, there is no carry
1510 // out of the result.
1511 if (KnownZero & MVT::getIntVTSignBit(VT))
1515 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1516 if (Tmp2 == 1) return 1;
1517 return std::min(Tmp, Tmp2)-1;
1521 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1522 if (Tmp2 == 1) return 1;
1525 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1526 if (CLHS->getValue() == 0) {
1527 uint64_t KnownZero, KnownOne;
1528 uint64_t Mask = MVT::getIntVTBitMask(VT);
1529 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1530 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1532 if ((KnownZero|1) == Mask)
1535 // If the input is known to be positive (the sign bit is known clear),
1536 // the output of the NEG has the same number of sign bits as the input.
1537 if (KnownZero & MVT::getIntVTSignBit(VT))
1540 // Otherwise, we treat this like a SUB.
1543 // Sub can have at most one carry bit. Thus we know that the output
1544 // is, at worst, one more bit than the inputs.
1545 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1546 if (Tmp == 1) return 1; // Early out.
1547 return std::min(Tmp, Tmp2)-1;
1550 // FIXME: it's tricky to do anything useful for this, but it is an important
1551 // case for targets like X86.
1555 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1556 if (Op.getOpcode() == ISD::LOAD) {
1557 LoadSDNode *LD = cast<LoadSDNode>(Op);
1558 unsigned ExtType = LD->getExtensionType();
1561 case ISD::SEXTLOAD: // '17' bits known
1562 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1563 return VTBits-Tmp+1;
1564 case ISD::ZEXTLOAD: // '16' bits known
1565 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1570 // Allow the target to implement this method for its nodes.
1571 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1572 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1573 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1574 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1575 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1576 if (NumBits > 1) return NumBits;
1579 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1580 // use this information.
1581 uint64_t KnownZero, KnownOne;
1582 uint64_t Mask = MVT::getIntVTBitMask(VT);
1583 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1585 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1586 if (KnownZero & SignBit) { // SignBit is 0
1588 } else if (KnownOne & SignBit) { // SignBit is 1;
1595 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1596 // the number of identical bits in the top of the input value.
1599 // Return # leading zeros. We use 'min' here in case Val was zero before
1600 // shifting. We don't want to return '64' as for an i32 "0".
1601 return std::min(VTBits, CountLeadingZeros_64(Mask));
1605 /// getNode - Gets or creates the specified node.
1607 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
1608 FoldingSetNodeID ID;
1609 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
1611 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1612 return SDOperand(E, 0);
1613 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
1614 CSEMap.InsertNode(N, IP);
1616 AllNodes.push_back(N);
1617 return SDOperand(N, 0);
1620 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1621 SDOperand Operand) {
1623 // Constant fold unary operations with an integer constant operand.
1624 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1625 uint64_t Val = C->getValue();
1628 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
1629 case ISD::ANY_EXTEND:
1630 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1631 case ISD::TRUNCATE: return getConstant(Val, VT);
1632 case ISD::UINT_TO_FP:
1633 case ISD::SINT_TO_FP: {
1634 const uint64_t zero[] = {0, 0};
1635 // No compile time operations on this type.
1636 if (VT==MVT::ppcf128)
1638 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
1639 (void)apf.convertFromZeroExtendedInteger(&Val,
1640 MVT::getSizeInBits(Operand.getValueType()),
1641 Opcode==ISD::SINT_TO_FP,
1642 APFloat::rmNearestTiesToEven);
1643 return getConstantFP(apf, VT);
1645 case ISD::BIT_CONVERT:
1646 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
1647 return getConstantFP(BitsToFloat(Val), VT);
1648 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
1649 return getConstantFP(BitsToDouble(Val), VT);
1653 default: assert(0 && "Invalid bswap!"); break;
1654 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1655 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1656 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1661 default: assert(0 && "Invalid ctpop!"); break;
1662 case MVT::i1: return getConstant(Val != 0, VT);
1664 Tmp1 = (unsigned)Val & 0xFF;
1665 return getConstant(CountPopulation_32(Tmp1), VT);
1667 Tmp1 = (unsigned)Val & 0xFFFF;
1668 return getConstant(CountPopulation_32(Tmp1), VT);
1670 return getConstant(CountPopulation_32((unsigned)Val), VT);
1672 return getConstant(CountPopulation_64(Val), VT);
1676 default: assert(0 && "Invalid ctlz!"); break;
1677 case MVT::i1: return getConstant(Val == 0, VT);
1679 Tmp1 = (unsigned)Val & 0xFF;
1680 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1682 Tmp1 = (unsigned)Val & 0xFFFF;
1683 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1685 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1687 return getConstant(CountLeadingZeros_64(Val), VT);
1691 default: assert(0 && "Invalid cttz!"); break;
1692 case MVT::i1: return getConstant(Val == 0, VT);
1694 Tmp1 = (unsigned)Val | 0x100;
1695 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1697 Tmp1 = (unsigned)Val | 0x10000;
1698 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1700 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1702 return getConstant(CountTrailingZeros_64(Val), VT);
1707 // Constant fold unary operations with a floating point constant operand.
1708 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1709 APFloat V = C->getValueAPF(); // make copy
1710 if (VT!=MVT::ppcf128 && Operand.getValueType()!=MVT::ppcf128) {
1714 return getConstantFP(V, VT);
1717 return getConstantFP(V, VT);
1719 case ISD::FP_EXTEND:
1720 // This can return overflow, underflow, or inexact; we don't care.
1721 // FIXME need to be more flexible about rounding mode.
1722 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle :
1723 VT==MVT::f64 ? APFloat::IEEEdouble :
1724 VT==MVT::f80 ? APFloat::x87DoubleExtended :
1725 VT==MVT::f128 ? APFloat::IEEEquad :
1727 APFloat::rmNearestTiesToEven);
1728 return getConstantFP(V, VT);
1729 case ISD::FP_TO_SINT:
1730 case ISD::FP_TO_UINT: {
1732 assert(integerPartWidth >= 64);
1733 // FIXME need to be more flexible about rounding mode.
1734 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1735 Opcode==ISD::FP_TO_SINT,
1736 APFloat::rmTowardZero);
1737 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1739 return getConstant(x, VT);
1741 case ISD::BIT_CONVERT:
1742 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1743 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1744 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1745 return getConstant(V.convertToAPInt().getZExtValue(), VT);
1751 unsigned OpOpcode = Operand.Val->getOpcode();
1753 case ISD::TokenFactor:
1754 return Operand; // Factor of one node? No factor.
1756 case ISD::FP_EXTEND:
1757 assert(MVT::isFloatingPoint(VT) &&
1758 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
1760 case ISD::SIGN_EXTEND:
1761 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1762 "Invalid SIGN_EXTEND!");
1763 if (Operand.getValueType() == VT) return Operand; // noop extension
1764 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1765 && "Invalid sext node, dst < src!");
1766 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1767 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1769 case ISD::ZERO_EXTEND:
1770 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1771 "Invalid ZERO_EXTEND!");
1772 if (Operand.getValueType() == VT) return Operand; // noop extension
1773 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1774 && "Invalid zext node, dst < src!");
1775 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1776 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1778 case ISD::ANY_EXTEND:
1779 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1780 "Invalid ANY_EXTEND!");
1781 if (Operand.getValueType() == VT) return Operand; // noop extension
1782 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1783 && "Invalid anyext node, dst < src!");
1784 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1785 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1786 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1789 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1790 "Invalid TRUNCATE!");
1791 if (Operand.getValueType() == VT) return Operand; // noop truncate
1792 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1793 && "Invalid truncate node, src < dst!");
1794 if (OpOpcode == ISD::TRUNCATE)
1795 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1796 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1797 OpOpcode == ISD::ANY_EXTEND) {
1798 // If the source is smaller than the dest, we still need an extend.
1799 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1800 < MVT::getSizeInBits(VT))
1801 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1802 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1803 > MVT::getSizeInBits(VT))
1804 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1806 return Operand.Val->getOperand(0);
1809 case ISD::BIT_CONVERT:
1810 // Basic sanity checking.
1811 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
1812 && "Cannot BIT_CONVERT between types of different sizes!");
1813 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1814 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1815 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1816 if (OpOpcode == ISD::UNDEF)
1817 return getNode(ISD::UNDEF, VT);
1819 case ISD::SCALAR_TO_VECTOR:
1820 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1821 MVT::getVectorElementType(VT) == Operand.getValueType() &&
1822 "Illegal SCALAR_TO_VECTOR node!");
1825 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1826 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1827 Operand.Val->getOperand(0));
1828 if (OpOpcode == ISD::FNEG) // --X -> X
1829 return Operand.Val->getOperand(0);
1832 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1833 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1838 SDVTList VTs = getVTList(VT);
1839 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1840 FoldingSetNodeID ID;
1841 SDOperand Ops[1] = { Operand };
1842 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
1844 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1845 return SDOperand(E, 0);
1846 N = new UnarySDNode(Opcode, VTs, Operand);
1847 CSEMap.InsertNode(N, IP);
1849 N = new UnarySDNode(Opcode, VTs, Operand);
1851 AllNodes.push_back(N);
1852 return SDOperand(N, 0);
1857 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1858 SDOperand N1, SDOperand N2) {
1861 case ISD::TokenFactor:
1862 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1863 N2.getValueType() == MVT::Other && "Invalid token factor!");
1872 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1879 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1886 assert(N1.getValueType() == N2.getValueType() &&
1887 N1.getValueType() == VT && "Binary operator types must match!");
1889 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1890 assert(N1.getValueType() == VT &&
1891 MVT::isFloatingPoint(N1.getValueType()) &&
1892 MVT::isFloatingPoint(N2.getValueType()) &&
1893 "Invalid FCOPYSIGN!");
1900 assert(VT == N1.getValueType() &&
1901 "Shift operators return type must be the same as their first arg");
1902 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1903 VT != MVT::i1 && "Shifts only work on integers");
1905 case ISD::FP_ROUND_INREG: {
1906 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1907 assert(VT == N1.getValueType() && "Not an inreg round!");
1908 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1909 "Cannot FP_ROUND_INREG integer types");
1910 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1911 "Not rounding down!");
1914 case ISD::AssertSext:
1915 case ISD::AssertZext:
1916 case ISD::SIGN_EXTEND_INREG: {
1917 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1918 assert(VT == N1.getValueType() && "Not an inreg extend!");
1919 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1920 "Cannot *_EXTEND_INREG FP types");
1921 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1929 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1930 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1932 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1933 int64_t Val = N1C->getValue();
1934 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1935 Val <<= 64-FromBits;
1936 Val >>= 64-FromBits;
1937 return getConstant(Val, VT);
1941 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1943 case ISD::ADD: return getConstant(C1 + C2, VT);
1944 case ISD::SUB: return getConstant(C1 - C2, VT);
1945 case ISD::MUL: return getConstant(C1 * C2, VT);
1947 if (C2) return getConstant(C1 / C2, VT);
1950 if (C2) return getConstant(C1 % C2, VT);
1953 if (C2) return getConstant(N1C->getSignExtended() /
1954 N2C->getSignExtended(), VT);
1957 if (C2) return getConstant(N1C->getSignExtended() %
1958 N2C->getSignExtended(), VT);
1960 case ISD::AND : return getConstant(C1 & C2, VT);
1961 case ISD::OR : return getConstant(C1 | C2, VT);
1962 case ISD::XOR : return getConstant(C1 ^ C2, VT);
1963 case ISD::SHL : return getConstant(C1 << C2, VT);
1964 case ISD::SRL : return getConstant(C1 >> C2, VT);
1965 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
1967 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1970 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1974 } else { // Cannonicalize constant to RHS if commutative
1975 if (isCommutativeBinOp(Opcode)) {
1976 std::swap(N1C, N2C);
1982 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1983 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1985 if (N2CFP && VT!=MVT::ppcf128) {
1986 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
1987 APFloat::opStatus s;
1990 s = V1.add(V2, APFloat::rmNearestTiesToEven);
1991 if (s!=APFloat::opInvalidOp)
1992 return getConstantFP(V1, VT);
1995 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
1996 if (s!=APFloat::opInvalidOp)
1997 return getConstantFP(V1, VT);
2000 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2001 if (s!=APFloat::opInvalidOp)
2002 return getConstantFP(V1, VT);
2005 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2006 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2007 return getConstantFP(V1, VT);
2010 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2011 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2012 return getConstantFP(V1, VT);
2014 case ISD::FCOPYSIGN:
2016 return getConstantFP(V1, VT);
2019 } else { // Cannonicalize constant to RHS if commutative
2020 if (isCommutativeBinOp(Opcode)) {
2021 std::swap(N1CFP, N2CFP);
2027 // Canonicalize an UNDEF to the RHS, even over a constant.
2028 if (N1.getOpcode() == ISD::UNDEF) {
2029 if (isCommutativeBinOp(Opcode)) {
2033 case ISD::FP_ROUND_INREG:
2034 case ISD::SIGN_EXTEND_INREG:
2040 return N1; // fold op(undef, arg2) -> undef
2047 if (!MVT::isVector(VT))
2048 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2049 // For vectors, we can't easily build an all zero vector, just return
2056 // Fold a bunch of operators when the RHS is undef.
2057 if (N2.getOpcode() == ISD::UNDEF) {
2073 return N2; // fold op(arg1, undef) -> undef
2078 if (!MVT::isVector(VT))
2079 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2080 // For vectors, we can't easily build an all zero vector, just return
2084 if (!MVT::isVector(VT))
2085 return getConstant(MVT::getIntVTBitMask(VT), VT);
2086 // For vectors, we can't easily build an all one vector, just return
2096 case ISD::TokenFactor:
2097 // Fold trivial token factors.
2098 if (N1.getOpcode() == ISD::EntryToken) return N2;
2099 if (N2.getOpcode() == ISD::EntryToken) return N1;
2103 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2104 // worth handling here.
2105 if (N2C && N2C->getValue() == 0)
2110 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
2111 // worth handling here.
2112 if (N2C && N2C->getValue() == 0)
2115 case ISD::FP_ROUND_INREG:
2116 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
2118 case ISD::SIGN_EXTEND_INREG: {
2119 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2120 if (EVT == VT) return N1; // Not actually extending
2123 case ISD::EXTRACT_VECTOR_ELT:
2124 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2126 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2127 // expanding copies of large vectors from registers.
2128 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2129 N1.getNumOperands() > 0) {
2131 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2132 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2133 N1.getOperand(N2C->getValue() / Factor),
2134 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2137 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2138 // expanding large vector constants.
2139 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2140 return N1.getOperand(N2C->getValue());
2142 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2143 // operations are lowered to scalars.
2144 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2145 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2147 return N1.getOperand(1);
2149 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2152 case ISD::EXTRACT_ELEMENT:
2153 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
2155 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2156 // 64-bit integers into 32-bit parts. Instead of building the extract of
2157 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2158 if (N1.getOpcode() == ISD::BUILD_PAIR)
2159 return N1.getOperand(N2C->getValue());
2161 // EXTRACT_ELEMENT of a constant int is also very common.
2162 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2163 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2164 return getConstant(C->getValue() >> Shift, VT);
2168 // FIXME: figure out how to safely handle things like
2169 // int foo(int x) { return 1 << (x & 255); }
2170 // int bar() { return foo(256); }
2175 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2176 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
2177 return getNode(Opcode, VT, N1, N2.getOperand(0));
2178 else if (N2.getOpcode() == ISD::AND)
2179 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
2180 // If the and is only masking out bits that cannot effect the shift,
2181 // eliminate the and.
2182 unsigned NumBits = MVT::getSizeInBits(VT);
2183 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2184 return getNode(Opcode, VT, N1, N2.getOperand(0));
2190 // Memoize this node if possible.
2192 SDVTList VTs = getVTList(VT);
2193 if (VT != MVT::Flag) {
2194 SDOperand Ops[] = { N1, N2 };
2195 FoldingSetNodeID ID;
2196 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
2198 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2199 return SDOperand(E, 0);
2200 N = new BinarySDNode(Opcode, VTs, N1, N2);
2201 CSEMap.InsertNode(N, IP);
2203 N = new BinarySDNode(Opcode, VTs, N1, N2);
2206 AllNodes.push_back(N);
2207 return SDOperand(N, 0);
2210 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2211 SDOperand N1, SDOperand N2, SDOperand N3) {
2212 // Perform various simplifications.
2213 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2214 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2217 // Use FoldSetCC to simplify SETCC's.
2218 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
2219 if (Simp.Val) return Simp;
2224 if (N1C->getValue())
2225 return N2; // select true, X, Y -> X
2227 return N3; // select false, X, Y -> Y
2229 if (N2 == N3) return N2; // select C, X, X -> X
2233 if (N2C->getValue()) // Unconditional branch
2234 return getNode(ISD::BR, MVT::Other, N1, N3);
2236 return N1; // Never-taken branch
2238 case ISD::VECTOR_SHUFFLE:
2239 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2240 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2241 N3.getOpcode() == ISD::BUILD_VECTOR &&
2242 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2243 "Illegal VECTOR_SHUFFLE node!");
2245 case ISD::BIT_CONVERT:
2246 // Fold bit_convert nodes from a type to themselves.
2247 if (N1.getValueType() == VT)
2252 // Memoize node if it doesn't produce a flag.
2254 SDVTList VTs = getVTList(VT);
2255 if (VT != MVT::Flag) {
2256 SDOperand Ops[] = { N1, N2, N3 };
2257 FoldingSetNodeID ID;
2258 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2260 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2261 return SDOperand(E, 0);
2262 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2263 CSEMap.InsertNode(N, IP);
2265 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2267 AllNodes.push_back(N);
2268 return SDOperand(N, 0);
2271 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2272 SDOperand N1, SDOperand N2, SDOperand N3,
2274 SDOperand Ops[] = { N1, N2, N3, N4 };
2275 return getNode(Opcode, VT, Ops, 4);
2278 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2279 SDOperand N1, SDOperand N2, SDOperand N3,
2280 SDOperand N4, SDOperand N5) {
2281 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2282 return getNode(Opcode, VT, Ops, 5);
2285 SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dest,
2286 SDOperand Src, SDOperand Size,
2288 SDOperand AlwaysInline) {
2289 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2290 return getNode(ISD::MEMCPY, MVT::Other, Ops, 6);
2293 SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dest,
2294 SDOperand Src, SDOperand Size,
2296 SDOperand AlwaysInline) {
2297 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2298 return getNode(ISD::MEMMOVE, MVT::Other, Ops, 6);
2301 SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dest,
2302 SDOperand Src, SDOperand Size,
2304 SDOperand AlwaysInline) {
2305 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2306 return getNode(ISD::MEMSET, MVT::Other, Ops, 6);
2309 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2310 SDOperand Chain, SDOperand Ptr,
2311 const Value *SV, int SVOffset,
2312 bool isVolatile, unsigned Alignment) {
2313 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2315 if (VT != MVT::iPTR) {
2316 Ty = MVT::getTypeForValueType(VT);
2318 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2319 assert(PT && "Value for load must be a pointer");
2320 Ty = PT->getElementType();
2322 assert(Ty && "Could not get type information for load");
2323 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2325 SDVTList VTs = getVTList(VT, MVT::Other);
2326 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2327 SDOperand Ops[] = { Chain, Ptr, Undef };
2328 FoldingSetNodeID ID;
2329 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2330 ID.AddInteger(ISD::UNINDEXED);
2331 ID.AddInteger(ISD::NON_EXTLOAD);
2332 ID.AddInteger((unsigned int)VT);
2334 ID.AddInteger(SVOffset);
2335 ID.AddInteger(Alignment);
2336 ID.AddInteger(isVolatile);
2338 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2339 return SDOperand(E, 0);
2340 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
2341 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2343 CSEMap.InsertNode(N, IP);
2344 AllNodes.push_back(N);
2345 return SDOperand(N, 0);
2348 SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
2349 SDOperand Chain, SDOperand Ptr,
2351 int SVOffset, MVT::ValueType EVT,
2352 bool isVolatile, unsigned Alignment) {
2353 // If they are asking for an extending load from/to the same thing, return a
2356 return getLoad(VT, Chain, Ptr, SV, SVOffset, isVolatile, Alignment);
2358 if (MVT::isVector(VT))
2359 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
2361 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2362 "Should only be an extending load, not truncating!");
2363 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2364 "Cannot sign/zero extend a FP/Vector load!");
2365 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2366 "Cannot convert from FP to Int or Int -> FP!");
2368 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2370 if (VT != MVT::iPTR) {
2371 Ty = MVT::getTypeForValueType(VT);
2373 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2374 assert(PT && "Value for load must be a pointer");
2375 Ty = PT->getElementType();
2377 assert(Ty && "Could not get type information for load");
2378 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2380 SDVTList VTs = getVTList(VT, MVT::Other);
2381 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2382 SDOperand Ops[] = { Chain, Ptr, Undef };
2383 FoldingSetNodeID ID;
2384 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2385 ID.AddInteger(ISD::UNINDEXED);
2386 ID.AddInteger(ExtType);
2387 ID.AddInteger((unsigned int)EVT);
2389 ID.AddInteger(SVOffset);
2390 ID.AddInteger(Alignment);
2391 ID.AddInteger(isVolatile);
2393 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2394 return SDOperand(E, 0);
2395 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
2396 SV, SVOffset, Alignment, isVolatile);
2397 CSEMap.InsertNode(N, IP);
2398 AllNodes.push_back(N);
2399 return SDOperand(N, 0);
2403 SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2404 SDOperand Offset, ISD::MemIndexedMode AM) {
2405 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
2406 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2407 "Load is already a indexed load!");
2408 MVT::ValueType VT = OrigLoad.getValueType();
2409 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
2410 SDOperand Ops[] = { LD->getChain(), Base, Offset };
2411 FoldingSetNodeID ID;
2412 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2414 ID.AddInteger(LD->getExtensionType());
2415 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
2416 ID.AddPointer(LD->getSrcValue());
2417 ID.AddInteger(LD->getSrcValueOffset());
2418 ID.AddInteger(LD->getAlignment());
2419 ID.AddInteger(LD->isVolatile());
2421 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2422 return SDOperand(E, 0);
2423 SDNode *N = new LoadSDNode(Ops, VTs, AM,
2424 LD->getExtensionType(), LD->getLoadedVT(),
2425 LD->getSrcValue(), LD->getSrcValueOffset(),
2426 LD->getAlignment(), LD->isVolatile());
2427 CSEMap.InsertNode(N, IP);
2428 AllNodes.push_back(N);
2429 return SDOperand(N, 0);
2432 SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
2433 SDOperand Ptr, const Value *SV, int SVOffset,
2434 bool isVolatile, unsigned Alignment) {
2435 MVT::ValueType VT = Val.getValueType();
2437 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2439 if (VT != MVT::iPTR) {
2440 Ty = MVT::getTypeForValueType(VT);
2442 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2443 assert(PT && "Value for store must be a pointer");
2444 Ty = PT->getElementType();
2446 assert(Ty && "Could not get type information for store");
2447 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2449 SDVTList VTs = getVTList(MVT::Other);
2450 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2451 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2452 FoldingSetNodeID ID;
2453 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2454 ID.AddInteger(ISD::UNINDEXED);
2455 ID.AddInteger(false);
2456 ID.AddInteger((unsigned int)VT);
2458 ID.AddInteger(SVOffset);
2459 ID.AddInteger(Alignment);
2460 ID.AddInteger(isVolatile);
2462 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2463 return SDOperand(E, 0);
2464 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
2465 VT, SV, SVOffset, Alignment, isVolatile);
2466 CSEMap.InsertNode(N, IP);
2467 AllNodes.push_back(N);
2468 return SDOperand(N, 0);
2471 SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
2472 SDOperand Ptr, const Value *SV,
2473 int SVOffset, MVT::ValueType SVT,
2474 bool isVolatile, unsigned Alignment) {
2475 MVT::ValueType VT = Val.getValueType();
2478 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
2480 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2481 "Not a truncation?");
2482 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2483 "Can't do FP-INT conversion!");
2485 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2487 if (VT != MVT::iPTR) {
2488 Ty = MVT::getTypeForValueType(VT);
2490 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2491 assert(PT && "Value for store must be a pointer");
2492 Ty = PT->getElementType();
2494 assert(Ty && "Could not get type information for store");
2495 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2497 SDVTList VTs = getVTList(MVT::Other);
2498 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2499 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2500 FoldingSetNodeID ID;
2501 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2502 ID.AddInteger(ISD::UNINDEXED);
2504 ID.AddInteger((unsigned int)SVT);
2506 ID.AddInteger(SVOffset);
2507 ID.AddInteger(Alignment);
2508 ID.AddInteger(isVolatile);
2510 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2511 return SDOperand(E, 0);
2512 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
2513 SVT, SV, SVOffset, Alignment, isVolatile);
2514 CSEMap.InsertNode(N, IP);
2515 AllNodes.push_back(N);
2516 return SDOperand(N, 0);
2520 SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2521 SDOperand Offset, ISD::MemIndexedMode AM) {
2522 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2523 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2524 "Store is already a indexed store!");
2525 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2526 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2527 FoldingSetNodeID ID;
2528 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2530 ID.AddInteger(ST->isTruncatingStore());
2531 ID.AddInteger((unsigned int)(ST->getStoredVT()));
2532 ID.AddPointer(ST->getSrcValue());
2533 ID.AddInteger(ST->getSrcValueOffset());
2534 ID.AddInteger(ST->getAlignment());
2535 ID.AddInteger(ST->isVolatile());
2537 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2538 return SDOperand(E, 0);
2539 SDNode *N = new StoreSDNode(Ops, VTs, AM,
2540 ST->isTruncatingStore(), ST->getStoredVT(),
2541 ST->getSrcValue(), ST->getSrcValueOffset(),
2542 ST->getAlignment(), ST->isVolatile());
2543 CSEMap.InsertNode(N, IP);
2544 AllNodes.push_back(N);
2545 return SDOperand(N, 0);
2548 SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2549 SDOperand Chain, SDOperand Ptr,
2551 SDOperand Ops[] = { Chain, Ptr, SV };
2552 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
2555 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2556 const SDOperand *Ops, unsigned NumOps) {
2558 case 0: return getNode(Opcode, VT);
2559 case 1: return getNode(Opcode, VT, Ops[0]);
2560 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2561 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
2567 case ISD::SELECT_CC: {
2568 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
2569 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2570 "LHS and RHS of condition must have same type!");
2571 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2572 "True and False arms of SelectCC must have same type!");
2573 assert(Ops[2].getValueType() == VT &&
2574 "select_cc node must be of same type as true and false value!");
2578 assert(NumOps == 5 && "BR_CC takes 5 operands!");
2579 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2580 "LHS/RHS of comparison should match types!");
2587 SDVTList VTs = getVTList(VT);
2588 if (VT != MVT::Flag) {
2589 FoldingSetNodeID ID;
2590 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
2592 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2593 return SDOperand(E, 0);
2594 N = new SDNode(Opcode, VTs, Ops, NumOps);
2595 CSEMap.InsertNode(N, IP);
2597 N = new SDNode(Opcode, VTs, Ops, NumOps);
2599 AllNodes.push_back(N);
2600 return SDOperand(N, 0);
2603 SDOperand SelectionDAG::getNode(unsigned Opcode,
2604 std::vector<MVT::ValueType> &ResultTys,
2605 const SDOperand *Ops, unsigned NumOps) {
2606 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2610 SDOperand SelectionDAG::getNode(unsigned Opcode,
2611 const MVT::ValueType *VTs, unsigned NumVTs,
2612 const SDOperand *Ops, unsigned NumOps) {
2614 return getNode(Opcode, VTs[0], Ops, NumOps);
2615 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2618 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2619 const SDOperand *Ops, unsigned NumOps) {
2620 if (VTList.NumVTs == 1)
2621 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
2624 // FIXME: figure out how to safely handle things like
2625 // int foo(int x) { return 1 << (x & 255); }
2626 // int bar() { return foo(256); }
2628 case ISD::SRA_PARTS:
2629 case ISD::SRL_PARTS:
2630 case ISD::SHL_PARTS:
2631 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2632 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
2633 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2634 else if (N3.getOpcode() == ISD::AND)
2635 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2636 // If the and is only masking out bits that cannot effect the shift,
2637 // eliminate the and.
2638 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2639 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2640 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2646 // Memoize the node unless it returns a flag.
2648 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
2649 FoldingSetNodeID ID;
2650 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
2652 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2653 return SDOperand(E, 0);
2655 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2656 else if (NumOps == 2)
2657 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2658 else if (NumOps == 3)
2659 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2661 N = new SDNode(Opcode, VTList, Ops, NumOps);
2662 CSEMap.InsertNode(N, IP);
2665 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2666 else if (NumOps == 2)
2667 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2668 else if (NumOps == 3)
2669 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2671 N = new SDNode(Opcode, VTList, Ops, NumOps);
2673 AllNodes.push_back(N);
2674 return SDOperand(N, 0);
2677 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2678 return getNode(Opcode, VTList, 0, 0);
2681 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2683 SDOperand Ops[] = { N1 };
2684 return getNode(Opcode, VTList, Ops, 1);
2687 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2688 SDOperand N1, SDOperand N2) {
2689 SDOperand Ops[] = { N1, N2 };
2690 return getNode(Opcode, VTList, Ops, 2);
2693 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2694 SDOperand N1, SDOperand N2, SDOperand N3) {
2695 SDOperand Ops[] = { N1, N2, N3 };
2696 return getNode(Opcode, VTList, Ops, 3);
2699 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2700 SDOperand N1, SDOperand N2, SDOperand N3,
2702 SDOperand Ops[] = { N1, N2, N3, N4 };
2703 return getNode(Opcode, VTList, Ops, 4);
2706 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2707 SDOperand N1, SDOperand N2, SDOperand N3,
2708 SDOperand N4, SDOperand N5) {
2709 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2710 return getNode(Opcode, VTList, Ops, 5);
2713 SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
2714 return makeVTList(SDNode::getValueTypeList(VT), 1);
2717 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
2718 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2719 E = VTList.end(); I != E; ++I) {
2720 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
2721 return makeVTList(&(*I)[0], 2);
2723 std::vector<MVT::ValueType> V;
2726 VTList.push_front(V);
2727 return makeVTList(&(*VTList.begin())[0], 2);
2729 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2730 MVT::ValueType VT3) {
2731 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2732 E = VTList.end(); I != E; ++I) {
2733 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2735 return makeVTList(&(*I)[0], 3);
2737 std::vector<MVT::ValueType> V;
2741 VTList.push_front(V);
2742 return makeVTList(&(*VTList.begin())[0], 3);
2745 SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2747 case 0: assert(0 && "Cannot have nodes without results!");
2748 case 1: return getVTList(VTs[0]);
2749 case 2: return getVTList(VTs[0], VTs[1]);
2750 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2754 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2755 E = VTList.end(); I != E; ++I) {
2756 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2758 bool NoMatch = false;
2759 for (unsigned i = 2; i != NumVTs; ++i)
2760 if (VTs[i] != (*I)[i]) {
2765 return makeVTList(&*I->begin(), NumVTs);
2768 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2769 return makeVTList(&*VTList.begin()->begin(), NumVTs);
2773 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2774 /// specified operands. If the resultant node already exists in the DAG,
2775 /// this does not modify the specified node, instead it returns the node that
2776 /// already exists. If the resultant node does not exist in the DAG, the
2777 /// input node is returned. As a degenerate case, if you specify the same
2778 /// input operands as the node already has, the input node is returned.
2779 SDOperand SelectionDAG::
2780 UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2781 SDNode *N = InN.Val;
2782 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2784 // Check to see if there is no change.
2785 if (Op == N->getOperand(0)) return InN;
2787 // See if the modified node already exists.
2788 void *InsertPos = 0;
2789 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2790 return SDOperand(Existing, InN.ResNo);
2792 // Nope it doesn't. Remove the node from it's current place in the maps.
2794 RemoveNodeFromCSEMaps(N);
2796 // Now we update the operands.
2797 N->OperandList[0].Val->removeUser(N);
2799 N->OperandList[0] = Op;
2801 // If this gets put into a CSE map, add it.
2802 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2806 SDOperand SelectionDAG::
2807 UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2808 SDNode *N = InN.Val;
2809 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2811 // Check to see if there is no change.
2812 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2813 return InN; // No operands changed, just return the input node.
2815 // See if the modified node already exists.
2816 void *InsertPos = 0;
2817 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2818 return SDOperand(Existing, InN.ResNo);
2820 // Nope it doesn't. Remove the node from it's current place in the maps.
2822 RemoveNodeFromCSEMaps(N);
2824 // Now we update the operands.
2825 if (N->OperandList[0] != Op1) {
2826 N->OperandList[0].Val->removeUser(N);
2827 Op1.Val->addUser(N);
2828 N->OperandList[0] = Op1;
2830 if (N->OperandList[1] != Op2) {
2831 N->OperandList[1].Val->removeUser(N);
2832 Op2.Val->addUser(N);
2833 N->OperandList[1] = Op2;
2836 // If this gets put into a CSE map, add it.
2837 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2841 SDOperand SelectionDAG::
2842 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2843 SDOperand Ops[] = { Op1, Op2, Op3 };
2844 return UpdateNodeOperands(N, Ops, 3);
2847 SDOperand SelectionDAG::
2848 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2849 SDOperand Op3, SDOperand Op4) {
2850 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2851 return UpdateNodeOperands(N, Ops, 4);
2854 SDOperand SelectionDAG::
2855 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2856 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2857 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2858 return UpdateNodeOperands(N, Ops, 5);
2862 SDOperand SelectionDAG::
2863 UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
2864 SDNode *N = InN.Val;
2865 assert(N->getNumOperands() == NumOps &&
2866 "Update with wrong number of operands");
2868 // Check to see if there is no change.
2869 bool AnyChange = false;
2870 for (unsigned i = 0; i != NumOps; ++i) {
2871 if (Ops[i] != N->getOperand(i)) {
2877 // No operands changed, just return the input node.
2878 if (!AnyChange) return InN;
2880 // See if the modified node already exists.
2881 void *InsertPos = 0;
2882 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
2883 return SDOperand(Existing, InN.ResNo);
2885 // Nope it doesn't. Remove the node from it's current place in the maps.
2887 RemoveNodeFromCSEMaps(N);
2889 // Now we update the operands.
2890 for (unsigned i = 0; i != NumOps; ++i) {
2891 if (N->OperandList[i] != Ops[i]) {
2892 N->OperandList[i].Val->removeUser(N);
2893 Ops[i].Val->addUser(N);
2894 N->OperandList[i] = Ops[i];
2898 // If this gets put into a CSE map, add it.
2899 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2904 /// MorphNodeTo - This frees the operands of the current node, resets the
2905 /// opcode, types, and operands to the specified value. This should only be
2906 /// used by the SelectionDAG class.
2907 void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2908 const SDOperand *Ops, unsigned NumOps) {
2911 NumValues = L.NumVTs;
2913 // Clear the operands list, updating used nodes to remove this from their
2915 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2916 I->Val->removeUser(this);
2918 // If NumOps is larger than the # of operands we currently have, reallocate
2919 // the operand list.
2920 if (NumOps > NumOperands) {
2921 if (OperandsNeedDelete)
2922 delete [] OperandList;
2923 OperandList = new SDOperand[NumOps];
2924 OperandsNeedDelete = true;
2927 // Assign the new operands.
2928 NumOperands = NumOps;
2930 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2931 OperandList[i] = Ops[i];
2932 SDNode *N = OperandList[i].Val;
2933 N->Uses.push_back(this);
2937 /// SelectNodeTo - These are used for target selectors to *mutate* the
2938 /// specified node to have the specified return type, Target opcode, and
2939 /// operands. Note that target opcodes are stored as
2940 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
2942 /// Note that SelectNodeTo returns the resultant node. If there is already a
2943 /// node of the specified opcode and operands, it returns that node instead of
2944 /// the current one.
2945 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2946 MVT::ValueType VT) {
2947 SDVTList VTs = getVTList(VT);
2948 FoldingSetNodeID ID;
2949 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
2951 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2954 RemoveNodeFromCSEMaps(N);
2956 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
2958 CSEMap.InsertNode(N, IP);
2962 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2963 MVT::ValueType VT, SDOperand Op1) {
2964 // If an identical node already exists, use it.
2965 SDVTList VTs = getVTList(VT);
2966 SDOperand Ops[] = { Op1 };
2968 FoldingSetNodeID ID;
2969 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
2971 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2974 RemoveNodeFromCSEMaps(N);
2975 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
2976 CSEMap.InsertNode(N, IP);
2980 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2981 MVT::ValueType VT, SDOperand Op1,
2983 // If an identical node already exists, use it.
2984 SDVTList VTs = getVTList(VT);
2985 SDOperand Ops[] = { Op1, Op2 };
2987 FoldingSetNodeID ID;
2988 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2990 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2993 RemoveNodeFromCSEMaps(N);
2995 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2997 CSEMap.InsertNode(N, IP); // Memoize the new node.
3001 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3002 MVT::ValueType VT, SDOperand Op1,
3003 SDOperand Op2, SDOperand Op3) {
3004 // If an identical node already exists, use it.
3005 SDVTList VTs = getVTList(VT);
3006 SDOperand Ops[] = { Op1, Op2, Op3 };
3007 FoldingSetNodeID ID;
3008 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3010 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3013 RemoveNodeFromCSEMaps(N);
3015 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3017 CSEMap.InsertNode(N, IP); // Memoize the new node.
3021 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3022 MVT::ValueType VT, const SDOperand *Ops,
3024 // If an identical node already exists, use it.
3025 SDVTList VTs = getVTList(VT);
3026 FoldingSetNodeID ID;
3027 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
3029 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3032 RemoveNodeFromCSEMaps(N);
3033 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
3035 CSEMap.InsertNode(N, IP); // Memoize the new node.
3039 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3040 MVT::ValueType VT1, MVT::ValueType VT2,
3041 SDOperand Op1, SDOperand Op2) {
3042 SDVTList VTs = getVTList(VT1, VT2);
3043 FoldingSetNodeID ID;
3044 SDOperand Ops[] = { Op1, Op2 };
3045 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3047 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3050 RemoveNodeFromCSEMaps(N);
3051 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3052 CSEMap.InsertNode(N, IP); // Memoize the new node.
3056 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3057 MVT::ValueType VT1, MVT::ValueType VT2,
3058 SDOperand Op1, SDOperand Op2,
3060 // If an identical node already exists, use it.
3061 SDVTList VTs = getVTList(VT1, VT2);
3062 SDOperand Ops[] = { Op1, Op2, Op3 };
3063 FoldingSetNodeID ID;
3064 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3066 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3069 RemoveNodeFromCSEMaps(N);
3071 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3072 CSEMap.InsertNode(N, IP); // Memoize the new node.
3077 /// getTargetNode - These are used for target selectors to create a new node
3078 /// with specified return type(s), target opcode, and operands.
3080 /// Note that getTargetNode returns the resultant node. If there is already a
3081 /// node of the specified opcode and operands, it returns that node instead of
3082 /// the current one.
3083 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3084 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3086 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3088 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3090 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3091 SDOperand Op1, SDOperand Op2) {
3092 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3094 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3095 SDOperand Op1, SDOperand Op2,
3097 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3099 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3100 const SDOperand *Ops, unsigned NumOps) {
3101 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
3103 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3104 MVT::ValueType VT2) {
3105 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3107 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3109 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3110 MVT::ValueType VT2, SDOperand Op1) {
3111 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3112 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
3114 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3115 MVT::ValueType VT2, SDOperand Op1,
3117 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3118 SDOperand Ops[] = { Op1, Op2 };
3119 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
3121 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3122 MVT::ValueType VT2, SDOperand Op1,
3123 SDOperand Op2, SDOperand Op3) {
3124 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3125 SDOperand Ops[] = { Op1, Op2, Op3 };
3126 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
3128 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3130 const SDOperand *Ops, unsigned NumOps) {
3131 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3132 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
3134 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3135 MVT::ValueType VT2, MVT::ValueType VT3,
3136 SDOperand Op1, SDOperand Op2) {
3137 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3138 SDOperand Ops[] = { Op1, Op2 };
3139 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
3141 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3142 MVT::ValueType VT2, MVT::ValueType VT3,
3143 SDOperand Op1, SDOperand Op2,
3145 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3146 SDOperand Ops[] = { Op1, Op2, Op3 };
3147 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3149 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3150 MVT::ValueType VT2, MVT::ValueType VT3,
3151 const SDOperand *Ops, unsigned NumOps) {
3152 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3153 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
3155 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3156 MVT::ValueType VT2, MVT::ValueType VT3,
3158 const SDOperand *Ops, unsigned NumOps) {
3159 std::vector<MVT::ValueType> VTList;
3160 VTList.push_back(VT1);
3161 VTList.push_back(VT2);
3162 VTList.push_back(VT3);
3163 VTList.push_back(VT4);
3164 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3165 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3167 SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3168 std::vector<MVT::ValueType> &ResultTys,
3169 const SDOperand *Ops, unsigned NumOps) {
3170 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3171 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3175 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3176 /// This can cause recursive merging of nodes in the DAG.
3178 /// This version assumes From/To have a single result value.
3180 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
3181 std::vector<SDNode*> *Deleted) {
3182 SDNode *From = FromN.Val, *To = ToN.Val;
3183 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
3184 "Cannot replace with this method!");
3185 assert(From != To && "Cannot replace uses of with self");
3187 while (!From->use_empty()) {
3188 // Process users until they are all gone.
3189 SDNode *U = *From->use_begin();
3191 // This node is about to morph, remove its old self from the CSE maps.
3192 RemoveNodeFromCSEMaps(U);
3194 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3196 if (I->Val == From) {
3197 From->removeUser(U);
3202 // Now that we have modified U, add it back to the CSE maps. If it already
3203 // exists there, recursively merge the results together.
3204 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3205 ReplaceAllUsesWith(U, Existing, Deleted);
3207 if (Deleted) Deleted->push_back(U);
3208 DeleteNodeNotInCSEMaps(U);
3213 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3214 /// This can cause recursive merging of nodes in the DAG.
3216 /// This version assumes From/To have matching types and numbers of result
3219 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3220 std::vector<SDNode*> *Deleted) {
3221 assert(From != To && "Cannot replace uses of with self");
3222 assert(From->getNumValues() == To->getNumValues() &&
3223 "Cannot use this version of ReplaceAllUsesWith!");
3224 if (From->getNumValues() == 1) { // If possible, use the faster version.
3225 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
3229 while (!From->use_empty()) {
3230 // Process users until they are all gone.
3231 SDNode *U = *From->use_begin();
3233 // This node is about to morph, remove its old self from the CSE maps.
3234 RemoveNodeFromCSEMaps(U);
3236 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3238 if (I->Val == From) {
3239 From->removeUser(U);
3244 // Now that we have modified U, add it back to the CSE maps. If it already
3245 // exists there, recursively merge the results together.
3246 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3247 ReplaceAllUsesWith(U, Existing, Deleted);
3249 if (Deleted) Deleted->push_back(U);
3250 DeleteNodeNotInCSEMaps(U);
3255 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3256 /// This can cause recursive merging of nodes in the DAG.
3258 /// This version can replace From with any result values. To must match the
3259 /// number and types of values returned by From.
3260 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
3261 const SDOperand *To,
3262 std::vector<SDNode*> *Deleted) {
3263 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
3264 // Degenerate case handled above.
3265 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
3269 while (!From->use_empty()) {
3270 // Process users until they are all gone.
3271 SDNode *U = *From->use_begin();
3273 // This node is about to morph, remove its old self from the CSE maps.
3274 RemoveNodeFromCSEMaps(U);
3276 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3278 if (I->Val == From) {
3279 const SDOperand &ToOp = To[I->ResNo];
3280 From->removeUser(U);
3282 ToOp.Val->addUser(U);
3285 // Now that we have modified U, add it back to the CSE maps. If it already
3286 // exists there, recursively merge the results together.
3287 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3288 ReplaceAllUsesWith(U, Existing, Deleted);
3290 if (Deleted) Deleted->push_back(U);
3291 DeleteNodeNotInCSEMaps(U);
3296 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3297 /// uses of other values produced by From.Val alone. The Deleted vector is
3298 /// handled the same was as for ReplaceAllUsesWith.
3299 void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
3300 std::vector<SDNode*> *Deleted) {
3301 assert(From != To && "Cannot replace a value with itself");
3302 // Handle the simple, trivial, case efficiently.
3303 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
3304 ReplaceAllUsesWith(From, To, Deleted);
3308 // Get all of the users of From.Val. We want these in a nice,
3309 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3310 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
3312 std::vector<SDNode*> LocalDeletionVector;
3314 // Pick a deletion vector to use. If the user specified one, use theirs,
3315 // otherwise use a local one.
3316 std::vector<SDNode*> *DeleteVector = Deleted ? Deleted : &LocalDeletionVector;
3317 while (!Users.empty()) {
3318 // We know that this user uses some value of From. If it is the right
3319 // value, update it.
3320 SDNode *User = Users.back();
3323 // Scan for an operand that matches From.
3324 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3325 for (; Op != E; ++Op)
3326 if (*Op == From) break;
3328 // If there are no matches, the user must use some other result of From.
3329 if (Op == E) continue;
3331 // Okay, we know this user needs to be updated. Remove its old self
3332 // from the CSE maps.
3333 RemoveNodeFromCSEMaps(User);
3335 // Update all operands that match "From".
3336 for (; Op != E; ++Op) {
3338 From.Val->removeUser(User);
3340 To.Val->addUser(User);
3344 // Now that we have modified User, add it back to the CSE maps. If it
3345 // already exists there, recursively merge the results together.
3346 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
3347 if (!Existing) continue; // Continue on to next user.
3349 // If there was already an existing matching node, use ReplaceAllUsesWith
3350 // to replace the dead one with the existing one. However, this can cause
3351 // recursive merging of other unrelated nodes down the line. The merging
3352 // can cause deletion of nodes that used the old value. In this case,
3353 // we have to be certain to remove them from the Users set.
3354 unsigned NumDeleted = DeleteVector->size();
3355 ReplaceAllUsesWith(User, Existing, DeleteVector);
3357 // User is now dead.
3358 DeleteVector->push_back(User);
3359 DeleteNodeNotInCSEMaps(User);
3361 // We have to be careful here, because ReplaceAllUsesWith could have
3362 // deleted a user of From, which means there may be dangling pointers
3363 // in the "Users" setvector. Scan over the deleted node pointers and
3364 // remove them from the setvector.
3365 for (unsigned i = NumDeleted, e = DeleteVector->size(); i != e; ++i)
3366 Users.remove((*DeleteVector)[i]);
3368 // If the user doesn't need the set of deleted elements, don't retain them
3369 // to the next loop iteration.
3371 LocalDeletionVector.clear();
3376 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3377 /// their allnodes order. It returns the maximum id.
3378 unsigned SelectionDAG::AssignNodeIds() {
3380 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3387 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
3388 /// based on their topological order. It returns the maximum id and a vector
3389 /// of the SDNodes* in assigned order by reference.
3390 unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
3391 unsigned DAGSize = AllNodes.size();
3392 std::vector<unsigned> InDegree(DAGSize);
3393 std::vector<SDNode*> Sources;
3395 // Use a two pass approach to avoid using a std::map which is slow.
3397 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3400 unsigned Degree = N->use_size();
3401 InDegree[N->getNodeId()] = Degree;
3403 Sources.push_back(N);
3407 while (!Sources.empty()) {
3408 SDNode *N = Sources.back();
3410 TopOrder.push_back(N);
3411 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3413 unsigned Degree = --InDegree[P->getNodeId()];
3415 Sources.push_back(P);
3419 // Second pass, assign the actual topological order as node ids.
3421 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3423 (*TI)->setNodeId(Id++);
3430 //===----------------------------------------------------------------------===//
3432 //===----------------------------------------------------------------------===//
3434 // Out-of-line virtual method to give class a home.
3435 void SDNode::ANCHOR() {}
3436 void UnarySDNode::ANCHOR() {}
3437 void BinarySDNode::ANCHOR() {}
3438 void TernarySDNode::ANCHOR() {}
3439 void HandleSDNode::ANCHOR() {}
3440 void StringSDNode::ANCHOR() {}
3441 void ConstantSDNode::ANCHOR() {}
3442 void ConstantFPSDNode::ANCHOR() {}
3443 void GlobalAddressSDNode::ANCHOR() {}
3444 void FrameIndexSDNode::ANCHOR() {}
3445 void JumpTableSDNode::ANCHOR() {}
3446 void ConstantPoolSDNode::ANCHOR() {}
3447 void BasicBlockSDNode::ANCHOR() {}
3448 void SrcValueSDNode::ANCHOR() {}
3449 void RegisterSDNode::ANCHOR() {}
3450 void ExternalSymbolSDNode::ANCHOR() {}
3451 void CondCodeSDNode::ANCHOR() {}
3452 void VTSDNode::ANCHOR() {}
3453 void LoadSDNode::ANCHOR() {}
3454 void StoreSDNode::ANCHOR() {}
3456 HandleSDNode::~HandleSDNode() {
3457 SDVTList VTs = { 0, 0 };
3458 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
3461 GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3462 MVT::ValueType VT, int o)
3463 : SDNode(isa<GlobalVariable>(GA) &&
3464 cast<GlobalVariable>(GA)->isThreadLocal() ?
3466 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3468 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3469 getSDVTList(VT)), Offset(o) {
3470 TheGlobal = const_cast<GlobalValue*>(GA);
3473 /// Profile - Gather unique data for the node.
3475 void SDNode::Profile(FoldingSetNodeID &ID) {
3476 AddNodeIDNode(ID, this);
3479 /// getValueTypeList - Return a pointer to the specified value type.
3481 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
3482 if (MVT::isExtendedVT(VT)) {
3483 static std::set<MVT::ValueType> EVTs;
3484 return (MVT::ValueType *)&(*EVTs.insert(VT).first);
3486 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3492 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3493 /// indicated value. This method ignores uses of other values defined by this
3495 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
3496 assert(Value < getNumValues() && "Bad value!");
3498 // If there is only one value, this is easy.
3499 if (getNumValues() == 1)
3500 return use_size() == NUses;
3501 if (use_size() < NUses) return false;
3503 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3505 SmallPtrSet<SDNode*, 32> UsersHandled;
3507 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3509 if (User->getNumOperands() == 1 ||
3510 UsersHandled.insert(User)) // First time we've seen this?
3511 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3512 if (User->getOperand(i) == TheValue) {
3514 return false; // too many uses
3519 // Found exactly the right number of uses?
3524 /// hasAnyUseOfValue - Return true if there are any use of the indicated
3525 /// value. This method ignores uses of other values defined by this operation.
3526 bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3527 assert(Value < getNumValues() && "Bad value!");
3529 if (use_size() == 0) return false;
3531 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3533 SmallPtrSet<SDNode*, 32> UsersHandled;
3535 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3537 if (User->getNumOperands() == 1 ||
3538 UsersHandled.insert(User)) // First time we've seen this?
3539 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3540 if (User->getOperand(i) == TheValue) {
3549 /// isOnlyUse - Return true if this node is the only use of N.
3551 bool SDNode::isOnlyUse(SDNode *N) const {
3553 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3564 /// isOperand - Return true if this node is an operand of N.
3566 bool SDOperand::isOperand(SDNode *N) const {
3567 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3568 if (*this == N->getOperand(i))
3573 bool SDNode::isOperand(SDNode *N) const {
3574 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3575 if (this == N->OperandList[i].Val)
3580 static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
3581 SmallPtrSet<SDNode *, 32> &Visited) {
3582 if (found || !Visited.insert(N))
3585 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3586 SDNode *Op = N->getOperand(i).Val;
3591 findPredecessor(Op, P, found, Visited);
3595 /// isPredecessor - Return true if this node is a predecessor of N. This node
3596 /// is either an operand of N or it can be reached by recursively traversing
3597 /// up the operands.
3598 /// NOTE: this is an expensive method. Use it carefully.
3599 bool SDNode::isPredecessor(SDNode *N) const {
3600 SmallPtrSet<SDNode *, 32> Visited;
3602 findPredecessor(N, this, found, Visited);
3606 uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3607 assert(Num < NumOperands && "Invalid child # of SDNode!");
3608 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3611 std::string SDNode::getOperationName(const SelectionDAG *G) const {
3612 switch (getOpcode()) {
3614 if (getOpcode() < ISD::BUILTIN_OP_END)
3615 return "<<Unknown DAG Node>>";
3618 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
3619 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
3620 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
3622 TargetLowering &TLI = G->getTargetLoweringInfo();
3624 TLI.getTargetNodeName(getOpcode());
3625 if (Name) return Name;
3628 return "<<Unknown Target Node>>";
3631 case ISD::PCMARKER: return "PCMarker";
3632 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
3633 case ISD::SRCVALUE: return "SrcValue";
3634 case ISD::EntryToken: return "EntryToken";
3635 case ISD::TokenFactor: return "TokenFactor";
3636 case ISD::AssertSext: return "AssertSext";
3637 case ISD::AssertZext: return "AssertZext";
3639 case ISD::STRING: return "String";
3640 case ISD::BasicBlock: return "BasicBlock";
3641 case ISD::VALUETYPE: return "ValueType";
3642 case ISD::Register: return "Register";
3644 case ISD::Constant: return "Constant";
3645 case ISD::ConstantFP: return "ConstantFP";
3646 case ISD::GlobalAddress: return "GlobalAddress";
3647 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
3648 case ISD::FrameIndex: return "FrameIndex";
3649 case ISD::JumpTable: return "JumpTable";
3650 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
3651 case ISD::RETURNADDR: return "RETURNADDR";
3652 case ISD::FRAMEADDR: return "FRAMEADDR";
3653 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
3654 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3655 case ISD::EHSELECTION: return "EHSELECTION";
3656 case ISD::EH_RETURN: return "EH_RETURN";
3657 case ISD::ConstantPool: return "ConstantPool";
3658 case ISD::ExternalSymbol: return "ExternalSymbol";
3659 case ISD::INTRINSIC_WO_CHAIN: {
3660 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3661 return Intrinsic::getName((Intrinsic::ID)IID);
3663 case ISD::INTRINSIC_VOID:
3664 case ISD::INTRINSIC_W_CHAIN: {
3665 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
3666 return Intrinsic::getName((Intrinsic::ID)IID);
3669 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
3670 case ISD::TargetConstant: return "TargetConstant";
3671 case ISD::TargetConstantFP:return "TargetConstantFP";
3672 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
3673 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
3674 case ISD::TargetFrameIndex: return "TargetFrameIndex";
3675 case ISD::TargetJumpTable: return "TargetJumpTable";
3676 case ISD::TargetConstantPool: return "TargetConstantPool";
3677 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
3679 case ISD::CopyToReg: return "CopyToReg";
3680 case ISD::CopyFromReg: return "CopyFromReg";
3681 case ISD::UNDEF: return "undef";
3682 case ISD::MERGE_VALUES: return "merge_values";
3683 case ISD::INLINEASM: return "inlineasm";
3684 case ISD::LABEL: return "label";
3685 case ISD::HANDLENODE: return "handlenode";
3686 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
3687 case ISD::CALL: return "call";
3690 case ISD::FABS: return "fabs";
3691 case ISD::FNEG: return "fneg";
3692 case ISD::FSQRT: return "fsqrt";
3693 case ISD::FSIN: return "fsin";
3694 case ISD::FCOS: return "fcos";
3695 case ISD::FPOWI: return "fpowi";
3696 case ISD::FPOW: return "fpow";
3699 case ISD::ADD: return "add";
3700 case ISD::SUB: return "sub";
3701 case ISD::MUL: return "mul";
3702 case ISD::MULHU: return "mulhu";
3703 case ISD::MULHS: return "mulhs";
3704 case ISD::SDIV: return "sdiv";
3705 case ISD::UDIV: return "udiv";
3706 case ISD::SREM: return "srem";
3707 case ISD::UREM: return "urem";
3708 case ISD::SMUL_LOHI: return "smul_lohi";
3709 case ISD::UMUL_LOHI: return "umul_lohi";
3710 case ISD::SDIVREM: return "sdivrem";
3711 case ISD::UDIVREM: return "divrem";
3712 case ISD::AND: return "and";
3713 case ISD::OR: return "or";
3714 case ISD::XOR: return "xor";
3715 case ISD::SHL: return "shl";
3716 case ISD::SRA: return "sra";
3717 case ISD::SRL: return "srl";
3718 case ISD::ROTL: return "rotl";
3719 case ISD::ROTR: return "rotr";
3720 case ISD::FADD: return "fadd";
3721 case ISD::FSUB: return "fsub";
3722 case ISD::FMUL: return "fmul";
3723 case ISD::FDIV: return "fdiv";
3724 case ISD::FREM: return "frem";
3725 case ISD::FCOPYSIGN: return "fcopysign";
3727 case ISD::SETCC: return "setcc";
3728 case ISD::SELECT: return "select";
3729 case ISD::SELECT_CC: return "select_cc";
3730 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
3731 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
3732 case ISD::CONCAT_VECTORS: return "concat_vectors";
3733 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
3734 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
3735 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
3736 case ISD::CARRY_FALSE: return "carry_false";
3737 case ISD::ADDC: return "addc";
3738 case ISD::ADDE: return "adde";
3739 case ISD::SUBC: return "subc";
3740 case ISD::SUBE: return "sube";
3741 case ISD::SHL_PARTS: return "shl_parts";
3742 case ISD::SRA_PARTS: return "sra_parts";
3743 case ISD::SRL_PARTS: return "srl_parts";
3745 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3746 case ISD::INSERT_SUBREG: return "insert_subreg";
3748 // Conversion operators.
3749 case ISD::SIGN_EXTEND: return "sign_extend";
3750 case ISD::ZERO_EXTEND: return "zero_extend";
3751 case ISD::ANY_EXTEND: return "any_extend";
3752 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
3753 case ISD::TRUNCATE: return "truncate";
3754 case ISD::FP_ROUND: return "fp_round";
3755 case ISD::FLT_ROUNDS: return "flt_rounds";
3756 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
3757 case ISD::FP_EXTEND: return "fp_extend";
3759 case ISD::SINT_TO_FP: return "sint_to_fp";
3760 case ISD::UINT_TO_FP: return "uint_to_fp";
3761 case ISD::FP_TO_SINT: return "fp_to_sint";
3762 case ISD::FP_TO_UINT: return "fp_to_uint";
3763 case ISD::BIT_CONVERT: return "bit_convert";
3765 // Control flow instructions
3766 case ISD::BR: return "br";
3767 case ISD::BRIND: return "brind";
3768 case ISD::BR_JT: return "br_jt";
3769 case ISD::BRCOND: return "brcond";
3770 case ISD::BR_CC: return "br_cc";
3771 case ISD::RET: return "ret";
3772 case ISD::CALLSEQ_START: return "callseq_start";
3773 case ISD::CALLSEQ_END: return "callseq_end";
3776 case ISD::LOAD: return "load";
3777 case ISD::STORE: return "store";
3778 case ISD::VAARG: return "vaarg";
3779 case ISD::VACOPY: return "vacopy";
3780 case ISD::VAEND: return "vaend";
3781 case ISD::VASTART: return "vastart";
3782 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
3783 case ISD::EXTRACT_ELEMENT: return "extract_element";
3784 case ISD::BUILD_PAIR: return "build_pair";
3785 case ISD::STACKSAVE: return "stacksave";
3786 case ISD::STACKRESTORE: return "stackrestore";
3788 // Block memory operations.
3789 case ISD::MEMSET: return "memset";
3790 case ISD::MEMCPY: return "memcpy";
3791 case ISD::MEMMOVE: return "memmove";
3794 case ISD::BSWAP: return "bswap";
3795 case ISD::CTPOP: return "ctpop";
3796 case ISD::CTTZ: return "cttz";
3797 case ISD::CTLZ: return "ctlz";
3800 case ISD::LOCATION: return "location";
3801 case ISD::DEBUG_LOC: return "debug_loc";
3804 case ISD::TRAMPOLINE: return "trampoline";
3807 switch (cast<CondCodeSDNode>(this)->get()) {
3808 default: assert(0 && "Unknown setcc condition!");
3809 case ISD::SETOEQ: return "setoeq";
3810 case ISD::SETOGT: return "setogt";
3811 case ISD::SETOGE: return "setoge";
3812 case ISD::SETOLT: return "setolt";
3813 case ISD::SETOLE: return "setole";
3814 case ISD::SETONE: return "setone";
3816 case ISD::SETO: return "seto";
3817 case ISD::SETUO: return "setuo";
3818 case ISD::SETUEQ: return "setue";
3819 case ISD::SETUGT: return "setugt";
3820 case ISD::SETUGE: return "setuge";
3821 case ISD::SETULT: return "setult";
3822 case ISD::SETULE: return "setule";
3823 case ISD::SETUNE: return "setune";
3825 case ISD::SETEQ: return "seteq";
3826 case ISD::SETGT: return "setgt";
3827 case ISD::SETGE: return "setge";
3828 case ISD::SETLT: return "setlt";
3829 case ISD::SETLE: return "setle";
3830 case ISD::SETNE: return "setne";
3835 const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
3844 return "<post-inc>";
3846 return "<post-dec>";
3850 void SDNode::dump() const { dump(0); }
3851 void SDNode::dump(const SelectionDAG *G) const {
3852 cerr << (void*)this << ": ";
3854 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
3856 if (getValueType(i) == MVT::Other)
3859 cerr << MVT::getValueTypeString(getValueType(i));
3861 cerr << " = " << getOperationName(G);
3864 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
3865 if (i) cerr << ", ";
3866 cerr << (void*)getOperand(i).Val;
3867 if (unsigned RN = getOperand(i).ResNo)
3871 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
3872 SDNode *Mask = getOperand(2).Val;
3874 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
3876 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
3879 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
3884 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
3885 cerr << "<" << CSDN->getValue() << ">";
3886 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
3887 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
3888 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
3889 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
3890 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
3892 cerr << "<APFloat(";
3893 CSDN->getValueAPF().convertToAPInt().dump();
3896 } else if (const GlobalAddressSDNode *GADN =
3897 dyn_cast<GlobalAddressSDNode>(this)) {
3898 int offset = GADN->getOffset();
3900 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
3902 cerr << " + " << offset;
3904 cerr << " " << offset;
3905 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
3906 cerr << "<" << FIDN->getIndex() << ">";
3907 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
3908 cerr << "<" << JTDN->getIndex() << ">";
3909 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
3910 int offset = CP->getOffset();
3911 if (CP->isMachineConstantPoolEntry())
3912 cerr << "<" << *CP->getMachineCPVal() << ">";
3914 cerr << "<" << *CP->getConstVal() << ">";
3916 cerr << " + " << offset;
3918 cerr << " " << offset;
3919 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
3921 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3923 cerr << LBB->getName() << " ";
3924 cerr << (const void*)BBDN->getBasicBlock() << ">";
3925 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
3926 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
3927 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
3929 cerr << " #" << R->getReg();
3931 } else if (const ExternalSymbolSDNode *ES =
3932 dyn_cast<ExternalSymbolSDNode>(this)) {
3933 cerr << "'" << ES->getSymbol() << "'";
3934 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3936 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
3938 cerr << "<null:" << M->getOffset() << ">";
3939 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
3940 cerr << ":" << MVT::getValueTypeString(N->getVT());
3941 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
3943 switch (LD->getExtensionType()) {
3944 default: doExt = false; break;
3946 cerr << " <anyext ";
3956 cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
3958 const char *AM = getIndexedModeName(LD->getAddressingMode());
3961 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
3962 if (ST->isTruncatingStore())
3964 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
3966 const char *AM = getIndexedModeName(ST->getAddressingMode());
3972 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
3973 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3974 if (N->getOperand(i).Val->hasOneUse())
3975 DumpNodes(N->getOperand(i).Val, indent+2, G);
3977 cerr << "\n" << std::string(indent+2, ' ')
3978 << (void*)N->getOperand(i).Val << ": <multiple use>";
3981 cerr << "\n" << std::string(indent, ' ');
3985 void SelectionDAG::dump() const {
3986 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
3987 std::vector<const SDNode*> Nodes;
3988 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3992 std::sort(Nodes.begin(), Nodes.end());
3994 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
3995 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
3996 DumpNodes(Nodes[i], 2, this);
3999 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
4004 const Type *ConstantPoolSDNode::getType() const {
4005 if (isMachineConstantPoolEntry())
4006 return Val.MachineCPVal->getType();
4007 return Val.ConstVal->getType();