1 //===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements the SelectionDAG class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/SelectionDAG.h"
15 #include "llvm/Constants.h"
16 #include "llvm/GlobalVariable.h"
17 #include "llvm/Intrinsics.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Assembly/Writer.h"
20 #include "llvm/CodeGen/MachineBasicBlock.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Target/MRegisterInfo.h"
24 #include "llvm/Target/TargetData.h"
25 #include "llvm/Target/TargetLowering.h"
26 #include "llvm/Target/TargetInstrInfo.h"
27 #include "llvm/Target/TargetMachine.h"
28 #include "llvm/ADT/SetVector.h"
29 #include "llvm/ADT/SmallPtrSet.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/ADT/StringExtras.h"
36 /// makeVTList - Return an instance of the SDVTList struct initialized with the
37 /// specified members.
38 static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
39 SDVTList Res = {VTs, NumVTs};
43 //===----------------------------------------------------------------------===//
44 // ConstantFPSDNode Class
45 //===----------------------------------------------------------------------===//
47 /// isExactlyValue - We don't rely on operator== working on double values, as
48 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
49 /// As such, this method can be used to do an exact bit-for-bit comparison of
50 /// two floating point values.
51 bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
52 return Value.bitwiseIsEqual(V);
55 bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
57 // convert modifies in place, so make a copy.
58 APFloat Val2 = APFloat(Val);
61 return false; // These can't be represented as floating point!
63 // FIXME rounding mode needs to be more flexible
65 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
66 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) ==
69 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
70 &Val2.getSemantics() == &APFloat::IEEEdouble ||
71 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
73 // TODO: Figure out how to test if we can use a shorter type instead!
81 //===----------------------------------------------------------------------===//
83 //===----------------------------------------------------------------------===//
85 /// isBuildVectorAllOnes - Return true if the specified node is a
86 /// BUILD_VECTOR where all of the elements are ~0 or undef.
87 bool ISD::isBuildVectorAllOnes(const SDNode *N) {
88 // Look through a bit convert.
89 if (N->getOpcode() == ISD::BIT_CONVERT)
90 N = N->getOperand(0).Val;
92 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
94 unsigned i = 0, e = N->getNumOperands();
96 // Skip over all of the undef values.
97 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
100 // Do not accept an all-undef vector.
101 if (i == e) return false;
103 // Do not accept build_vectors that aren't all constants or which have non-~0
105 SDOperand NotZero = N->getOperand(i);
106 if (isa<ConstantSDNode>(NotZero)) {
107 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
109 } else if (isa<ConstantFPSDNode>(NotZero)) {
110 MVT::ValueType VT = NotZero.getValueType();
112 if (((cast<ConstantFPSDNode>(NotZero)->getValueAPF().
113 convertToAPInt().getZExtValue())) != (uint64_t)-1)
116 if ((uint32_t)cast<ConstantFPSDNode>(NotZero)->
117 getValueAPF().convertToAPInt().getZExtValue() !=
124 // Okay, we have at least one ~0 value, check to see if the rest match or are
126 for (++i; i != e; ++i)
127 if (N->getOperand(i) != NotZero &&
128 N->getOperand(i).getOpcode() != ISD::UNDEF)
134 /// isBuildVectorAllZeros - Return true if the specified node is a
135 /// BUILD_VECTOR where all of the elements are 0 or undef.
136 bool ISD::isBuildVectorAllZeros(const SDNode *N) {
137 // Look through a bit convert.
138 if (N->getOpcode() == ISD::BIT_CONVERT)
139 N = N->getOperand(0).Val;
141 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
143 unsigned i = 0, e = N->getNumOperands();
145 // Skip over all of the undef values.
146 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
149 // Do not accept an all-undef vector.
150 if (i == e) return false;
152 // Do not accept build_vectors that aren't all constants or which have non-~0
154 SDOperand Zero = N->getOperand(i);
155 if (isa<ConstantSDNode>(Zero)) {
156 if (!cast<ConstantSDNode>(Zero)->isNullValue())
158 } else if (isa<ConstantFPSDNode>(Zero)) {
159 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
164 // Okay, we have at least one ~0 value, check to see if the rest match or are
166 for (++i; i != e; ++i)
167 if (N->getOperand(i) != Zero &&
168 N->getOperand(i).getOpcode() != ISD::UNDEF)
173 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
174 /// when given the operation for (X op Y).
175 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
176 // To perform this operation, we just need to swap the L and G bits of the
178 unsigned OldL = (Operation >> 2) & 1;
179 unsigned OldG = (Operation >> 1) & 1;
180 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
181 (OldL << 1) | // New G bit
182 (OldG << 2)); // New L bit.
185 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
186 /// 'op' is a valid SetCC operation.
187 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
188 unsigned Operation = Op;
190 Operation ^= 7; // Flip L, G, E bits, but not U.
192 Operation ^= 15; // Flip all of the condition bits.
193 if (Operation > ISD::SETTRUE2)
194 Operation &= ~8; // Don't let N and U bits get set.
195 return ISD::CondCode(Operation);
199 /// isSignedOp - For an integer comparison, return 1 if the comparison is a
200 /// signed operation and 2 if the result is an unsigned comparison. Return zero
201 /// if the operation does not depend on the sign of the input (setne and seteq).
202 static int isSignedOp(ISD::CondCode Opcode) {
204 default: assert(0 && "Illegal integer setcc operation!");
206 case ISD::SETNE: return 0;
210 case ISD::SETGE: return 1;
214 case ISD::SETUGE: return 2;
218 /// getSetCCOrOperation - Return the result of a logical OR between different
219 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
220 /// returns SETCC_INVALID if it is not possible to represent the resultant
222 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
224 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
225 // Cannot fold a signed integer setcc with an unsigned integer setcc.
226 return ISD::SETCC_INVALID;
228 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
230 // If the N and U bits get set then the resultant comparison DOES suddenly
231 // care about orderedness, and is true when ordered.
232 if (Op > ISD::SETTRUE2)
233 Op &= ~16; // Clear the U bit if the N bit is set.
235 // Canonicalize illegal integer setcc's.
236 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
239 return ISD::CondCode(Op);
242 /// getSetCCAndOperation - Return the result of a logical AND between different
243 /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
244 /// function returns zero if it is not possible to represent the resultant
246 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
248 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
249 // Cannot fold a signed setcc with an unsigned setcc.
250 return ISD::SETCC_INVALID;
252 // Combine all of the condition bits.
253 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
255 // Canonicalize illegal integer setcc's.
259 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
260 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
261 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
262 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
269 const TargetMachine &SelectionDAG::getTarget() const {
270 return TLI.getTargetMachine();
273 //===----------------------------------------------------------------------===//
274 // SDNode Profile Support
275 //===----------------------------------------------------------------------===//
277 /// AddNodeIDOpcode - Add the node opcode to the NodeID data.
279 static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
283 /// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
284 /// solely with their pointer.
285 void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
286 ID.AddPointer(VTList.VTs);
289 /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
291 static void AddNodeIDOperands(FoldingSetNodeID &ID,
292 const SDOperand *Ops, unsigned NumOps) {
293 for (; NumOps; --NumOps, ++Ops) {
294 ID.AddPointer(Ops->Val);
295 ID.AddInteger(Ops->ResNo);
299 static void AddNodeIDNode(FoldingSetNodeID &ID,
300 unsigned short OpC, SDVTList VTList,
301 const SDOperand *OpList, unsigned N) {
302 AddNodeIDOpcode(ID, OpC);
303 AddNodeIDValueTypes(ID, VTList);
304 AddNodeIDOperands(ID, OpList, N);
307 /// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
309 static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
310 AddNodeIDOpcode(ID, N->getOpcode());
311 // Add the return value info.
312 AddNodeIDValueTypes(ID, N->getVTList());
313 // Add the operand info.
314 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
316 // Handle SDNode leafs with special info.
317 switch (N->getOpcode()) {
318 default: break; // Normal nodes don't need extra info.
319 case ISD::TargetConstant:
321 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
323 case ISD::TargetConstantFP:
324 case ISD::ConstantFP: {
325 ID.AddAPFloat(cast<ConstantFPSDNode>(N)->getValueAPF());
328 case ISD::TargetGlobalAddress:
329 case ISD::GlobalAddress:
330 case ISD::TargetGlobalTLSAddress:
331 case ISD::GlobalTLSAddress: {
332 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
333 ID.AddPointer(GA->getGlobal());
334 ID.AddInteger(GA->getOffset());
337 case ISD::BasicBlock:
338 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
341 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
343 case ISD::SRCVALUE: {
344 SrcValueSDNode *SV = cast<SrcValueSDNode>(N);
345 ID.AddPointer(SV->getValue());
346 ID.AddInteger(SV->getOffset());
349 case ISD::FrameIndex:
350 case ISD::TargetFrameIndex:
351 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
354 case ISD::TargetJumpTable:
355 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
357 case ISD::ConstantPool:
358 case ISD::TargetConstantPool: {
359 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
360 ID.AddInteger(CP->getAlignment());
361 ID.AddInteger(CP->getOffset());
362 if (CP->isMachineConstantPoolEntry())
363 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
365 ID.AddPointer(CP->getConstVal());
369 LoadSDNode *LD = cast<LoadSDNode>(N);
370 ID.AddInteger(LD->getAddressingMode());
371 ID.AddInteger(LD->getExtensionType());
372 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
373 ID.AddPointer(LD->getSrcValue());
374 ID.AddInteger(LD->getSrcValueOffset());
375 ID.AddInteger(LD->getAlignment());
376 ID.AddInteger(LD->isVolatile());
380 StoreSDNode *ST = cast<StoreSDNode>(N);
381 ID.AddInteger(ST->getAddressingMode());
382 ID.AddInteger(ST->isTruncatingStore());
383 ID.AddInteger((unsigned int)(ST->getStoredVT()));
384 ID.AddPointer(ST->getSrcValue());
385 ID.AddInteger(ST->getSrcValueOffset());
386 ID.AddInteger(ST->getAlignment());
387 ID.AddInteger(ST->isVolatile());
393 //===----------------------------------------------------------------------===//
394 // SelectionDAG Class
395 //===----------------------------------------------------------------------===//
397 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
399 void SelectionDAG::RemoveDeadNodes() {
400 // Create a dummy node (which is not added to allnodes), that adds a reference
401 // to the root node, preventing it from being deleted.
402 HandleSDNode Dummy(getRoot());
404 SmallVector<SDNode*, 128> DeadNodes;
406 // Add all obviously-dead nodes to the DeadNodes worklist.
407 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
409 DeadNodes.push_back(I);
411 // Process the worklist, deleting the nodes and adding their uses to the
413 while (!DeadNodes.empty()) {
414 SDNode *N = DeadNodes.back();
415 DeadNodes.pop_back();
417 // Take the node out of the appropriate CSE map.
418 RemoveNodeFromCSEMaps(N);
420 // Next, brutally remove the operand list. This is safe to do, as there are
421 // no cycles in the graph.
422 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
423 SDNode *Operand = I->Val;
424 Operand->removeUser(N);
426 // Now that we removed this operand, see if there are no uses of it left.
427 if (Operand->use_empty())
428 DeadNodes.push_back(Operand);
430 if (N->OperandsNeedDelete)
431 delete[] N->OperandList;
435 // Finally, remove N itself.
439 // If the root changed (e.g. it was a dead load, update the root).
440 setRoot(Dummy.getValue());
443 void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
444 SmallVector<SDNode*, 16> DeadNodes;
445 DeadNodes.push_back(N);
447 // Process the worklist, deleting the nodes and adding their uses to the
449 while (!DeadNodes.empty()) {
450 SDNode *N = DeadNodes.back();
451 DeadNodes.pop_back();
453 // Take the node out of the appropriate CSE map.
454 RemoveNodeFromCSEMaps(N);
456 // Next, brutally remove the operand list. This is safe to do, as there are
457 // no cycles in the graph.
458 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
459 SDNode *Operand = I->Val;
460 Operand->removeUser(N);
462 // Now that we removed this operand, see if there are no uses of it left.
463 if (Operand->use_empty())
464 DeadNodes.push_back(Operand);
466 if (N->OperandsNeedDelete)
467 delete[] N->OperandList;
471 // Finally, remove N itself.
472 Deleted.push_back(N);
477 void SelectionDAG::DeleteNode(SDNode *N) {
478 assert(N->use_empty() && "Cannot delete a node that is not dead!");
480 // First take this out of the appropriate CSE map.
481 RemoveNodeFromCSEMaps(N);
483 // Finally, remove uses due to operands of this node, remove from the
484 // AllNodes list, and delete the node.
485 DeleteNodeNotInCSEMaps(N);
488 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
490 // Remove it from the AllNodes list.
493 // Drop all of the operands and decrement used nodes use counts.
494 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
495 I->Val->removeUser(N);
496 if (N->OperandsNeedDelete)
497 delete[] N->OperandList;
504 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
505 /// correspond to it. This is useful when we're about to delete or repurpose
506 /// the node. We don't want future request for structurally identical nodes
507 /// to return N anymore.
508 void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
510 switch (N->getOpcode()) {
511 case ISD::HANDLENODE: return; // noop.
513 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
516 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
517 "Cond code doesn't exist!");
518 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
519 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
521 case ISD::ExternalSymbol:
522 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
524 case ISD::TargetExternalSymbol:
526 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
529 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
530 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
533 // Remove it from the CSE Map.
534 Erased = CSEMap.RemoveNode(N);
538 // Verify that the node was actually in one of the CSE maps, unless it has a
539 // flag result (which cannot be CSE'd) or is one of the special cases that are
540 // not subject to CSE.
541 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
542 !N->isTargetOpcode()) {
545 assert(0 && "Node is not in map!");
550 /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
551 /// has been taken out and modified in some way. If the specified node already
552 /// exists in the CSE maps, do not modify the maps, but return the existing node
553 /// instead. If it doesn't exist, add it and return null.
555 SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
556 assert(N->getNumOperands() && "This is a leaf node!");
557 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
558 return 0; // Never add these nodes.
560 // Check that remaining values produced are not flags.
561 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
562 if (N->getValueType(i) == MVT::Flag)
563 return 0; // Never CSE anything that produces a flag.
565 SDNode *New = CSEMap.GetOrInsertNode(N);
566 if (New != N) return New; // Node already existed.
570 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
571 /// were replaced with those specified. If this node is never memoized,
572 /// return null, otherwise return a pointer to the slot it would take. If a
573 /// node already exists with these operands, the slot will be non-null.
574 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
576 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
577 return 0; // Never add these nodes.
579 // Check that remaining values produced are not flags.
580 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
581 if (N->getValueType(i) == MVT::Flag)
582 return 0; // Never CSE anything that produces a flag.
584 SDOperand Ops[] = { Op };
586 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
587 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
590 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
591 /// were replaced with those specified. If this node is never memoized,
592 /// return null, otherwise return a pointer to the slot it would take. If a
593 /// node already exists with these operands, the slot will be non-null.
594 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
595 SDOperand Op1, SDOperand Op2,
597 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
598 return 0; // Never add these nodes.
600 // Check that remaining values produced are not flags.
601 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
602 if (N->getValueType(i) == MVT::Flag)
603 return 0; // Never CSE anything that produces a flag.
605 SDOperand Ops[] = { Op1, Op2 };
607 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
608 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
612 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
613 /// were replaced with those specified. If this node is never memoized,
614 /// return null, otherwise return a pointer to the slot it would take. If a
615 /// node already exists with these operands, the slot will be non-null.
616 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
617 const SDOperand *Ops,unsigned NumOps,
619 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
620 return 0; // Never add these nodes.
622 // Check that remaining values produced are not flags.
623 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
624 if (N->getValueType(i) == MVT::Flag)
625 return 0; // Never CSE anything that produces a flag.
628 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
630 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
631 ID.AddInteger(LD->getAddressingMode());
632 ID.AddInteger(LD->getExtensionType());
633 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
634 ID.AddPointer(LD->getSrcValue());
635 ID.AddInteger(LD->getSrcValueOffset());
636 ID.AddInteger(LD->getAlignment());
637 ID.AddInteger(LD->isVolatile());
638 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
639 ID.AddInteger(ST->getAddressingMode());
640 ID.AddInteger(ST->isTruncatingStore());
641 ID.AddInteger((unsigned int)(ST->getStoredVT()));
642 ID.AddPointer(ST->getSrcValue());
643 ID.AddInteger(ST->getSrcValueOffset());
644 ID.AddInteger(ST->getAlignment());
645 ID.AddInteger(ST->isVolatile());
648 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
652 SelectionDAG::~SelectionDAG() {
653 while (!AllNodes.empty()) {
654 SDNode *N = AllNodes.begin();
655 N->SetNextInBucket(0);
656 if (N->OperandsNeedDelete)
657 delete [] N->OperandList;
660 AllNodes.pop_front();
664 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
665 if (Op.getValueType() == VT) return Op;
666 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
667 return getNode(ISD::AND, Op.getValueType(), Op,
668 getConstant(Imm, Op.getValueType()));
671 SDOperand SelectionDAG::getString(const std::string &Val) {
672 StringSDNode *&N = StringNodes[Val];
674 N = new StringSDNode(Val);
675 AllNodes.push_back(N);
677 return SDOperand(N, 0);
680 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
681 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
682 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
684 // Mask out any bits that are not valid for this constant.
685 Val &= MVT::getIntVTBitMask(VT);
687 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
689 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
692 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
693 return SDOperand(E, 0);
694 SDNode *N = new ConstantSDNode(isT, Val, VT);
695 CSEMap.InsertNode(N, IP);
696 AllNodes.push_back(N);
697 return SDOperand(N, 0);
700 SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
702 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
704 MVT::ValueType EltVT =
705 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
707 // Do the map lookup using the actual bit pattern for the floating point
708 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
709 // we don't have issues with SNANs.
710 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
712 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
716 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
717 if (!MVT::isVector(VT))
718 return SDOperand(N, 0);
720 N = new ConstantFPSDNode(isTarget, V, EltVT);
721 CSEMap.InsertNode(N, IP);
722 AllNodes.push_back(N);
725 SDOperand Result(N, 0);
726 if (MVT::isVector(VT)) {
727 SmallVector<SDOperand, 8> Ops;
728 Ops.assign(MVT::getVectorNumElements(VT), Result);
729 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
734 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
736 MVT::ValueType EltVT =
737 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
739 return getConstantFP(APFloat((float)Val), VT, isTarget);
741 return getConstantFP(APFloat(Val), VT, isTarget);
744 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
745 MVT::ValueType VT, int Offset,
747 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
749 if (GVar && GVar->isThreadLocal())
750 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
752 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
754 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
756 ID.AddInteger(Offset);
758 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
759 return SDOperand(E, 0);
760 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
761 CSEMap.InsertNode(N, IP);
762 AllNodes.push_back(N);
763 return SDOperand(N, 0);
766 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
768 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
770 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
773 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
774 return SDOperand(E, 0);
775 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
776 CSEMap.InsertNode(N, IP);
777 AllNodes.push_back(N);
778 return SDOperand(N, 0);
781 SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
782 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
784 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
787 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
788 return SDOperand(E, 0);
789 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
790 CSEMap.InsertNode(N, IP);
791 AllNodes.push_back(N);
792 return SDOperand(N, 0);
795 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
796 unsigned Alignment, int Offset,
798 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
800 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
801 ID.AddInteger(Alignment);
802 ID.AddInteger(Offset);
805 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
806 return SDOperand(E, 0);
807 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
808 CSEMap.InsertNode(N, IP);
809 AllNodes.push_back(N);
810 return SDOperand(N, 0);
814 SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
816 unsigned Alignment, int Offset,
818 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
820 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
821 ID.AddInteger(Alignment);
822 ID.AddInteger(Offset);
823 C->AddSelectionDAGCSEId(ID);
825 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
826 return SDOperand(E, 0);
827 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
828 CSEMap.InsertNode(N, IP);
829 AllNodes.push_back(N);
830 return SDOperand(N, 0);
834 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
836 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
839 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
840 return SDOperand(E, 0);
841 SDNode *N = new BasicBlockSDNode(MBB);
842 CSEMap.InsertNode(N, IP);
843 AllNodes.push_back(N);
844 return SDOperand(N, 0);
847 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
848 if ((unsigned)VT >= ValueTypeNodes.size())
849 ValueTypeNodes.resize(VT+1);
850 if (ValueTypeNodes[VT] == 0) {
851 ValueTypeNodes[VT] = new VTSDNode(VT);
852 AllNodes.push_back(ValueTypeNodes[VT]);
855 return SDOperand(ValueTypeNodes[VT], 0);
858 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
859 SDNode *&N = ExternalSymbols[Sym];
860 if (N) return SDOperand(N, 0);
861 N = new ExternalSymbolSDNode(false, Sym, VT);
862 AllNodes.push_back(N);
863 return SDOperand(N, 0);
866 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
868 SDNode *&N = TargetExternalSymbols[Sym];
869 if (N) return SDOperand(N, 0);
870 N = new ExternalSymbolSDNode(true, Sym, VT);
871 AllNodes.push_back(N);
872 return SDOperand(N, 0);
875 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
876 if ((unsigned)Cond >= CondCodeNodes.size())
877 CondCodeNodes.resize(Cond+1);
879 if (CondCodeNodes[Cond] == 0) {
880 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
881 AllNodes.push_back(CondCodeNodes[Cond]);
883 return SDOperand(CondCodeNodes[Cond], 0);
886 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
888 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
889 ID.AddInteger(RegNo);
891 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
892 return SDOperand(E, 0);
893 SDNode *N = new RegisterSDNode(RegNo, VT);
894 CSEMap.InsertNode(N, IP);
895 AllNodes.push_back(N);
896 return SDOperand(N, 0);
899 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
900 assert((!V || isa<PointerType>(V->getType())) &&
901 "SrcValue is not a pointer?");
904 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
906 ID.AddInteger(Offset);
908 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
909 return SDOperand(E, 0);
910 SDNode *N = new SrcValueSDNode(V, Offset);
911 CSEMap.InsertNode(N, IP);
912 AllNodes.push_back(N);
913 return SDOperand(N, 0);
916 SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
917 SDOperand N2, ISD::CondCode Cond) {
918 // These setcc operations always fold.
922 case ISD::SETFALSE2: return getConstant(0, VT);
924 case ISD::SETTRUE2: return getConstant(1, VT);
936 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
940 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
941 uint64_t C2 = N2C->getValue();
942 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
943 uint64_t C1 = N1C->getValue();
945 // Sign extend the operands if required
946 if (ISD::isSignedIntSetCC(Cond)) {
947 C1 = N1C->getSignExtended();
948 C2 = N2C->getSignExtended();
952 default: assert(0 && "Unknown integer setcc!");
953 case ISD::SETEQ: return getConstant(C1 == C2, VT);
954 case ISD::SETNE: return getConstant(C1 != C2, VT);
955 case ISD::SETULT: return getConstant(C1 < C2, VT);
956 case ISD::SETUGT: return getConstant(C1 > C2, VT);
957 case ISD::SETULE: return getConstant(C1 <= C2, VT);
958 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
959 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
960 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
961 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
962 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
966 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
967 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
968 // No compile time operations on this type yet.
969 if (N1C->getValueType(0) == MVT::ppcf128)
972 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
975 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
976 return getNode(ISD::UNDEF, VT);
978 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
979 case ISD::SETNE: if (R==APFloat::cmpUnordered)
980 return getNode(ISD::UNDEF, VT);
982 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
983 R==APFloat::cmpLessThan, VT);
984 case ISD::SETLT: if (R==APFloat::cmpUnordered)
985 return getNode(ISD::UNDEF, VT);
987 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
988 case ISD::SETGT: if (R==APFloat::cmpUnordered)
989 return getNode(ISD::UNDEF, VT);
991 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
992 case ISD::SETLE: if (R==APFloat::cmpUnordered)
993 return getNode(ISD::UNDEF, VT);
995 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
996 R==APFloat::cmpEqual, VT);
997 case ISD::SETGE: if (R==APFloat::cmpUnordered)
998 return getNode(ISD::UNDEF, VT);
1000 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
1001 R==APFloat::cmpEqual, VT);
1002 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1003 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1004 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1005 R==APFloat::cmpEqual, VT);
1006 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1007 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1008 R==APFloat::cmpLessThan, VT);
1009 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1010 R==APFloat::cmpUnordered, VT);
1011 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1012 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
1015 // Ensure that the constant occurs on the RHS.
1016 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
1019 // Could not fold it.
1023 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1024 /// this predicate to simplify operations downstream. Mask is known to be zero
1025 /// for bits that V cannot have.
1026 bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1027 unsigned Depth) const {
1028 // The masks are not wide enough to represent this type! Should use APInt.
1029 if (Op.getValueType() == MVT::i128)
1032 uint64_t KnownZero, KnownOne;
1033 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1034 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1035 return (KnownZero & Mask) == Mask;
1038 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
1039 /// known to be either zero or one and return them in the KnownZero/KnownOne
1040 /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1042 void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1043 uint64_t &KnownZero, uint64_t &KnownOne,
1044 unsigned Depth) const {
1045 KnownZero = KnownOne = 0; // Don't know anything.
1046 if (Depth == 6 || Mask == 0)
1047 return; // Limit search depth.
1049 // The masks are not wide enough to represent this type! Should use APInt.
1050 if (Op.getValueType() == MVT::i128)
1053 uint64_t KnownZero2, KnownOne2;
1055 switch (Op.getOpcode()) {
1057 // We know all of the bits for a constant!
1058 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1059 KnownZero = ~KnownOne & Mask;
1062 // If either the LHS or the RHS are Zero, the result is zero.
1063 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1065 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1066 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1067 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1069 // Output known-1 bits are only known if set in both the LHS & RHS.
1070 KnownOne &= KnownOne2;
1071 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1072 KnownZero |= KnownZero2;
1075 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1077 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1078 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1079 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1081 // Output known-0 bits are only known if clear in both the LHS & RHS.
1082 KnownZero &= KnownZero2;
1083 // Output known-1 are known to be set if set in either the LHS | RHS.
1084 KnownOne |= KnownOne2;
1087 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1088 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1089 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1090 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1092 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1093 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1094 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1095 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1096 KnownZero = KnownZeroOut;
1100 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1101 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1102 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1103 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1105 // Only known if known in both the LHS and RHS.
1106 KnownOne &= KnownOne2;
1107 KnownZero &= KnownZero2;
1109 case ISD::SELECT_CC:
1110 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1111 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1112 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1113 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1115 // Only known if known in both the LHS and RHS.
1116 KnownOne &= KnownOne2;
1117 KnownZero &= KnownZero2;
1120 // If we know the result of a setcc has the top bits zero, use this info.
1121 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1122 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1125 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1126 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1127 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1128 KnownZero, KnownOne, Depth+1);
1129 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1130 KnownZero <<= SA->getValue();
1131 KnownOne <<= SA->getValue();
1132 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1136 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1137 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1138 MVT::ValueType VT = Op.getValueType();
1139 unsigned ShAmt = SA->getValue();
1141 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1142 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1143 KnownZero, KnownOne, Depth+1);
1144 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1145 KnownZero &= TypeMask;
1146 KnownOne &= TypeMask;
1147 KnownZero >>= ShAmt;
1150 uint64_t HighBits = (1ULL << ShAmt)-1;
1151 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1152 KnownZero |= HighBits; // High bits known zero.
1156 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1157 MVT::ValueType VT = Op.getValueType();
1158 unsigned ShAmt = SA->getValue();
1160 // Compute the new bits that are at the top now.
1161 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1163 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1164 // If any of the demanded bits are produced by the sign extension, we also
1165 // demand the input sign bit.
1166 uint64_t HighBits = (1ULL << ShAmt)-1;
1167 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1168 if (HighBits & Mask)
1169 InDemandedMask |= MVT::getIntVTSignBit(VT);
1171 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1173 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1174 KnownZero &= TypeMask;
1175 KnownOne &= TypeMask;
1176 KnownZero >>= ShAmt;
1179 // Handle the sign bits.
1180 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1181 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1183 if (KnownZero & SignBit) {
1184 KnownZero |= HighBits; // New bits are known zero.
1185 } else if (KnownOne & SignBit) {
1186 KnownOne |= HighBits; // New bits are known one.
1190 case ISD::SIGN_EXTEND_INREG: {
1191 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1193 // Sign extension. Compute the demanded bits in the result that are not
1194 // present in the input.
1195 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1197 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1198 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1200 // If the sign extended bits are demanded, we know that the sign
1203 InputDemandedBits |= InSignBit;
1205 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1206 KnownZero, KnownOne, Depth+1);
1207 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1209 // If the sign bit of the input is known set or clear, then we know the
1210 // top bits of the result.
1211 if (KnownZero & InSignBit) { // Input sign bit known clear
1212 KnownZero |= NewBits;
1213 KnownOne &= ~NewBits;
1214 } else if (KnownOne & InSignBit) { // Input sign bit known set
1215 KnownOne |= NewBits;
1216 KnownZero &= ~NewBits;
1217 } else { // Input sign bit unknown
1218 KnownZero &= ~NewBits;
1219 KnownOne &= ~NewBits;
1226 MVT::ValueType VT = Op.getValueType();
1227 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1228 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1233 if (ISD::isZEXTLoad(Op.Val)) {
1234 LoadSDNode *LD = cast<LoadSDNode>(Op);
1235 MVT::ValueType VT = LD->getLoadedVT();
1236 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1240 case ISD::ZERO_EXTEND: {
1241 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1242 uint64_t NewBits = (~InMask) & Mask;
1243 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1245 KnownZero |= NewBits & Mask;
1246 KnownOne &= ~NewBits;
1249 case ISD::SIGN_EXTEND: {
1250 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1251 unsigned InBits = MVT::getSizeInBits(InVT);
1252 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1253 uint64_t InSignBit = 1ULL << (InBits-1);
1254 uint64_t NewBits = (~InMask) & Mask;
1255 uint64_t InDemandedBits = Mask & InMask;
1257 // If any of the sign extended bits are demanded, we know that the sign
1260 InDemandedBits |= InSignBit;
1262 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1264 // If the sign bit is known zero or one, the top bits match.
1265 if (KnownZero & InSignBit) {
1266 KnownZero |= NewBits;
1267 KnownOne &= ~NewBits;
1268 } else if (KnownOne & InSignBit) {
1269 KnownOne |= NewBits;
1270 KnownZero &= ~NewBits;
1271 } else { // Otherwise, top bits aren't known.
1272 KnownOne &= ~NewBits;
1273 KnownZero &= ~NewBits;
1277 case ISD::ANY_EXTEND: {
1278 MVT::ValueType VT = Op.getOperand(0).getValueType();
1279 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1280 KnownZero, KnownOne, Depth+1);
1283 case ISD::TRUNCATE: {
1284 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1285 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1286 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1287 KnownZero &= OutMask;
1288 KnownOne &= OutMask;
1291 case ISD::AssertZext: {
1292 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1293 uint64_t InMask = MVT::getIntVTBitMask(VT);
1294 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1296 KnownZero |= (~InMask) & Mask;
1300 // If either the LHS or the RHS are Zero, the result is zero.
1301 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1302 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1303 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1304 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1306 // Output known-0 bits are known if clear or set in both the low clear bits
1307 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1308 // low 3 bits clear.
1309 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1310 CountTrailingZeros_64(~KnownZero2));
1312 KnownZero = (1ULL << KnownZeroOut) - 1;
1317 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1320 // We know that the top bits of C-X are clear if X contains less bits
1321 // than C (i.e. no wrap-around can happen). For example, 20-X is
1322 // positive if we can prove that X is >= 0 and < 16.
1323 MVT::ValueType VT = CLHS->getValueType(0);
1324 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1325 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1326 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1327 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1328 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1330 // If all of the MaskV bits are known to be zero, then we know the output
1331 // top bits are zero, because we now know that the output is from [0-C].
1332 if ((KnownZero & MaskV) == MaskV) {
1333 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1334 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1335 KnownOne = 0; // No one bits known.
1337 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1343 // Allow the target to implement this method for its nodes.
1344 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1345 case ISD::INTRINSIC_WO_CHAIN:
1346 case ISD::INTRINSIC_W_CHAIN:
1347 case ISD::INTRINSIC_VOID:
1348 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1354 /// ComputeNumSignBits - Return the number of times the sign bit of the
1355 /// register is replicated into the other bits. We know that at least 1 bit
1356 /// is always equal to the sign bit (itself), but other cases can give us
1357 /// information. For example, immediately after an "SRA X, 2", we know that
1358 /// the top 3 bits are all equal to each other, so we return 3.
1359 unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1360 MVT::ValueType VT = Op.getValueType();
1361 assert(MVT::isInteger(VT) && "Invalid VT!");
1362 unsigned VTBits = MVT::getSizeInBits(VT);
1366 return 1; // Limit search depth.
1368 switch (Op.getOpcode()) {
1370 case ISD::AssertSext:
1371 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1372 return VTBits-Tmp+1;
1373 case ISD::AssertZext:
1374 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1377 case ISD::Constant: {
1378 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1379 // If negative, invert the bits, then look at it.
1380 if (Val & MVT::getIntVTSignBit(VT))
1383 // Shift the bits so they are the leading bits in the int64_t.
1386 // Return # leading zeros. We use 'min' here in case Val was zero before
1387 // shifting. We don't want to return '64' as for an i32 "0".
1388 return std::min(VTBits, CountLeadingZeros_64(Val));
1391 case ISD::SIGN_EXTEND:
1392 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1393 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1395 case ISD::SIGN_EXTEND_INREG:
1396 // Max of the input and what this extends.
1397 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1400 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1401 return std::max(Tmp, Tmp2);
1404 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1405 // SRA X, C -> adds C sign bits.
1406 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1407 Tmp += C->getValue();
1408 if (Tmp > VTBits) Tmp = VTBits;
1412 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1413 // shl destroys sign bits.
1414 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1415 if (C->getValue() >= VTBits || // Bad shift.
1416 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1417 return Tmp - C->getValue();
1422 case ISD::XOR: // NOT is handled here.
1423 // Logical binary ops preserve the number of sign bits.
1424 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1425 if (Tmp == 1) return 1; // Early out.
1426 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1427 return std::min(Tmp, Tmp2);
1430 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1431 if (Tmp == 1) return 1; // Early out.
1432 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1433 return std::min(Tmp, Tmp2);
1436 // If setcc returns 0/-1, all bits are sign bits.
1437 if (TLI.getSetCCResultContents() ==
1438 TargetLowering::ZeroOrNegativeOneSetCCResult)
1443 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1444 unsigned RotAmt = C->getValue() & (VTBits-1);
1446 // Handle rotate right by N like a rotate left by 32-N.
1447 if (Op.getOpcode() == ISD::ROTR)
1448 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1450 // If we aren't rotating out all of the known-in sign bits, return the
1451 // number that are left. This handles rotl(sext(x), 1) for example.
1452 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1453 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1457 // Add can have at most one carry bit. Thus we know that the output
1458 // is, at worst, one more bit than the inputs.
1459 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1460 if (Tmp == 1) return 1; // Early out.
1462 // Special case decrementing a value (ADD X, -1):
1463 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1464 if (CRHS->isAllOnesValue()) {
1465 uint64_t KnownZero, KnownOne;
1466 uint64_t Mask = MVT::getIntVTBitMask(VT);
1467 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1469 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1471 if ((KnownZero|1) == Mask)
1474 // If we are subtracting one from a positive number, there is no carry
1475 // out of the result.
1476 if (KnownZero & MVT::getIntVTSignBit(VT))
1480 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1481 if (Tmp2 == 1) return 1;
1482 return std::min(Tmp, Tmp2)-1;
1486 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1487 if (Tmp2 == 1) return 1;
1490 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1491 if (CLHS->getValue() == 0) {
1492 uint64_t KnownZero, KnownOne;
1493 uint64_t Mask = MVT::getIntVTBitMask(VT);
1494 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1495 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1497 if ((KnownZero|1) == Mask)
1500 // If the input is known to be positive (the sign bit is known clear),
1501 // the output of the NEG has the same number of sign bits as the input.
1502 if (KnownZero & MVT::getIntVTSignBit(VT))
1505 // Otherwise, we treat this like a SUB.
1508 // Sub can have at most one carry bit. Thus we know that the output
1509 // is, at worst, one more bit than the inputs.
1510 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1511 if (Tmp == 1) return 1; // Early out.
1512 return std::min(Tmp, Tmp2)-1;
1515 // FIXME: it's tricky to do anything useful for this, but it is an important
1516 // case for targets like X86.
1520 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1521 if (Op.getOpcode() == ISD::LOAD) {
1522 LoadSDNode *LD = cast<LoadSDNode>(Op);
1523 unsigned ExtType = LD->getExtensionType();
1526 case ISD::SEXTLOAD: // '17' bits known
1527 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1528 return VTBits-Tmp+1;
1529 case ISD::ZEXTLOAD: // '16' bits known
1530 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1535 // Allow the target to implement this method for its nodes.
1536 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1537 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1538 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1539 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1540 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1541 if (NumBits > 1) return NumBits;
1544 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1545 // use this information.
1546 uint64_t KnownZero, KnownOne;
1547 uint64_t Mask = MVT::getIntVTBitMask(VT);
1548 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1550 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1551 if (KnownZero & SignBit) { // SignBit is 0
1553 } else if (KnownOne & SignBit) { // SignBit is 1;
1560 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1561 // the number of identical bits in the top of the input value.
1564 // Return # leading zeros. We use 'min' here in case Val was zero before
1565 // shifting. We don't want to return '64' as for an i32 "0".
1566 return std::min(VTBits, CountLeadingZeros_64(Mask));
1570 /// getNode - Gets or creates the specified node.
1572 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
1573 FoldingSetNodeID ID;
1574 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
1576 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1577 return SDOperand(E, 0);
1578 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
1579 CSEMap.InsertNode(N, IP);
1581 AllNodes.push_back(N);
1582 return SDOperand(N, 0);
1585 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1586 SDOperand Operand) {
1588 // Constant fold unary operations with an integer constant operand.
1589 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1590 uint64_t Val = C->getValue();
1593 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
1594 case ISD::ANY_EXTEND:
1595 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1596 case ISD::TRUNCATE: return getConstant(Val, VT);
1597 case ISD::UINT_TO_FP:
1598 case ISD::SINT_TO_FP: {
1599 const uint64_t zero[] = {0, 0};
1600 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
1601 (void)apf.convertFromZeroExtendedInteger(&Val,
1602 MVT::getSizeInBits(Operand.getValueType()),
1603 Opcode==ISD::SINT_TO_FP,
1604 APFloat::rmNearestTiesToEven);
1605 return getConstantFP(apf, VT);
1607 case ISD::BIT_CONVERT:
1608 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
1609 return getConstantFP(BitsToFloat(Val), VT);
1610 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
1611 return getConstantFP(BitsToDouble(Val), VT);
1615 default: assert(0 && "Invalid bswap!"); break;
1616 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1617 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1618 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1623 default: assert(0 && "Invalid ctpop!"); break;
1624 case MVT::i1: return getConstant(Val != 0, VT);
1626 Tmp1 = (unsigned)Val & 0xFF;
1627 return getConstant(CountPopulation_32(Tmp1), VT);
1629 Tmp1 = (unsigned)Val & 0xFFFF;
1630 return getConstant(CountPopulation_32(Tmp1), VT);
1632 return getConstant(CountPopulation_32((unsigned)Val), VT);
1634 return getConstant(CountPopulation_64(Val), VT);
1638 default: assert(0 && "Invalid ctlz!"); break;
1639 case MVT::i1: return getConstant(Val == 0, VT);
1641 Tmp1 = (unsigned)Val & 0xFF;
1642 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1644 Tmp1 = (unsigned)Val & 0xFFFF;
1645 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1647 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1649 return getConstant(CountLeadingZeros_64(Val), VT);
1653 default: assert(0 && "Invalid cttz!"); break;
1654 case MVT::i1: return getConstant(Val == 0, VT);
1656 Tmp1 = (unsigned)Val | 0x100;
1657 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1659 Tmp1 = (unsigned)Val | 0x10000;
1660 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1662 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1664 return getConstant(CountTrailingZeros_64(Val), VT);
1669 // Constant fold unary operations with a floating point constant operand.
1670 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1671 APFloat V = C->getValueAPF(); // make copy
1675 return getConstantFP(V, VT);
1678 return getConstantFP(V, VT);
1680 case ISD::FP_EXTEND:
1681 // This can return overflow, underflow, or inexact; we don't care.
1682 // FIXME need to be more flexible about rounding mode.
1683 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle :
1684 VT==MVT::f64 ? APFloat::IEEEdouble :
1685 VT==MVT::f80 ? APFloat::x87DoubleExtended :
1686 VT==MVT::f128 ? APFloat::IEEEquad :
1688 APFloat::rmNearestTiesToEven);
1689 return getConstantFP(V, VT);
1690 case ISD::FP_TO_SINT:
1691 case ISD::FP_TO_UINT: {
1693 assert(integerPartWidth >= 64);
1694 // FIXME need to be more flexible about rounding mode.
1695 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1696 Opcode==ISD::FP_TO_SINT,
1697 APFloat::rmTowardZero);
1698 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1700 return getConstant(x, VT);
1702 case ISD::BIT_CONVERT:
1703 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1704 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1705 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1706 return getConstant(V.convertToAPInt().getZExtValue(), VT);
1711 unsigned OpOpcode = Operand.Val->getOpcode();
1713 case ISD::TokenFactor:
1714 return Operand; // Factor of one node? No factor.
1716 case ISD::FP_EXTEND:
1717 assert(MVT::isFloatingPoint(VT) &&
1718 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
1720 case ISD::SIGN_EXTEND:
1721 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1722 "Invalid SIGN_EXTEND!");
1723 if (Operand.getValueType() == VT) return Operand; // noop extension
1724 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
1725 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1726 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1728 case ISD::ZERO_EXTEND:
1729 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1730 "Invalid ZERO_EXTEND!");
1731 if (Operand.getValueType() == VT) return Operand; // noop extension
1732 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
1733 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1734 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1736 case ISD::ANY_EXTEND:
1737 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1738 "Invalid ANY_EXTEND!");
1739 if (Operand.getValueType() == VT) return Operand; // noop extension
1740 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
1741 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1742 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1743 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1746 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1747 "Invalid TRUNCATE!");
1748 if (Operand.getValueType() == VT) return Operand; // noop truncate
1749 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
1750 if (OpOpcode == ISD::TRUNCATE)
1751 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1752 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1753 OpOpcode == ISD::ANY_EXTEND) {
1754 // If the source is smaller than the dest, we still need an extend.
1755 if (Operand.Val->getOperand(0).getValueType() < VT)
1756 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1757 else if (Operand.Val->getOperand(0).getValueType() > VT)
1758 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1760 return Operand.Val->getOperand(0);
1763 case ISD::BIT_CONVERT:
1764 // Basic sanity checking.
1765 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
1766 && "Cannot BIT_CONVERT between types of different sizes!");
1767 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1768 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1769 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1770 if (OpOpcode == ISD::UNDEF)
1771 return getNode(ISD::UNDEF, VT);
1773 case ISD::SCALAR_TO_VECTOR:
1774 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1775 MVT::getVectorElementType(VT) == Operand.getValueType() &&
1776 "Illegal SCALAR_TO_VECTOR node!");
1779 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1780 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1781 Operand.Val->getOperand(0));
1782 if (OpOpcode == ISD::FNEG) // --X -> X
1783 return Operand.Val->getOperand(0);
1786 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1787 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1792 SDVTList VTs = getVTList(VT);
1793 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1794 FoldingSetNodeID ID;
1795 SDOperand Ops[1] = { Operand };
1796 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
1798 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1799 return SDOperand(E, 0);
1800 N = new UnarySDNode(Opcode, VTs, Operand);
1801 CSEMap.InsertNode(N, IP);
1803 N = new UnarySDNode(Opcode, VTs, Operand);
1805 AllNodes.push_back(N);
1806 return SDOperand(N, 0);
1811 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1812 SDOperand N1, SDOperand N2) {
1815 case ISD::TokenFactor:
1816 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1817 N2.getValueType() == MVT::Other && "Invalid token factor!");
1826 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1833 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1840 assert(N1.getValueType() == N2.getValueType() &&
1841 N1.getValueType() == VT && "Binary operator types must match!");
1843 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1844 assert(N1.getValueType() == VT &&
1845 MVT::isFloatingPoint(N1.getValueType()) &&
1846 MVT::isFloatingPoint(N2.getValueType()) &&
1847 "Invalid FCOPYSIGN!");
1854 assert(VT == N1.getValueType() &&
1855 "Shift operators return type must be the same as their first arg");
1856 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1857 VT != MVT::i1 && "Shifts only work on integers");
1859 case ISD::FP_ROUND_INREG: {
1860 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1861 assert(VT == N1.getValueType() && "Not an inreg round!");
1862 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1863 "Cannot FP_ROUND_INREG integer types");
1864 assert(EVT <= VT && "Not rounding down!");
1867 case ISD::AssertSext:
1868 case ISD::AssertZext:
1869 case ISD::SIGN_EXTEND_INREG: {
1870 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1871 assert(VT == N1.getValueType() && "Not an inreg extend!");
1872 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1873 "Cannot *_EXTEND_INREG FP types");
1874 assert(EVT <= VT && "Not extending!");
1881 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1882 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1884 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1885 int64_t Val = N1C->getValue();
1886 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1887 Val <<= 64-FromBits;
1888 Val >>= 64-FromBits;
1889 return getConstant(Val, VT);
1893 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1895 case ISD::ADD: return getConstant(C1 + C2, VT);
1896 case ISD::SUB: return getConstant(C1 - C2, VT);
1897 case ISD::MUL: return getConstant(C1 * C2, VT);
1899 if (C2) return getConstant(C1 / C2, VT);
1902 if (C2) return getConstant(C1 % C2, VT);
1905 if (C2) return getConstant(N1C->getSignExtended() /
1906 N2C->getSignExtended(), VT);
1909 if (C2) return getConstant(N1C->getSignExtended() %
1910 N2C->getSignExtended(), VT);
1912 case ISD::AND : return getConstant(C1 & C2, VT);
1913 case ISD::OR : return getConstant(C1 | C2, VT);
1914 case ISD::XOR : return getConstant(C1 ^ C2, VT);
1915 case ISD::SHL : return getConstant(C1 << C2, VT);
1916 case ISD::SRL : return getConstant(C1 >> C2, VT);
1917 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
1919 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1922 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1926 } else { // Cannonicalize constant to RHS if commutative
1927 if (isCommutativeBinOp(Opcode)) {
1928 std::swap(N1C, N2C);
1934 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1935 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1938 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
1939 APFloat::opStatus s;
1942 s = V1.add(V2, APFloat::rmNearestTiesToEven);
1943 if (s!=APFloat::opInvalidOp)
1944 return getConstantFP(V1, VT);
1947 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
1948 if (s!=APFloat::opInvalidOp)
1949 return getConstantFP(V1, VT);
1952 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
1953 if (s!=APFloat::opInvalidOp)
1954 return getConstantFP(V1, VT);
1957 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
1958 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1959 return getConstantFP(V1, VT);
1962 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
1963 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1964 return getConstantFP(V1, VT);
1966 case ISD::FCOPYSIGN:
1968 return getConstantFP(V1, VT);
1971 } else { // Cannonicalize constant to RHS if commutative
1972 if (isCommutativeBinOp(Opcode)) {
1973 std::swap(N1CFP, N2CFP);
1979 // Canonicalize an UNDEF to the RHS, even over a constant.
1980 if (N1.getOpcode() == ISD::UNDEF) {
1981 if (isCommutativeBinOp(Opcode)) {
1985 case ISD::FP_ROUND_INREG:
1986 case ISD::SIGN_EXTEND_INREG:
1992 return N1; // fold op(undef, arg2) -> undef
1999 if (!MVT::isVector(VT))
2000 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2001 // For vectors, we can't easily build an all zero vector, just return
2008 // Fold a bunch of operators when the RHS is undef.
2009 if (N2.getOpcode() == ISD::UNDEF) {
2025 return N2; // fold op(arg1, undef) -> undef
2030 if (!MVT::isVector(VT))
2031 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2032 // For vectors, we can't easily build an all zero vector, just return
2036 if (!MVT::isVector(VT))
2037 return getConstant(MVT::getIntVTBitMask(VT), VT);
2038 // For vectors, we can't easily build an all one vector, just return
2048 case ISD::TokenFactor:
2049 // Fold trivial token factors.
2050 if (N1.getOpcode() == ISD::EntryToken) return N2;
2051 if (N2.getOpcode() == ISD::EntryToken) return N1;
2055 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2056 // worth handling here.
2057 if (N2C && N2C->getValue() == 0)
2062 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
2063 // worth handling here.
2064 if (N2C && N2C->getValue() == 0)
2067 case ISD::FP_ROUND_INREG:
2068 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
2070 case ISD::SIGN_EXTEND_INREG: {
2071 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2072 if (EVT == VT) return N1; // Not actually extending
2075 case ISD::EXTRACT_VECTOR_ELT:
2076 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2078 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2079 // expanding copies of large vectors from registers.
2080 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2081 N1.getNumOperands() > 0) {
2083 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2084 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2085 N1.getOperand(N2C->getValue() / Factor),
2086 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2089 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2090 // expanding large vector constants.
2091 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2092 return N1.getOperand(N2C->getValue());
2094 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2095 // operations are lowered to scalars.
2096 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2097 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2099 return N1.getOperand(1);
2101 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2104 case ISD::EXTRACT_ELEMENT:
2105 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
2107 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2108 // 64-bit integers into 32-bit parts. Instead of building the extract of
2109 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2110 if (N1.getOpcode() == ISD::BUILD_PAIR)
2111 return N1.getOperand(N2C->getValue());
2113 // EXTRACT_ELEMENT of a constant int is also very common.
2114 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2115 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2116 return getConstant(C->getValue() >> Shift, VT);
2120 // FIXME: figure out how to safely handle things like
2121 // int foo(int x) { return 1 << (x & 255); }
2122 // int bar() { return foo(256); }
2127 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2128 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
2129 return getNode(Opcode, VT, N1, N2.getOperand(0));
2130 else if (N2.getOpcode() == ISD::AND)
2131 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
2132 // If the and is only masking out bits that cannot effect the shift,
2133 // eliminate the and.
2134 unsigned NumBits = MVT::getSizeInBits(VT);
2135 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2136 return getNode(Opcode, VT, N1, N2.getOperand(0));
2142 // Memoize this node if possible.
2144 SDVTList VTs = getVTList(VT);
2145 if (VT != MVT::Flag) {
2146 SDOperand Ops[] = { N1, N2 };
2147 FoldingSetNodeID ID;
2148 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
2150 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2151 return SDOperand(E, 0);
2152 N = new BinarySDNode(Opcode, VTs, N1, N2);
2153 CSEMap.InsertNode(N, IP);
2155 N = new BinarySDNode(Opcode, VTs, N1, N2);
2158 AllNodes.push_back(N);
2159 return SDOperand(N, 0);
2162 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2163 SDOperand N1, SDOperand N2, SDOperand N3) {
2164 // Perform various simplifications.
2165 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2166 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2169 // Use FoldSetCC to simplify SETCC's.
2170 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
2171 if (Simp.Val) return Simp;
2176 if (N1C->getValue())
2177 return N2; // select true, X, Y -> X
2179 return N3; // select false, X, Y -> Y
2181 if (N2 == N3) return N2; // select C, X, X -> X
2185 if (N2C->getValue()) // Unconditional branch
2186 return getNode(ISD::BR, MVT::Other, N1, N3);
2188 return N1; // Never-taken branch
2190 case ISD::VECTOR_SHUFFLE:
2191 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2192 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2193 N3.getOpcode() == ISD::BUILD_VECTOR &&
2194 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2195 "Illegal VECTOR_SHUFFLE node!");
2197 case ISD::BIT_CONVERT:
2198 // Fold bit_convert nodes from a type to themselves.
2199 if (N1.getValueType() == VT)
2204 // Memoize node if it doesn't produce a flag.
2206 SDVTList VTs = getVTList(VT);
2207 if (VT != MVT::Flag) {
2208 SDOperand Ops[] = { N1, N2, N3 };
2209 FoldingSetNodeID ID;
2210 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2212 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2213 return SDOperand(E, 0);
2214 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2215 CSEMap.InsertNode(N, IP);
2217 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2219 AllNodes.push_back(N);
2220 return SDOperand(N, 0);
2223 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2224 SDOperand N1, SDOperand N2, SDOperand N3,
2226 SDOperand Ops[] = { N1, N2, N3, N4 };
2227 return getNode(Opcode, VT, Ops, 4);
2230 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2231 SDOperand N1, SDOperand N2, SDOperand N3,
2232 SDOperand N4, SDOperand N5) {
2233 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2234 return getNode(Opcode, VT, Ops, 5);
2237 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2238 SDOperand Chain, SDOperand Ptr,
2239 const Value *SV, int SVOffset,
2240 bool isVolatile, unsigned Alignment) {
2241 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2243 if (VT != MVT::iPTR) {
2244 Ty = MVT::getTypeForValueType(VT);
2246 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2247 assert(PT && "Value for load must be a pointer");
2248 Ty = PT->getElementType();
2250 assert(Ty && "Could not get type information for load");
2251 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2253 SDVTList VTs = getVTList(VT, MVT::Other);
2254 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2255 SDOperand Ops[] = { Chain, Ptr, Undef };
2256 FoldingSetNodeID ID;
2257 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2258 ID.AddInteger(ISD::UNINDEXED);
2259 ID.AddInteger(ISD::NON_EXTLOAD);
2260 ID.AddInteger((unsigned int)VT);
2262 ID.AddInteger(SVOffset);
2263 ID.AddInteger(Alignment);
2264 ID.AddInteger(isVolatile);
2266 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2267 return SDOperand(E, 0);
2268 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
2269 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2271 CSEMap.InsertNode(N, IP);
2272 AllNodes.push_back(N);
2273 return SDOperand(N, 0);
2276 SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
2277 SDOperand Chain, SDOperand Ptr,
2279 int SVOffset, MVT::ValueType EVT,
2280 bool isVolatile, unsigned Alignment) {
2281 // If they are asking for an extending load from/to the same thing, return a
2284 ExtType = ISD::NON_EXTLOAD;
2286 if (MVT::isVector(VT))
2287 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
2289 assert(EVT < VT && "Should only be an extending load, not truncating!");
2290 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2291 "Cannot sign/zero extend a FP/Vector load!");
2292 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2293 "Cannot convert from FP to Int or Int -> FP!");
2295 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2297 if (VT != MVT::iPTR) {
2298 Ty = MVT::getTypeForValueType(VT);
2300 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2301 assert(PT && "Value for load must be a pointer");
2302 Ty = PT->getElementType();
2304 assert(Ty && "Could not get type information for load");
2305 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2307 SDVTList VTs = getVTList(VT, MVT::Other);
2308 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2309 SDOperand Ops[] = { Chain, Ptr, Undef };
2310 FoldingSetNodeID ID;
2311 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2312 ID.AddInteger(ISD::UNINDEXED);
2313 ID.AddInteger(ExtType);
2314 ID.AddInteger((unsigned int)EVT);
2316 ID.AddInteger(SVOffset);
2317 ID.AddInteger(Alignment);
2318 ID.AddInteger(isVolatile);
2320 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2321 return SDOperand(E, 0);
2322 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
2323 SV, SVOffset, Alignment, isVolatile);
2324 CSEMap.InsertNode(N, IP);
2325 AllNodes.push_back(N);
2326 return SDOperand(N, 0);
2330 SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2331 SDOperand Offset, ISD::MemIndexedMode AM) {
2332 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
2333 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2334 "Load is already a indexed load!");
2335 MVT::ValueType VT = OrigLoad.getValueType();
2336 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
2337 SDOperand Ops[] = { LD->getChain(), Base, Offset };
2338 FoldingSetNodeID ID;
2339 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2341 ID.AddInteger(LD->getExtensionType());
2342 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
2343 ID.AddPointer(LD->getSrcValue());
2344 ID.AddInteger(LD->getSrcValueOffset());
2345 ID.AddInteger(LD->getAlignment());
2346 ID.AddInteger(LD->isVolatile());
2348 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2349 return SDOperand(E, 0);
2350 SDNode *N = new LoadSDNode(Ops, VTs, AM,
2351 LD->getExtensionType(), LD->getLoadedVT(),
2352 LD->getSrcValue(), LD->getSrcValueOffset(),
2353 LD->getAlignment(), LD->isVolatile());
2354 CSEMap.InsertNode(N, IP);
2355 AllNodes.push_back(N);
2356 return SDOperand(N, 0);
2359 SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
2360 SDOperand Ptr, const Value *SV, int SVOffset,
2361 bool isVolatile, unsigned Alignment) {
2362 MVT::ValueType VT = Val.getValueType();
2364 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2366 if (VT != MVT::iPTR) {
2367 Ty = MVT::getTypeForValueType(VT);
2369 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2370 assert(PT && "Value for store must be a pointer");
2371 Ty = PT->getElementType();
2373 assert(Ty && "Could not get type information for store");
2374 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2376 SDVTList VTs = getVTList(MVT::Other);
2377 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2378 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2379 FoldingSetNodeID ID;
2380 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2381 ID.AddInteger(ISD::UNINDEXED);
2382 ID.AddInteger(false);
2383 ID.AddInteger((unsigned int)VT);
2385 ID.AddInteger(SVOffset);
2386 ID.AddInteger(Alignment);
2387 ID.AddInteger(isVolatile);
2389 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2390 return SDOperand(E, 0);
2391 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
2392 VT, SV, SVOffset, Alignment, isVolatile);
2393 CSEMap.InsertNode(N, IP);
2394 AllNodes.push_back(N);
2395 return SDOperand(N, 0);
2398 SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
2399 SDOperand Ptr, const Value *SV,
2400 int SVOffset, MVT::ValueType SVT,
2401 bool isVolatile, unsigned Alignment) {
2402 MVT::ValueType VT = Val.getValueType();
2403 bool isTrunc = VT != SVT;
2405 assert(VT > SVT && "Not a truncation?");
2406 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2407 "Can't do FP-INT conversion!");
2409 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2411 if (VT != MVT::iPTR) {
2412 Ty = MVT::getTypeForValueType(VT);
2414 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2415 assert(PT && "Value for store must be a pointer");
2416 Ty = PT->getElementType();
2418 assert(Ty && "Could not get type information for store");
2419 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2421 SDVTList VTs = getVTList(MVT::Other);
2422 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2423 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2424 FoldingSetNodeID ID;
2425 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2426 ID.AddInteger(ISD::UNINDEXED);
2427 ID.AddInteger(isTrunc);
2428 ID.AddInteger((unsigned int)SVT);
2430 ID.AddInteger(SVOffset);
2431 ID.AddInteger(Alignment);
2432 ID.AddInteger(isVolatile);
2434 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2435 return SDOperand(E, 0);
2436 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, isTrunc,
2437 SVT, SV, SVOffset, Alignment, isVolatile);
2438 CSEMap.InsertNode(N, IP);
2439 AllNodes.push_back(N);
2440 return SDOperand(N, 0);
2444 SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2445 SDOperand Offset, ISD::MemIndexedMode AM) {
2446 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2447 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2448 "Store is already a indexed store!");
2449 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2450 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2451 FoldingSetNodeID ID;
2452 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2454 ID.AddInteger(ST->isTruncatingStore());
2455 ID.AddInteger((unsigned int)(ST->getStoredVT()));
2456 ID.AddPointer(ST->getSrcValue());
2457 ID.AddInteger(ST->getSrcValueOffset());
2458 ID.AddInteger(ST->getAlignment());
2459 ID.AddInteger(ST->isVolatile());
2461 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2462 return SDOperand(E, 0);
2463 SDNode *N = new StoreSDNode(Ops, VTs, AM,
2464 ST->isTruncatingStore(), ST->getStoredVT(),
2465 ST->getSrcValue(), ST->getSrcValueOffset(),
2466 ST->getAlignment(), ST->isVolatile());
2467 CSEMap.InsertNode(N, IP);
2468 AllNodes.push_back(N);
2469 return SDOperand(N, 0);
2472 SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2473 SDOperand Chain, SDOperand Ptr,
2475 SDOperand Ops[] = { Chain, Ptr, SV };
2476 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
2479 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2480 const SDOperand *Ops, unsigned NumOps) {
2482 case 0: return getNode(Opcode, VT);
2483 case 1: return getNode(Opcode, VT, Ops[0]);
2484 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2485 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
2491 case ISD::SELECT_CC: {
2492 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
2493 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2494 "LHS and RHS of condition must have same type!");
2495 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2496 "True and False arms of SelectCC must have same type!");
2497 assert(Ops[2].getValueType() == VT &&
2498 "select_cc node must be of same type as true and false value!");
2502 assert(NumOps == 5 && "BR_CC takes 5 operands!");
2503 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2504 "LHS/RHS of comparison should match types!");
2511 SDVTList VTs = getVTList(VT);
2512 if (VT != MVT::Flag) {
2513 FoldingSetNodeID ID;
2514 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
2516 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2517 return SDOperand(E, 0);
2518 N = new SDNode(Opcode, VTs, Ops, NumOps);
2519 CSEMap.InsertNode(N, IP);
2521 N = new SDNode(Opcode, VTs, Ops, NumOps);
2523 AllNodes.push_back(N);
2524 return SDOperand(N, 0);
2527 SDOperand SelectionDAG::getNode(unsigned Opcode,
2528 std::vector<MVT::ValueType> &ResultTys,
2529 const SDOperand *Ops, unsigned NumOps) {
2530 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2534 SDOperand SelectionDAG::getNode(unsigned Opcode,
2535 const MVT::ValueType *VTs, unsigned NumVTs,
2536 const SDOperand *Ops, unsigned NumOps) {
2538 return getNode(Opcode, VTs[0], Ops, NumOps);
2539 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2542 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2543 const SDOperand *Ops, unsigned NumOps) {
2544 if (VTList.NumVTs == 1)
2545 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
2548 // FIXME: figure out how to safely handle things like
2549 // int foo(int x) { return 1 << (x & 255); }
2550 // int bar() { return foo(256); }
2552 case ISD::SRA_PARTS:
2553 case ISD::SRL_PARTS:
2554 case ISD::SHL_PARTS:
2555 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2556 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
2557 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2558 else if (N3.getOpcode() == ISD::AND)
2559 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2560 // If the and is only masking out bits that cannot effect the shift,
2561 // eliminate the and.
2562 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2563 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2564 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2570 // Memoize the node unless it returns a flag.
2572 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
2573 FoldingSetNodeID ID;
2574 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
2576 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2577 return SDOperand(E, 0);
2579 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2580 else if (NumOps == 2)
2581 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2582 else if (NumOps == 3)
2583 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2585 N = new SDNode(Opcode, VTList, Ops, NumOps);
2586 CSEMap.InsertNode(N, IP);
2589 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2590 else if (NumOps == 2)
2591 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2592 else if (NumOps == 3)
2593 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2595 N = new SDNode(Opcode, VTList, Ops, NumOps);
2597 AllNodes.push_back(N);
2598 return SDOperand(N, 0);
2601 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2602 return getNode(Opcode, VTList, 0, 0);
2605 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2607 SDOperand Ops[] = { N1 };
2608 return getNode(Opcode, VTList, Ops, 1);
2611 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2612 SDOperand N1, SDOperand N2) {
2613 SDOperand Ops[] = { N1, N2 };
2614 return getNode(Opcode, VTList, Ops, 2);
2617 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2618 SDOperand N1, SDOperand N2, SDOperand N3) {
2619 SDOperand Ops[] = { N1, N2, N3 };
2620 return getNode(Opcode, VTList, Ops, 3);
2623 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2624 SDOperand N1, SDOperand N2, SDOperand N3,
2626 SDOperand Ops[] = { N1, N2, N3, N4 };
2627 return getNode(Opcode, VTList, Ops, 4);
2630 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2631 SDOperand N1, SDOperand N2, SDOperand N3,
2632 SDOperand N4, SDOperand N5) {
2633 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2634 return getNode(Opcode, VTList, Ops, 5);
2637 SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
2638 if (!MVT::isExtendedVT(VT))
2639 return makeVTList(SDNode::getValueTypeList(VT), 1);
2641 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2642 E = VTList.end(); I != E; ++I) {
2643 if (I->size() == 1 && (*I)[0] == VT)
2644 return makeVTList(&(*I)[0], 1);
2646 std::vector<MVT::ValueType> V;
2648 VTList.push_front(V);
2649 return makeVTList(&(*VTList.begin())[0], 1);
2652 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
2653 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2654 E = VTList.end(); I != E; ++I) {
2655 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
2656 return makeVTList(&(*I)[0], 2);
2658 std::vector<MVT::ValueType> V;
2661 VTList.push_front(V);
2662 return makeVTList(&(*VTList.begin())[0], 2);
2664 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2665 MVT::ValueType VT3) {
2666 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2667 E = VTList.end(); I != E; ++I) {
2668 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2670 return makeVTList(&(*I)[0], 3);
2672 std::vector<MVT::ValueType> V;
2676 VTList.push_front(V);
2677 return makeVTList(&(*VTList.begin())[0], 3);
2680 SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2682 case 0: assert(0 && "Cannot have nodes without results!");
2683 case 1: return getVTList(VTs[0]);
2684 case 2: return getVTList(VTs[0], VTs[1]);
2685 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2689 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2690 E = VTList.end(); I != E; ++I) {
2691 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2693 bool NoMatch = false;
2694 for (unsigned i = 2; i != NumVTs; ++i)
2695 if (VTs[i] != (*I)[i]) {
2700 return makeVTList(&*I->begin(), NumVTs);
2703 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2704 return makeVTList(&*VTList.begin()->begin(), NumVTs);
2708 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2709 /// specified operands. If the resultant node already exists in the DAG,
2710 /// this does not modify the specified node, instead it returns the node that
2711 /// already exists. If the resultant node does not exist in the DAG, the
2712 /// input node is returned. As a degenerate case, if you specify the same
2713 /// input operands as the node already has, the input node is returned.
2714 SDOperand SelectionDAG::
2715 UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2716 SDNode *N = InN.Val;
2717 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2719 // Check to see if there is no change.
2720 if (Op == N->getOperand(0)) return InN;
2722 // See if the modified node already exists.
2723 void *InsertPos = 0;
2724 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2725 return SDOperand(Existing, InN.ResNo);
2727 // Nope it doesn't. Remove the node from it's current place in the maps.
2729 RemoveNodeFromCSEMaps(N);
2731 // Now we update the operands.
2732 N->OperandList[0].Val->removeUser(N);
2734 N->OperandList[0] = Op;
2736 // If this gets put into a CSE map, add it.
2737 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2741 SDOperand SelectionDAG::
2742 UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2743 SDNode *N = InN.Val;
2744 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2746 // Check to see if there is no change.
2747 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2748 return InN; // No operands changed, just return the input node.
2750 // See if the modified node already exists.
2751 void *InsertPos = 0;
2752 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2753 return SDOperand(Existing, InN.ResNo);
2755 // Nope it doesn't. Remove the node from it's current place in the maps.
2757 RemoveNodeFromCSEMaps(N);
2759 // Now we update the operands.
2760 if (N->OperandList[0] != Op1) {
2761 N->OperandList[0].Val->removeUser(N);
2762 Op1.Val->addUser(N);
2763 N->OperandList[0] = Op1;
2765 if (N->OperandList[1] != Op2) {
2766 N->OperandList[1].Val->removeUser(N);
2767 Op2.Val->addUser(N);
2768 N->OperandList[1] = Op2;
2771 // If this gets put into a CSE map, add it.
2772 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2776 SDOperand SelectionDAG::
2777 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2778 SDOperand Ops[] = { Op1, Op2, Op3 };
2779 return UpdateNodeOperands(N, Ops, 3);
2782 SDOperand SelectionDAG::
2783 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2784 SDOperand Op3, SDOperand Op4) {
2785 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2786 return UpdateNodeOperands(N, Ops, 4);
2789 SDOperand SelectionDAG::
2790 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2791 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2792 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2793 return UpdateNodeOperands(N, Ops, 5);
2797 SDOperand SelectionDAG::
2798 UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
2799 SDNode *N = InN.Val;
2800 assert(N->getNumOperands() == NumOps &&
2801 "Update with wrong number of operands");
2803 // Check to see if there is no change.
2804 bool AnyChange = false;
2805 for (unsigned i = 0; i != NumOps; ++i) {
2806 if (Ops[i] != N->getOperand(i)) {
2812 // No operands changed, just return the input node.
2813 if (!AnyChange) return InN;
2815 // See if the modified node already exists.
2816 void *InsertPos = 0;
2817 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, 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 for (unsigned i = 0; i != NumOps; ++i) {
2826 if (N->OperandList[i] != Ops[i]) {
2827 N->OperandList[i].Val->removeUser(N);
2828 Ops[i].Val->addUser(N);
2829 N->OperandList[i] = Ops[i];
2833 // If this gets put into a CSE map, add it.
2834 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2839 /// MorphNodeTo - This frees the operands of the current node, resets the
2840 /// opcode, types, and operands to the specified value. This should only be
2841 /// used by the SelectionDAG class.
2842 void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2843 const SDOperand *Ops, unsigned NumOps) {
2846 NumValues = L.NumVTs;
2848 // Clear the operands list, updating used nodes to remove this from their
2850 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2851 I->Val->removeUser(this);
2853 // If NumOps is larger than the # of operands we currently have, reallocate
2854 // the operand list.
2855 if (NumOps > NumOperands) {
2856 if (OperandsNeedDelete)
2857 delete [] OperandList;
2858 OperandList = new SDOperand[NumOps];
2859 OperandsNeedDelete = true;
2862 // Assign the new operands.
2863 NumOperands = NumOps;
2865 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2866 OperandList[i] = Ops[i];
2867 SDNode *N = OperandList[i].Val;
2868 N->Uses.push_back(this);
2872 /// SelectNodeTo - These are used for target selectors to *mutate* the
2873 /// specified node to have the specified return type, Target opcode, and
2874 /// operands. Note that target opcodes are stored as
2875 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
2877 /// Note that SelectNodeTo returns the resultant node. If there is already a
2878 /// node of the specified opcode and operands, it returns that node instead of
2879 /// the current one.
2880 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2881 MVT::ValueType VT) {
2882 SDVTList VTs = getVTList(VT);
2883 FoldingSetNodeID ID;
2884 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
2886 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2889 RemoveNodeFromCSEMaps(N);
2891 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
2893 CSEMap.InsertNode(N, IP);
2897 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2898 MVT::ValueType VT, SDOperand Op1) {
2899 // If an identical node already exists, use it.
2900 SDVTList VTs = getVTList(VT);
2901 SDOperand Ops[] = { Op1 };
2903 FoldingSetNodeID ID;
2904 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
2906 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2909 RemoveNodeFromCSEMaps(N);
2910 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
2911 CSEMap.InsertNode(N, IP);
2915 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2916 MVT::ValueType VT, SDOperand Op1,
2918 // If an identical node already exists, use it.
2919 SDVTList VTs = getVTList(VT);
2920 SDOperand Ops[] = { Op1, Op2 };
2922 FoldingSetNodeID ID;
2923 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2925 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2928 RemoveNodeFromCSEMaps(N);
2930 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2932 CSEMap.InsertNode(N, IP); // Memoize the new node.
2936 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2937 MVT::ValueType VT, SDOperand Op1,
2938 SDOperand Op2, SDOperand Op3) {
2939 // If an identical node already exists, use it.
2940 SDVTList VTs = getVTList(VT);
2941 SDOperand Ops[] = { Op1, Op2, Op3 };
2942 FoldingSetNodeID ID;
2943 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
2945 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2948 RemoveNodeFromCSEMaps(N);
2950 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
2952 CSEMap.InsertNode(N, IP); // Memoize the new node.
2956 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2957 MVT::ValueType VT, const SDOperand *Ops,
2959 // If an identical node already exists, use it.
2960 SDVTList VTs = getVTList(VT);
2961 FoldingSetNodeID ID;
2962 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
2964 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2967 RemoveNodeFromCSEMaps(N);
2968 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
2970 CSEMap.InsertNode(N, IP); // Memoize the new node.
2974 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2975 MVT::ValueType VT1, MVT::ValueType VT2,
2976 SDOperand Op1, SDOperand Op2) {
2977 SDVTList VTs = getVTList(VT1, VT2);
2978 FoldingSetNodeID ID;
2979 SDOperand Ops[] = { Op1, Op2 };
2980 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2982 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2985 RemoveNodeFromCSEMaps(N);
2986 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2987 CSEMap.InsertNode(N, IP); // Memoize the new node.
2991 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2992 MVT::ValueType VT1, MVT::ValueType VT2,
2993 SDOperand Op1, SDOperand Op2,
2995 // If an identical node already exists, use it.
2996 SDVTList VTs = getVTList(VT1, VT2);
2997 SDOperand Ops[] = { Op1, Op2, Op3 };
2998 FoldingSetNodeID ID;
2999 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3001 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3004 RemoveNodeFromCSEMaps(N);
3006 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3007 CSEMap.InsertNode(N, IP); // Memoize the new node.
3012 /// getTargetNode - These are used for target selectors to create a new node
3013 /// with specified return type(s), target opcode, and operands.
3015 /// Note that getTargetNode returns the resultant node. If there is already a
3016 /// node of the specified opcode and operands, it returns that node instead of
3017 /// the current one.
3018 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3019 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3021 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3023 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3025 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3026 SDOperand Op1, SDOperand Op2) {
3027 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3029 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3030 SDOperand Op1, SDOperand Op2,
3032 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3034 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3035 const SDOperand *Ops, unsigned NumOps) {
3036 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
3038 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3039 MVT::ValueType VT2) {
3040 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3042 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3044 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3045 MVT::ValueType VT2, SDOperand Op1) {
3046 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3047 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
3049 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3050 MVT::ValueType VT2, SDOperand Op1,
3052 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3053 SDOperand Ops[] = { Op1, Op2 };
3054 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
3056 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3057 MVT::ValueType VT2, SDOperand Op1,
3058 SDOperand Op2, SDOperand Op3) {
3059 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3060 SDOperand Ops[] = { Op1, Op2, Op3 };
3061 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
3063 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3065 const SDOperand *Ops, unsigned NumOps) {
3066 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3067 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
3069 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3070 MVT::ValueType VT2, MVT::ValueType VT3,
3071 SDOperand Op1, SDOperand Op2) {
3072 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3073 SDOperand Ops[] = { Op1, Op2 };
3074 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
3076 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3077 MVT::ValueType VT2, MVT::ValueType VT3,
3078 SDOperand Op1, SDOperand Op2,
3080 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3081 SDOperand Ops[] = { Op1, Op2, Op3 };
3082 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3084 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3085 MVT::ValueType VT2, MVT::ValueType VT3,
3086 const SDOperand *Ops, unsigned NumOps) {
3087 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3088 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
3090 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3091 MVT::ValueType VT2, MVT::ValueType VT3,
3093 const SDOperand *Ops, unsigned NumOps) {
3094 std::vector<MVT::ValueType> VTList;
3095 VTList.push_back(VT1);
3096 VTList.push_back(VT2);
3097 VTList.push_back(VT3);
3098 VTList.push_back(VT4);
3099 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3100 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3102 SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3103 std::vector<MVT::ValueType> &ResultTys,
3104 const SDOperand *Ops, unsigned NumOps) {
3105 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3106 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3110 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3111 /// This can cause recursive merging of nodes in the DAG.
3113 /// This version assumes From/To have a single result value.
3115 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
3116 std::vector<SDNode*> *Deleted) {
3117 SDNode *From = FromN.Val, *To = ToN.Val;
3118 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
3119 "Cannot replace with this method!");
3120 assert(From != To && "Cannot replace uses of with self");
3122 while (!From->use_empty()) {
3123 // Process users until they are all gone.
3124 SDNode *U = *From->use_begin();
3126 // This node is about to morph, remove its old self from the CSE maps.
3127 RemoveNodeFromCSEMaps(U);
3129 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3131 if (I->Val == From) {
3132 From->removeUser(U);
3137 // Now that we have modified U, add it back to the CSE maps. If it already
3138 // exists there, recursively merge the results together.
3139 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3140 ReplaceAllUsesWith(U, Existing, Deleted);
3142 if (Deleted) Deleted->push_back(U);
3143 DeleteNodeNotInCSEMaps(U);
3148 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3149 /// This can cause recursive merging of nodes in the DAG.
3151 /// This version assumes From/To have matching types and numbers of result
3154 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3155 std::vector<SDNode*> *Deleted) {
3156 assert(From != To && "Cannot replace uses of with self");
3157 assert(From->getNumValues() == To->getNumValues() &&
3158 "Cannot use this version of ReplaceAllUsesWith!");
3159 if (From->getNumValues() == 1) { // If possible, use the faster version.
3160 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
3164 while (!From->use_empty()) {
3165 // Process users until they are all gone.
3166 SDNode *U = *From->use_begin();
3168 // This node is about to morph, remove its old self from the CSE maps.
3169 RemoveNodeFromCSEMaps(U);
3171 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3173 if (I->Val == From) {
3174 From->removeUser(U);
3179 // Now that we have modified U, add it back to the CSE maps. If it already
3180 // exists there, recursively merge the results together.
3181 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3182 ReplaceAllUsesWith(U, Existing, Deleted);
3184 if (Deleted) Deleted->push_back(U);
3185 DeleteNodeNotInCSEMaps(U);
3190 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3191 /// This can cause recursive merging of nodes in the DAG.
3193 /// This version can replace From with any result values. To must match the
3194 /// number and types of values returned by From.
3195 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
3196 const SDOperand *To,
3197 std::vector<SDNode*> *Deleted) {
3198 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
3199 // Degenerate case handled above.
3200 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
3204 while (!From->use_empty()) {
3205 // Process users until they are all gone.
3206 SDNode *U = *From->use_begin();
3208 // This node is about to morph, remove its old self from the CSE maps.
3209 RemoveNodeFromCSEMaps(U);
3211 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3213 if (I->Val == From) {
3214 const SDOperand &ToOp = To[I->ResNo];
3215 From->removeUser(U);
3217 ToOp.Val->addUser(U);
3220 // Now that we have modified U, add it back to the CSE maps. If it already
3221 // exists there, recursively merge the results together.
3222 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3223 ReplaceAllUsesWith(U, Existing, Deleted);
3225 if (Deleted) Deleted->push_back(U);
3226 DeleteNodeNotInCSEMaps(U);
3231 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3232 /// uses of other values produced by From.Val alone. The Deleted vector is
3233 /// handled the same was as for ReplaceAllUsesWith.
3234 void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
3235 std::vector<SDNode*> *Deleted) {
3236 assert(From != To && "Cannot replace a value with itself");
3237 // Handle the simple, trivial, case efficiently.
3238 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
3239 ReplaceAllUsesWith(From, To, Deleted);
3243 // Get all of the users of From.Val. We want these in a nice,
3244 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3245 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
3247 std::vector<SDNode*> LocalDeletionVector;
3249 // Pick a deletion vector to use. If the user specified one, use theirs,
3250 // otherwise use a local one.
3251 std::vector<SDNode*> *DeleteVector = Deleted ? Deleted : &LocalDeletionVector;
3252 while (!Users.empty()) {
3253 // We know that this user uses some value of From. If it is the right
3254 // value, update it.
3255 SDNode *User = Users.back();
3258 // Scan for an operand that matches From.
3259 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3260 for (; Op != E; ++Op)
3261 if (*Op == From) break;
3263 // If there are no matches, the user must use some other result of From.
3264 if (Op == E) continue;
3266 // Okay, we know this user needs to be updated. Remove its old self
3267 // from the CSE maps.
3268 RemoveNodeFromCSEMaps(User);
3270 // Update all operands that match "From".
3271 for (; Op != E; ++Op) {
3273 From.Val->removeUser(User);
3275 To.Val->addUser(User);
3279 // Now that we have modified User, add it back to the CSE maps. If it
3280 // already exists there, recursively merge the results together.
3281 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
3282 if (!Existing) continue; // Continue on to next user.
3284 // If there was already an existing matching node, use ReplaceAllUsesWith
3285 // to replace the dead one with the existing one. However, this can cause
3286 // recursive merging of other unrelated nodes down the line. The merging
3287 // can cause deletion of nodes that used the old value. In this case,
3288 // we have to be certain to remove them from the Users set.
3289 unsigned NumDeleted = DeleteVector->size();
3290 ReplaceAllUsesWith(User, Existing, DeleteVector);
3292 // User is now dead.
3293 DeleteVector->push_back(User);
3294 DeleteNodeNotInCSEMaps(User);
3296 // We have to be careful here, because ReplaceAllUsesWith could have
3297 // deleted a user of From, which means there may be dangling pointers
3298 // in the "Users" setvector. Scan over the deleted node pointers and
3299 // remove them from the setvector.
3300 for (unsigned i = NumDeleted, e = DeleteVector->size(); i != e; ++i)
3301 Users.remove((*DeleteVector)[i]);
3303 // If the user doesn't need the set of deleted elements, don't retain them
3304 // to the next loop iteration.
3306 LocalDeletionVector.clear();
3311 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3312 /// their allnodes order. It returns the maximum id.
3313 unsigned SelectionDAG::AssignNodeIds() {
3315 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3322 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
3323 /// based on their topological order. It returns the maximum id and a vector
3324 /// of the SDNodes* in assigned order by reference.
3325 unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
3326 unsigned DAGSize = AllNodes.size();
3327 std::vector<unsigned> InDegree(DAGSize);
3328 std::vector<SDNode*> Sources;
3330 // Use a two pass approach to avoid using a std::map which is slow.
3332 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3335 unsigned Degree = N->use_size();
3336 InDegree[N->getNodeId()] = Degree;
3338 Sources.push_back(N);
3342 while (!Sources.empty()) {
3343 SDNode *N = Sources.back();
3345 TopOrder.push_back(N);
3346 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3348 unsigned Degree = --InDegree[P->getNodeId()];
3350 Sources.push_back(P);
3354 // Second pass, assign the actual topological order as node ids.
3356 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3358 (*TI)->setNodeId(Id++);
3365 //===----------------------------------------------------------------------===//
3367 //===----------------------------------------------------------------------===//
3369 // Out-of-line virtual method to give class a home.
3370 void SDNode::ANCHOR() {}
3371 void UnarySDNode::ANCHOR() {}
3372 void BinarySDNode::ANCHOR() {}
3373 void TernarySDNode::ANCHOR() {}
3374 void HandleSDNode::ANCHOR() {}
3375 void StringSDNode::ANCHOR() {}
3376 void ConstantSDNode::ANCHOR() {}
3377 void ConstantFPSDNode::ANCHOR() {}
3378 void GlobalAddressSDNode::ANCHOR() {}
3379 void FrameIndexSDNode::ANCHOR() {}
3380 void JumpTableSDNode::ANCHOR() {}
3381 void ConstantPoolSDNode::ANCHOR() {}
3382 void BasicBlockSDNode::ANCHOR() {}
3383 void SrcValueSDNode::ANCHOR() {}
3384 void RegisterSDNode::ANCHOR() {}
3385 void ExternalSymbolSDNode::ANCHOR() {}
3386 void CondCodeSDNode::ANCHOR() {}
3387 void VTSDNode::ANCHOR() {}
3388 void LoadSDNode::ANCHOR() {}
3389 void StoreSDNode::ANCHOR() {}
3391 HandleSDNode::~HandleSDNode() {
3392 SDVTList VTs = { 0, 0 };
3393 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
3396 GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3397 MVT::ValueType VT, int o)
3398 : SDNode(isa<GlobalVariable>(GA) &&
3399 cast<GlobalVariable>(GA)->isThreadLocal() ?
3401 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3403 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3404 getSDVTList(VT)), Offset(o) {
3405 TheGlobal = const_cast<GlobalValue*>(GA);
3408 /// Profile - Gather unique data for the node.
3410 void SDNode::Profile(FoldingSetNodeID &ID) {
3411 AddNodeIDNode(ID, this);
3414 /// getValueTypeList - Return a pointer to the specified value type.
3416 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
3417 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3422 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3423 /// indicated value. This method ignores uses of other values defined by this
3425 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
3426 assert(Value < getNumValues() && "Bad value!");
3428 // If there is only one value, this is easy.
3429 if (getNumValues() == 1)
3430 return use_size() == NUses;
3431 if (use_size() < NUses) return false;
3433 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3435 SmallPtrSet<SDNode*, 32> UsersHandled;
3437 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3439 if (User->getNumOperands() == 1 ||
3440 UsersHandled.insert(User)) // First time we've seen this?
3441 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3442 if (User->getOperand(i) == TheValue) {
3444 return false; // too many uses
3449 // Found exactly the right number of uses?
3454 /// hasAnyUseOfValue - Return true if there are any use of the indicated
3455 /// value. This method ignores uses of other values defined by this operation.
3456 bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3457 assert(Value < getNumValues() && "Bad value!");
3459 if (use_size() == 0) return false;
3461 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3463 SmallPtrSet<SDNode*, 32> UsersHandled;
3465 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3467 if (User->getNumOperands() == 1 ||
3468 UsersHandled.insert(User)) // First time we've seen this?
3469 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3470 if (User->getOperand(i) == TheValue) {
3479 /// isOnlyUse - Return true if this node is the only use of N.
3481 bool SDNode::isOnlyUse(SDNode *N) const {
3483 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3494 /// isOperand - Return true if this node is an operand of N.
3496 bool SDOperand::isOperand(SDNode *N) const {
3497 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3498 if (*this == N->getOperand(i))
3503 bool SDNode::isOperand(SDNode *N) const {
3504 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3505 if (this == N->OperandList[i].Val)
3510 static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
3511 SmallPtrSet<SDNode *, 32> &Visited) {
3512 if (found || !Visited.insert(N))
3515 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3516 SDNode *Op = N->getOperand(i).Val;
3521 findPredecessor(Op, P, found, Visited);
3525 /// isPredecessor - Return true if this node is a predecessor of N. This node
3526 /// is either an operand of N or it can be reached by recursively traversing
3527 /// up the operands.
3528 /// NOTE: this is an expensive method. Use it carefully.
3529 bool SDNode::isPredecessor(SDNode *N) const {
3530 SmallPtrSet<SDNode *, 32> Visited;
3532 findPredecessor(N, this, found, Visited);
3536 uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3537 assert(Num < NumOperands && "Invalid child # of SDNode!");
3538 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3541 std::string SDNode::getOperationName(const SelectionDAG *G) const {
3542 switch (getOpcode()) {
3544 if (getOpcode() < ISD::BUILTIN_OP_END)
3545 return "<<Unknown DAG Node>>";
3548 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
3549 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
3550 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
3552 TargetLowering &TLI = G->getTargetLoweringInfo();
3554 TLI.getTargetNodeName(getOpcode());
3555 if (Name) return Name;
3558 return "<<Unknown Target Node>>";
3561 case ISD::PCMARKER: return "PCMarker";
3562 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
3563 case ISD::SRCVALUE: return "SrcValue";
3564 case ISD::EntryToken: return "EntryToken";
3565 case ISD::TokenFactor: return "TokenFactor";
3566 case ISD::AssertSext: return "AssertSext";
3567 case ISD::AssertZext: return "AssertZext";
3569 case ISD::STRING: return "String";
3570 case ISD::BasicBlock: return "BasicBlock";
3571 case ISD::VALUETYPE: return "ValueType";
3572 case ISD::Register: return "Register";
3574 case ISD::Constant: return "Constant";
3575 case ISD::ConstantFP: return "ConstantFP";
3576 case ISD::GlobalAddress: return "GlobalAddress";
3577 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
3578 case ISD::FrameIndex: return "FrameIndex";
3579 case ISD::JumpTable: return "JumpTable";
3580 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
3581 case ISD::RETURNADDR: return "RETURNADDR";
3582 case ISD::FRAMEADDR: return "FRAMEADDR";
3583 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
3584 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3585 case ISD::EHSELECTION: return "EHSELECTION";
3586 case ISD::EH_RETURN: return "EH_RETURN";
3587 case ISD::ConstantPool: return "ConstantPool";
3588 case ISD::ExternalSymbol: return "ExternalSymbol";
3589 case ISD::INTRINSIC_WO_CHAIN: {
3590 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3591 return Intrinsic::getName((Intrinsic::ID)IID);
3593 case ISD::INTRINSIC_VOID:
3594 case ISD::INTRINSIC_W_CHAIN: {
3595 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
3596 return Intrinsic::getName((Intrinsic::ID)IID);
3599 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
3600 case ISD::TargetConstant: return "TargetConstant";
3601 case ISD::TargetConstantFP:return "TargetConstantFP";
3602 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
3603 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
3604 case ISD::TargetFrameIndex: return "TargetFrameIndex";
3605 case ISD::TargetJumpTable: return "TargetJumpTable";
3606 case ISD::TargetConstantPool: return "TargetConstantPool";
3607 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
3609 case ISD::CopyToReg: return "CopyToReg";
3610 case ISD::CopyFromReg: return "CopyFromReg";
3611 case ISD::UNDEF: return "undef";
3612 case ISD::MERGE_VALUES: return "merge_values";
3613 case ISD::INLINEASM: return "inlineasm";
3614 case ISD::LABEL: return "label";
3615 case ISD::HANDLENODE: return "handlenode";
3616 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
3617 case ISD::CALL: return "call";
3620 case ISD::FABS: return "fabs";
3621 case ISD::FNEG: return "fneg";
3622 case ISD::FSQRT: return "fsqrt";
3623 case ISD::FSIN: return "fsin";
3624 case ISD::FCOS: return "fcos";
3625 case ISD::FPOWI: return "fpowi";
3626 case ISD::FPOW: return "fpow";
3629 case ISD::ADD: return "add";
3630 case ISD::SUB: return "sub";
3631 case ISD::MUL: return "mul";
3632 case ISD::MULHU: return "mulhu";
3633 case ISD::MULHS: return "mulhs";
3634 case ISD::SDIV: return "sdiv";
3635 case ISD::UDIV: return "udiv";
3636 case ISD::SREM: return "srem";
3637 case ISD::UREM: return "urem";
3638 case ISD::SMUL_LOHI: return "smul_lohi";
3639 case ISD::UMUL_LOHI: return "umul_lohi";
3640 case ISD::SDIVREM: return "sdivrem";
3641 case ISD::UDIVREM: return "divrem";
3642 case ISD::AND: return "and";
3643 case ISD::OR: return "or";
3644 case ISD::XOR: return "xor";
3645 case ISD::SHL: return "shl";
3646 case ISD::SRA: return "sra";
3647 case ISD::SRL: return "srl";
3648 case ISD::ROTL: return "rotl";
3649 case ISD::ROTR: return "rotr";
3650 case ISD::FADD: return "fadd";
3651 case ISD::FSUB: return "fsub";
3652 case ISD::FMUL: return "fmul";
3653 case ISD::FDIV: return "fdiv";
3654 case ISD::FREM: return "frem";
3655 case ISD::FCOPYSIGN: return "fcopysign";
3657 case ISD::SETCC: return "setcc";
3658 case ISD::SELECT: return "select";
3659 case ISD::SELECT_CC: return "select_cc";
3660 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
3661 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
3662 case ISD::CONCAT_VECTORS: return "concat_vectors";
3663 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
3664 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
3665 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
3666 case ISD::CARRY_FALSE: return "carry_false";
3667 case ISD::ADDC: return "addc";
3668 case ISD::ADDE: return "adde";
3669 case ISD::SUBC: return "subc";
3670 case ISD::SUBE: return "sube";
3671 case ISD::SHL_PARTS: return "shl_parts";
3672 case ISD::SRA_PARTS: return "sra_parts";
3673 case ISD::SRL_PARTS: return "srl_parts";
3675 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3676 case ISD::INSERT_SUBREG: return "insert_subreg";
3678 // Conversion operators.
3679 case ISD::SIGN_EXTEND: return "sign_extend";
3680 case ISD::ZERO_EXTEND: return "zero_extend";
3681 case ISD::ANY_EXTEND: return "any_extend";
3682 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
3683 case ISD::TRUNCATE: return "truncate";
3684 case ISD::FP_ROUND: return "fp_round";
3685 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
3686 case ISD::FP_EXTEND: return "fp_extend";
3688 case ISD::SINT_TO_FP: return "sint_to_fp";
3689 case ISD::UINT_TO_FP: return "uint_to_fp";
3690 case ISD::FP_TO_SINT: return "fp_to_sint";
3691 case ISD::FP_TO_UINT: return "fp_to_uint";
3692 case ISD::BIT_CONVERT: return "bit_convert";
3694 // Control flow instructions
3695 case ISD::BR: return "br";
3696 case ISD::BRIND: return "brind";
3697 case ISD::BR_JT: return "br_jt";
3698 case ISD::BRCOND: return "brcond";
3699 case ISD::BR_CC: return "br_cc";
3700 case ISD::RET: return "ret";
3701 case ISD::CALLSEQ_START: return "callseq_start";
3702 case ISD::CALLSEQ_END: return "callseq_end";
3705 case ISD::LOAD: return "load";
3706 case ISD::STORE: return "store";
3707 case ISD::VAARG: return "vaarg";
3708 case ISD::VACOPY: return "vacopy";
3709 case ISD::VAEND: return "vaend";
3710 case ISD::VASTART: return "vastart";
3711 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
3712 case ISD::EXTRACT_ELEMENT: return "extract_element";
3713 case ISD::BUILD_PAIR: return "build_pair";
3714 case ISD::STACKSAVE: return "stacksave";
3715 case ISD::STACKRESTORE: return "stackrestore";
3717 // Block memory operations.
3718 case ISD::MEMSET: return "memset";
3719 case ISD::MEMCPY: return "memcpy";
3720 case ISD::MEMMOVE: return "memmove";
3723 case ISD::BSWAP: return "bswap";
3724 case ISD::CTPOP: return "ctpop";
3725 case ISD::CTTZ: return "cttz";
3726 case ISD::CTLZ: return "ctlz";
3729 case ISD::LOCATION: return "location";
3730 case ISD::DEBUG_LOC: return "debug_loc";
3733 case ISD::TRAMPOLINE: return "trampoline";
3736 switch (cast<CondCodeSDNode>(this)->get()) {
3737 default: assert(0 && "Unknown setcc condition!");
3738 case ISD::SETOEQ: return "setoeq";
3739 case ISD::SETOGT: return "setogt";
3740 case ISD::SETOGE: return "setoge";
3741 case ISD::SETOLT: return "setolt";
3742 case ISD::SETOLE: return "setole";
3743 case ISD::SETONE: return "setone";
3745 case ISD::SETO: return "seto";
3746 case ISD::SETUO: return "setuo";
3747 case ISD::SETUEQ: return "setue";
3748 case ISD::SETUGT: return "setugt";
3749 case ISD::SETUGE: return "setuge";
3750 case ISD::SETULT: return "setult";
3751 case ISD::SETULE: return "setule";
3752 case ISD::SETUNE: return "setune";
3754 case ISD::SETEQ: return "seteq";
3755 case ISD::SETGT: return "setgt";
3756 case ISD::SETGE: return "setge";
3757 case ISD::SETLT: return "setlt";
3758 case ISD::SETLE: return "setle";
3759 case ISD::SETNE: return "setne";
3764 const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
3773 return "<post-inc>";
3775 return "<post-dec>";
3779 void SDNode::dump() const { dump(0); }
3780 void SDNode::dump(const SelectionDAG *G) const {
3781 cerr << (void*)this << ": ";
3783 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
3785 if (getValueType(i) == MVT::Other)
3788 cerr << MVT::getValueTypeString(getValueType(i));
3790 cerr << " = " << getOperationName(G);
3793 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
3794 if (i) cerr << ", ";
3795 cerr << (void*)getOperand(i).Val;
3796 if (unsigned RN = getOperand(i).ResNo)
3800 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
3801 cerr << "<" << CSDN->getValue() << ">";
3802 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
3803 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
3804 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
3805 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
3806 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
3808 cerr << "<APFloat(";
3809 CSDN->getValueAPF().convertToAPInt().dump();
3812 } else if (const GlobalAddressSDNode *GADN =
3813 dyn_cast<GlobalAddressSDNode>(this)) {
3814 int offset = GADN->getOffset();
3816 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
3818 cerr << " + " << offset;
3820 cerr << " " << offset;
3821 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
3822 cerr << "<" << FIDN->getIndex() << ">";
3823 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
3824 cerr << "<" << JTDN->getIndex() << ">";
3825 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
3826 int offset = CP->getOffset();
3827 if (CP->isMachineConstantPoolEntry())
3828 cerr << "<" << *CP->getMachineCPVal() << ">";
3830 cerr << "<" << *CP->getConstVal() << ">";
3832 cerr << " + " << offset;
3834 cerr << " " << offset;
3835 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
3837 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3839 cerr << LBB->getName() << " ";
3840 cerr << (const void*)BBDN->getBasicBlock() << ">";
3841 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
3842 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
3843 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
3845 cerr << " #" << R->getReg();
3847 } else if (const ExternalSymbolSDNode *ES =
3848 dyn_cast<ExternalSymbolSDNode>(this)) {
3849 cerr << "'" << ES->getSymbol() << "'";
3850 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3852 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
3854 cerr << "<null:" << M->getOffset() << ">";
3855 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
3856 cerr << ":" << MVT::getValueTypeString(N->getVT());
3857 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
3859 switch (LD->getExtensionType()) {
3860 default: doExt = false; break;
3862 cerr << " <anyext ";
3872 cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
3874 const char *AM = getIndexedModeName(LD->getAddressingMode());
3877 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
3878 if (ST->isTruncatingStore())
3880 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
3882 const char *AM = getIndexedModeName(ST->getAddressingMode());
3888 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
3889 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3890 if (N->getOperand(i).Val->hasOneUse())
3891 DumpNodes(N->getOperand(i).Val, indent+2, G);
3893 cerr << "\n" << std::string(indent+2, ' ')
3894 << (void*)N->getOperand(i).Val << ": <multiple use>";
3897 cerr << "\n" << std::string(indent, ' ');
3901 void SelectionDAG::dump() const {
3902 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
3903 std::vector<const SDNode*> Nodes;
3904 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3908 std::sort(Nodes.begin(), Nodes.end());
3910 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
3911 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
3912 DumpNodes(Nodes[i], 2, this);
3915 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
3920 const Type *ConstantPoolSDNode::getType() const {
3921 if (isMachineConstantPoolEntry())
3922 return Val.MachineCPVal->getType();
3923 return Val.ConstVal->getType();