1 //===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements the SelectionDAG class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/SelectionDAG.h"
15 #include "llvm/Constants.h"
16 #include "llvm/GlobalVariable.h"
17 #include "llvm/Intrinsics.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Assembly/Writer.h"
20 #include "llvm/CodeGen/MachineBasicBlock.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/Support/MathExtras.h"
24 #include "llvm/Target/MRegisterInfo.h"
25 #include "llvm/Target/TargetData.h"
26 #include "llvm/Target/TargetLowering.h"
27 #include "llvm/Target/TargetInstrInfo.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/ADT/SetVector.h"
30 #include "llvm/ADT/SmallPtrSet.h"
31 #include "llvm/ADT/SmallVector.h"
32 #include "llvm/ADT/StringExtras.h"
37 /// makeVTList - Return an instance of the SDVTList struct initialized with the
38 /// specified members.
39 static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
40 SDVTList Res = {VTs, NumVTs};
44 //===----------------------------------------------------------------------===//
45 // ConstantFPSDNode Class
46 //===----------------------------------------------------------------------===//
48 /// isExactlyValue - We don't rely on operator== working on double values, as
49 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
50 /// As such, this method can be used to do an exact bit-for-bit comparison of
51 /// two floating point values.
52 bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
53 return Value.bitwiseIsEqual(V);
56 bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
58 // convert modifies in place, so make a copy.
59 APFloat Val2 = APFloat(Val);
62 return false; // These can't be represented as floating point!
64 // FIXME rounding mode needs to be more flexible
66 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
67 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) ==
70 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
71 &Val2.getSemantics() == &APFloat::IEEEdouble ||
72 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
74 // TODO: Figure out how to test if we can use a shorter type instead!
82 //===----------------------------------------------------------------------===//
84 //===----------------------------------------------------------------------===//
86 /// isBuildVectorAllOnes - Return true if the specified node is a
87 /// BUILD_VECTOR where all of the elements are ~0 or undef.
88 bool ISD::isBuildVectorAllOnes(const SDNode *N) {
89 // Look through a bit convert.
90 if (N->getOpcode() == ISD::BIT_CONVERT)
91 N = N->getOperand(0).Val;
93 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
95 unsigned i = 0, e = N->getNumOperands();
97 // Skip over all of the undef values.
98 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
101 // Do not accept an all-undef vector.
102 if (i == e) return false;
104 // Do not accept build_vectors that aren't all constants or which have non-~0
106 SDOperand NotZero = N->getOperand(i);
107 if (isa<ConstantSDNode>(NotZero)) {
108 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
110 } else if (isa<ConstantFPSDNode>(NotZero)) {
111 MVT::ValueType VT = NotZero.getValueType();
113 if (((cast<ConstantFPSDNode>(NotZero)->getValueAPF().
114 convertToAPInt().getZExtValue())) != (uint64_t)-1)
117 if ((uint32_t)cast<ConstantFPSDNode>(NotZero)->
118 getValueAPF().convertToAPInt().getZExtValue() !=
125 // Okay, we have at least one ~0 value, check to see if the rest match or are
127 for (++i; i != e; ++i)
128 if (N->getOperand(i) != NotZero &&
129 N->getOperand(i).getOpcode() != ISD::UNDEF)
135 /// isBuildVectorAllZeros - Return true if the specified node is a
136 /// BUILD_VECTOR where all of the elements are 0 or undef.
137 bool ISD::isBuildVectorAllZeros(const SDNode *N) {
138 // Look through a bit convert.
139 if (N->getOpcode() == ISD::BIT_CONVERT)
140 N = N->getOperand(0).Val;
142 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
144 unsigned i = 0, e = N->getNumOperands();
146 // Skip over all of the undef values.
147 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
150 // Do not accept an all-undef vector.
151 if (i == e) return false;
153 // Do not accept build_vectors that aren't all constants or which have non-~0
155 SDOperand Zero = N->getOperand(i);
156 if (isa<ConstantSDNode>(Zero)) {
157 if (!cast<ConstantSDNode>(Zero)->isNullValue())
159 } else if (isa<ConstantFPSDNode>(Zero)) {
160 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
165 // Okay, we have at least one ~0 value, check to see if the rest match or are
167 for (++i; i != e; ++i)
168 if (N->getOperand(i) != Zero &&
169 N->getOperand(i).getOpcode() != ISD::UNDEF)
174 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
175 /// when given the operation for (X op Y).
176 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
177 // To perform this operation, we just need to swap the L and G bits of the
179 unsigned OldL = (Operation >> 2) & 1;
180 unsigned OldG = (Operation >> 1) & 1;
181 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
182 (OldL << 1) | // New G bit
183 (OldG << 2)); // New L bit.
186 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
187 /// 'op' is a valid SetCC operation.
188 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
189 unsigned Operation = Op;
191 Operation ^= 7; // Flip L, G, E bits, but not U.
193 Operation ^= 15; // Flip all of the condition bits.
194 if (Operation > ISD::SETTRUE2)
195 Operation &= ~8; // Don't let N and U bits get set.
196 return ISD::CondCode(Operation);
200 /// isSignedOp - For an integer comparison, return 1 if the comparison is a
201 /// signed operation and 2 if the result is an unsigned comparison. Return zero
202 /// if the operation does not depend on the sign of the input (setne and seteq).
203 static int isSignedOp(ISD::CondCode Opcode) {
205 default: assert(0 && "Illegal integer setcc operation!");
207 case ISD::SETNE: return 0;
211 case ISD::SETGE: return 1;
215 case ISD::SETUGE: return 2;
219 /// getSetCCOrOperation - Return the result of a logical OR between different
220 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
221 /// returns SETCC_INVALID if it is not possible to represent the resultant
223 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
225 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
226 // Cannot fold a signed integer setcc with an unsigned integer setcc.
227 return ISD::SETCC_INVALID;
229 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
231 // If the N and U bits get set then the resultant comparison DOES suddenly
232 // care about orderedness, and is true when ordered.
233 if (Op > ISD::SETTRUE2)
234 Op &= ~16; // Clear the U bit if the N bit is set.
236 // Canonicalize illegal integer setcc's.
237 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
240 return ISD::CondCode(Op);
243 /// getSetCCAndOperation - Return the result of a logical AND between different
244 /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
245 /// function returns zero if it is not possible to represent the resultant
247 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
249 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
250 // Cannot fold a signed setcc with an unsigned setcc.
251 return ISD::SETCC_INVALID;
253 // Combine all of the condition bits.
254 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
256 // Canonicalize illegal integer setcc's.
260 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
261 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
262 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
263 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
270 const TargetMachine &SelectionDAG::getTarget() const {
271 return TLI.getTargetMachine();
274 //===----------------------------------------------------------------------===//
275 // SDNode Profile Support
276 //===----------------------------------------------------------------------===//
278 /// AddNodeIDOpcode - Add the node opcode to the NodeID data.
280 static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
284 /// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
285 /// solely with their pointer.
286 void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
287 ID.AddPointer(VTList.VTs);
290 /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
292 static void AddNodeIDOperands(FoldingSetNodeID &ID,
293 const SDOperand *Ops, unsigned NumOps) {
294 for (; NumOps; --NumOps, ++Ops) {
295 ID.AddPointer(Ops->Val);
296 ID.AddInteger(Ops->ResNo);
300 static void AddNodeIDNode(FoldingSetNodeID &ID,
301 unsigned short OpC, SDVTList VTList,
302 const SDOperand *OpList, unsigned N) {
303 AddNodeIDOpcode(ID, OpC);
304 AddNodeIDValueTypes(ID, VTList);
305 AddNodeIDOperands(ID, OpList, N);
308 /// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
310 static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
311 AddNodeIDOpcode(ID, N->getOpcode());
312 // Add the return value info.
313 AddNodeIDValueTypes(ID, N->getVTList());
314 // Add the operand info.
315 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
317 // Handle SDNode leafs with special info.
318 switch (N->getOpcode()) {
319 default: break; // Normal nodes don't need extra info.
320 case ISD::TargetConstant:
322 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
324 case ISD::TargetConstantFP:
325 case ISD::ConstantFP: {
326 ID.AddAPFloat(cast<ConstantFPSDNode>(N)->getValueAPF());
329 case ISD::TargetGlobalAddress:
330 case ISD::GlobalAddress:
331 case ISD::TargetGlobalTLSAddress:
332 case ISD::GlobalTLSAddress: {
333 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
334 ID.AddPointer(GA->getGlobal());
335 ID.AddInteger(GA->getOffset());
338 case ISD::BasicBlock:
339 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
342 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
344 case ISD::SRCVALUE: {
345 SrcValueSDNode *SV = cast<SrcValueSDNode>(N);
346 ID.AddPointer(SV->getValue());
347 ID.AddInteger(SV->getOffset());
350 case ISD::FrameIndex:
351 case ISD::TargetFrameIndex:
352 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
355 case ISD::TargetJumpTable:
356 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
358 case ISD::ConstantPool:
359 case ISD::TargetConstantPool: {
360 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
361 ID.AddInteger(CP->getAlignment());
362 ID.AddInteger(CP->getOffset());
363 if (CP->isMachineConstantPoolEntry())
364 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
366 ID.AddPointer(CP->getConstVal());
370 LoadSDNode *LD = cast<LoadSDNode>(N);
371 ID.AddInteger(LD->getAddressingMode());
372 ID.AddInteger(LD->getExtensionType());
373 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
374 ID.AddPointer(LD->getSrcValue());
375 ID.AddInteger(LD->getSrcValueOffset());
376 ID.AddInteger(LD->getAlignment());
377 ID.AddInteger(LD->isVolatile());
381 StoreSDNode *ST = cast<StoreSDNode>(N);
382 ID.AddInteger(ST->getAddressingMode());
383 ID.AddInteger(ST->isTruncatingStore());
384 ID.AddInteger((unsigned int)(ST->getStoredVT()));
385 ID.AddPointer(ST->getSrcValue());
386 ID.AddInteger(ST->getSrcValueOffset());
387 ID.AddInteger(ST->getAlignment());
388 ID.AddInteger(ST->isVolatile());
394 //===----------------------------------------------------------------------===//
395 // SelectionDAG Class
396 //===----------------------------------------------------------------------===//
398 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
400 void SelectionDAG::RemoveDeadNodes() {
401 // Create a dummy node (which is not added to allnodes), that adds a reference
402 // to the root node, preventing it from being deleted.
403 HandleSDNode Dummy(getRoot());
405 SmallVector<SDNode*, 128> DeadNodes;
407 // Add all obviously-dead nodes to the DeadNodes worklist.
408 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
410 DeadNodes.push_back(I);
412 // Process the worklist, deleting the nodes and adding their uses to the
414 while (!DeadNodes.empty()) {
415 SDNode *N = DeadNodes.back();
416 DeadNodes.pop_back();
418 // Take the node out of the appropriate CSE map.
419 RemoveNodeFromCSEMaps(N);
421 // Next, brutally remove the operand list. This is safe to do, as there are
422 // no cycles in the graph.
423 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
424 SDNode *Operand = I->Val;
425 Operand->removeUser(N);
427 // Now that we removed this operand, see if there are no uses of it left.
428 if (Operand->use_empty())
429 DeadNodes.push_back(Operand);
431 if (N->OperandsNeedDelete)
432 delete[] N->OperandList;
436 // Finally, remove N itself.
440 // If the root changed (e.g. it was a dead load, update the root).
441 setRoot(Dummy.getValue());
444 void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
445 SmallVector<SDNode*, 16> DeadNodes;
446 DeadNodes.push_back(N);
448 // Process the worklist, deleting the nodes and adding their uses to the
450 while (!DeadNodes.empty()) {
451 SDNode *N = DeadNodes.back();
452 DeadNodes.pop_back();
454 // Take the node out of the appropriate CSE map.
455 RemoveNodeFromCSEMaps(N);
457 // Next, brutally remove the operand list. This is safe to do, as there are
458 // no cycles in the graph.
459 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
460 SDNode *Operand = I->Val;
461 Operand->removeUser(N);
463 // Now that we removed this operand, see if there are no uses of it left.
464 if (Operand->use_empty())
465 DeadNodes.push_back(Operand);
467 if (N->OperandsNeedDelete)
468 delete[] N->OperandList;
472 // Finally, remove N itself.
473 Deleted.push_back(N);
478 void SelectionDAG::DeleteNode(SDNode *N) {
479 assert(N->use_empty() && "Cannot delete a node that is not dead!");
481 // First take this out of the appropriate CSE map.
482 RemoveNodeFromCSEMaps(N);
484 // Finally, remove uses due to operands of this node, remove from the
485 // AllNodes list, and delete the node.
486 DeleteNodeNotInCSEMaps(N);
489 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
491 // Remove it from the AllNodes list.
494 // Drop all of the operands and decrement used nodes use counts.
495 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
496 I->Val->removeUser(N);
497 if (N->OperandsNeedDelete)
498 delete[] N->OperandList;
505 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
506 /// correspond to it. This is useful when we're about to delete or repurpose
507 /// the node. We don't want future request for structurally identical nodes
508 /// to return N anymore.
509 void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
511 switch (N->getOpcode()) {
512 case ISD::HANDLENODE: return; // noop.
514 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
517 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
518 "Cond code doesn't exist!");
519 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
520 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
522 case ISD::ExternalSymbol:
523 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
525 case ISD::TargetExternalSymbol:
527 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
530 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
531 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
534 // Remove it from the CSE Map.
535 Erased = CSEMap.RemoveNode(N);
539 // Verify that the node was actually in one of the CSE maps, unless it has a
540 // flag result (which cannot be CSE'd) or is one of the special cases that are
541 // not subject to CSE.
542 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
543 !N->isTargetOpcode()) {
546 assert(0 && "Node is not in map!");
551 /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
552 /// has been taken out and modified in some way. If the specified node already
553 /// exists in the CSE maps, do not modify the maps, but return the existing node
554 /// instead. If it doesn't exist, add it and return null.
556 SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
557 assert(N->getNumOperands() && "This is a leaf node!");
558 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
559 return 0; // Never add these nodes.
561 // Check that remaining values produced are not flags.
562 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
563 if (N->getValueType(i) == MVT::Flag)
564 return 0; // Never CSE anything that produces a flag.
566 SDNode *New = CSEMap.GetOrInsertNode(N);
567 if (New != N) return New; // Node already existed.
571 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
572 /// were replaced with those specified. If this node is never memoized,
573 /// return null, otherwise return a pointer to the slot it would take. If a
574 /// node already exists with these operands, the slot will be non-null.
575 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
577 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
578 return 0; // Never add these nodes.
580 // Check that remaining values produced are not flags.
581 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
582 if (N->getValueType(i) == MVT::Flag)
583 return 0; // Never CSE anything that produces a flag.
585 SDOperand Ops[] = { Op };
587 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
588 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
591 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
592 /// were replaced with those specified. If this node is never memoized,
593 /// return null, otherwise return a pointer to the slot it would take. If a
594 /// node already exists with these operands, the slot will be non-null.
595 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
596 SDOperand Op1, SDOperand Op2,
598 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
599 return 0; // Never add these nodes.
601 // Check that remaining values produced are not flags.
602 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
603 if (N->getValueType(i) == MVT::Flag)
604 return 0; // Never CSE anything that produces a flag.
606 SDOperand Ops[] = { Op1, Op2 };
608 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
609 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
613 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
614 /// were replaced with those specified. If this node is never memoized,
615 /// return null, otherwise return a pointer to the slot it would take. If a
616 /// node already exists with these operands, the slot will be non-null.
617 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
618 const SDOperand *Ops,unsigned NumOps,
620 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
621 return 0; // Never add these nodes.
623 // Check that remaining values produced are not flags.
624 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
625 if (N->getValueType(i) == MVT::Flag)
626 return 0; // Never CSE anything that produces a flag.
629 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
631 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
632 ID.AddInteger(LD->getAddressingMode());
633 ID.AddInteger(LD->getExtensionType());
634 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
635 ID.AddPointer(LD->getSrcValue());
636 ID.AddInteger(LD->getSrcValueOffset());
637 ID.AddInteger(LD->getAlignment());
638 ID.AddInteger(LD->isVolatile());
639 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
640 ID.AddInteger(ST->getAddressingMode());
641 ID.AddInteger(ST->isTruncatingStore());
642 ID.AddInteger((unsigned int)(ST->getStoredVT()));
643 ID.AddPointer(ST->getSrcValue());
644 ID.AddInteger(ST->getSrcValueOffset());
645 ID.AddInteger(ST->getAlignment());
646 ID.AddInteger(ST->isVolatile());
649 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
653 SelectionDAG::~SelectionDAG() {
654 while (!AllNodes.empty()) {
655 SDNode *N = AllNodes.begin();
656 N->SetNextInBucket(0);
657 if (N->OperandsNeedDelete)
658 delete [] N->OperandList;
661 AllNodes.pop_front();
665 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
666 if (Op.getValueType() == VT) return Op;
667 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
668 return getNode(ISD::AND, Op.getValueType(), Op,
669 getConstant(Imm, Op.getValueType()));
672 SDOperand SelectionDAG::getString(const std::string &Val) {
673 StringSDNode *&N = StringNodes[Val];
675 N = new StringSDNode(Val);
676 AllNodes.push_back(N);
678 return SDOperand(N, 0);
681 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
682 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
683 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
685 // Mask out any bits that are not valid for this constant.
686 Val &= MVT::getIntVTBitMask(VT);
688 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
690 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
693 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
694 return SDOperand(E, 0);
695 SDNode *N = new ConstantSDNode(isT, Val, VT);
696 CSEMap.InsertNode(N, IP);
697 AllNodes.push_back(N);
698 return SDOperand(N, 0);
701 SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
703 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
705 MVT::ValueType EltVT =
706 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
708 // Do the map lookup using the actual bit pattern for the floating point
709 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
710 // we don't have issues with SNANs.
711 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
713 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
717 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
718 if (!MVT::isVector(VT))
719 return SDOperand(N, 0);
721 N = new ConstantFPSDNode(isTarget, V, EltVT);
722 CSEMap.InsertNode(N, IP);
723 AllNodes.push_back(N);
726 SDOperand Result(N, 0);
727 if (MVT::isVector(VT)) {
728 SmallVector<SDOperand, 8> Ops;
729 Ops.assign(MVT::getVectorNumElements(VT), Result);
730 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
735 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
737 MVT::ValueType EltVT =
738 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
740 return getConstantFP(APFloat((float)Val), VT, isTarget);
742 return getConstantFP(APFloat(Val), VT, isTarget);
745 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
746 MVT::ValueType VT, int Offset,
748 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
750 if (GVar && GVar->isThreadLocal())
751 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
753 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
755 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
757 ID.AddInteger(Offset);
759 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
760 return SDOperand(E, 0);
761 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
762 CSEMap.InsertNode(N, IP);
763 AllNodes.push_back(N);
764 return SDOperand(N, 0);
767 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
769 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
771 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
774 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
775 return SDOperand(E, 0);
776 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
777 CSEMap.InsertNode(N, IP);
778 AllNodes.push_back(N);
779 return SDOperand(N, 0);
782 SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
783 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
785 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
788 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
789 return SDOperand(E, 0);
790 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
791 CSEMap.InsertNode(N, IP);
792 AllNodes.push_back(N);
793 return SDOperand(N, 0);
796 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
797 unsigned Alignment, int Offset,
799 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
801 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
802 ID.AddInteger(Alignment);
803 ID.AddInteger(Offset);
806 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
807 return SDOperand(E, 0);
808 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
809 CSEMap.InsertNode(N, IP);
810 AllNodes.push_back(N);
811 return SDOperand(N, 0);
815 SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
817 unsigned Alignment, int Offset,
819 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
821 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
822 ID.AddInteger(Alignment);
823 ID.AddInteger(Offset);
824 C->AddSelectionDAGCSEId(ID);
826 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
827 return SDOperand(E, 0);
828 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
829 CSEMap.InsertNode(N, IP);
830 AllNodes.push_back(N);
831 return SDOperand(N, 0);
835 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
837 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
840 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
841 return SDOperand(E, 0);
842 SDNode *N = new BasicBlockSDNode(MBB);
843 CSEMap.InsertNode(N, IP);
844 AllNodes.push_back(N);
845 return SDOperand(N, 0);
848 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
849 if ((unsigned)VT >= ValueTypeNodes.size())
850 ValueTypeNodes.resize(VT+1);
851 if (ValueTypeNodes[VT] == 0) {
852 ValueTypeNodes[VT] = new VTSDNode(VT);
853 AllNodes.push_back(ValueTypeNodes[VT]);
856 return SDOperand(ValueTypeNodes[VT], 0);
859 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
860 SDNode *&N = ExternalSymbols[Sym];
861 if (N) return SDOperand(N, 0);
862 N = new ExternalSymbolSDNode(false, Sym, VT);
863 AllNodes.push_back(N);
864 return SDOperand(N, 0);
867 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
869 SDNode *&N = TargetExternalSymbols[Sym];
870 if (N) return SDOperand(N, 0);
871 N = new ExternalSymbolSDNode(true, Sym, VT);
872 AllNodes.push_back(N);
873 return SDOperand(N, 0);
876 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
877 if ((unsigned)Cond >= CondCodeNodes.size())
878 CondCodeNodes.resize(Cond+1);
880 if (CondCodeNodes[Cond] == 0) {
881 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
882 AllNodes.push_back(CondCodeNodes[Cond]);
884 return SDOperand(CondCodeNodes[Cond], 0);
887 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
889 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
890 ID.AddInteger(RegNo);
892 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
893 return SDOperand(E, 0);
894 SDNode *N = new RegisterSDNode(RegNo, VT);
895 CSEMap.InsertNode(N, IP);
896 AllNodes.push_back(N);
897 return SDOperand(N, 0);
900 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
901 assert((!V || isa<PointerType>(V->getType())) &&
902 "SrcValue is not a pointer?");
905 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
907 ID.AddInteger(Offset);
909 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
910 return SDOperand(E, 0);
911 SDNode *N = new SrcValueSDNode(V, Offset);
912 CSEMap.InsertNode(N, IP);
913 AllNodes.push_back(N);
914 return SDOperand(N, 0);
917 /// CreateStackTemporary - Create a stack temporary, suitable for holding the
918 /// specified value type.
919 SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
920 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
921 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
922 const Type *Ty = MVT::getTypeForValueType(VT);
923 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
924 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
925 return getFrameIndex(FrameIdx, TLI.getPointerTy());
929 SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
930 SDOperand N2, ISD::CondCode Cond) {
931 // These setcc operations always fold.
935 case ISD::SETFALSE2: return getConstant(0, VT);
937 case ISD::SETTRUE2: return getConstant(1, VT);
949 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
953 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
954 uint64_t C2 = N2C->getValue();
955 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
956 uint64_t C1 = N1C->getValue();
958 // Sign extend the operands if required
959 if (ISD::isSignedIntSetCC(Cond)) {
960 C1 = N1C->getSignExtended();
961 C2 = N2C->getSignExtended();
965 default: assert(0 && "Unknown integer setcc!");
966 case ISD::SETEQ: return getConstant(C1 == C2, VT);
967 case ISD::SETNE: return getConstant(C1 != C2, VT);
968 case ISD::SETULT: return getConstant(C1 < C2, VT);
969 case ISD::SETUGT: return getConstant(C1 > C2, VT);
970 case ISD::SETULE: return getConstant(C1 <= C2, VT);
971 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
972 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
973 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
974 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
975 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
979 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
980 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
981 // No compile time operations on this type yet.
982 if (N1C->getValueType(0) == MVT::ppcf128)
985 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
988 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
989 return getNode(ISD::UNDEF, VT);
991 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
992 case ISD::SETNE: if (R==APFloat::cmpUnordered)
993 return getNode(ISD::UNDEF, VT);
995 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
996 R==APFloat::cmpLessThan, VT);
997 case ISD::SETLT: if (R==APFloat::cmpUnordered)
998 return getNode(ISD::UNDEF, VT);
1000 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1001 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1002 return getNode(ISD::UNDEF, VT);
1004 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1005 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1006 return getNode(ISD::UNDEF, VT);
1008 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
1009 R==APFloat::cmpEqual, VT);
1010 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1011 return getNode(ISD::UNDEF, VT);
1013 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
1014 R==APFloat::cmpEqual, VT);
1015 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1016 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1017 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1018 R==APFloat::cmpEqual, VT);
1019 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1020 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1021 R==APFloat::cmpLessThan, VT);
1022 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1023 R==APFloat::cmpUnordered, VT);
1024 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1025 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
1028 // Ensure that the constant occurs on the RHS.
1029 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
1032 // Could not fold it.
1036 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1037 /// this predicate to simplify operations downstream. Mask is known to be zero
1038 /// for bits that V cannot have.
1039 bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1040 unsigned Depth) const {
1041 // The masks are not wide enough to represent this type! Should use APInt.
1042 if (Op.getValueType() == MVT::i128)
1045 uint64_t KnownZero, KnownOne;
1046 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1047 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1048 return (KnownZero & Mask) == Mask;
1051 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
1052 /// known to be either zero or one and return them in the KnownZero/KnownOne
1053 /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1055 void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1056 uint64_t &KnownZero, uint64_t &KnownOne,
1057 unsigned Depth) const {
1058 KnownZero = KnownOne = 0; // Don't know anything.
1059 if (Depth == 6 || Mask == 0)
1060 return; // Limit search depth.
1062 // The masks are not wide enough to represent this type! Should use APInt.
1063 if (Op.getValueType() == MVT::i128)
1066 uint64_t KnownZero2, KnownOne2;
1068 switch (Op.getOpcode()) {
1070 // We know all of the bits for a constant!
1071 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1072 KnownZero = ~KnownOne & Mask;
1075 // If either the LHS or the RHS are Zero, the result is zero.
1076 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1078 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1079 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1080 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1082 // Output known-1 bits are only known if set in both the LHS & RHS.
1083 KnownOne &= KnownOne2;
1084 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1085 KnownZero |= KnownZero2;
1088 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1090 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1091 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1092 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1094 // Output known-0 bits are only known if clear in both the LHS & RHS.
1095 KnownZero &= KnownZero2;
1096 // Output known-1 are known to be set if set in either the LHS | RHS.
1097 KnownOne |= KnownOne2;
1100 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1101 ComputeMaskedBits(Op.getOperand(0), 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 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1106 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1107 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1108 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1109 KnownZero = KnownZeroOut;
1113 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1114 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1115 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1116 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1118 // Only known if known in both the LHS and RHS.
1119 KnownOne &= KnownOne2;
1120 KnownZero &= KnownZero2;
1122 case ISD::SELECT_CC:
1123 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1124 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1125 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1126 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1128 // Only known if known in both the LHS and RHS.
1129 KnownOne &= KnownOne2;
1130 KnownZero &= KnownZero2;
1133 // If we know the result of a setcc has the top bits zero, use this info.
1134 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1135 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1138 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1139 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1140 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1141 KnownZero, KnownOne, Depth+1);
1142 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1143 KnownZero <<= SA->getValue();
1144 KnownOne <<= SA->getValue();
1145 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1149 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1150 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1151 MVT::ValueType VT = Op.getValueType();
1152 unsigned ShAmt = SA->getValue();
1154 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1155 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1156 KnownZero, KnownOne, Depth+1);
1157 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1158 KnownZero &= TypeMask;
1159 KnownOne &= TypeMask;
1160 KnownZero >>= ShAmt;
1163 uint64_t HighBits = (1ULL << ShAmt)-1;
1164 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1165 KnownZero |= HighBits; // High bits known zero.
1169 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1170 MVT::ValueType VT = Op.getValueType();
1171 unsigned ShAmt = SA->getValue();
1173 // Compute the new bits that are at the top now.
1174 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1176 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1177 // If any of the demanded bits are produced by the sign extension, we also
1178 // demand the input sign bit.
1179 uint64_t HighBits = (1ULL << ShAmt)-1;
1180 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1181 if (HighBits & Mask)
1182 InDemandedMask |= MVT::getIntVTSignBit(VT);
1184 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1186 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1187 KnownZero &= TypeMask;
1188 KnownOne &= TypeMask;
1189 KnownZero >>= ShAmt;
1192 // Handle the sign bits.
1193 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1194 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1196 if (KnownZero & SignBit) {
1197 KnownZero |= HighBits; // New bits are known zero.
1198 } else if (KnownOne & SignBit) {
1199 KnownOne |= HighBits; // New bits are known one.
1203 case ISD::SIGN_EXTEND_INREG: {
1204 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1206 // Sign extension. Compute the demanded bits in the result that are not
1207 // present in the input.
1208 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1210 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1211 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1213 // If the sign extended bits are demanded, we know that the sign
1216 InputDemandedBits |= InSignBit;
1218 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1219 KnownZero, KnownOne, Depth+1);
1220 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1222 // If the sign bit of the input is known set or clear, then we know the
1223 // top bits of the result.
1224 if (KnownZero & InSignBit) { // Input sign bit known clear
1225 KnownZero |= NewBits;
1226 KnownOne &= ~NewBits;
1227 } else if (KnownOne & InSignBit) { // Input sign bit known set
1228 KnownOne |= NewBits;
1229 KnownZero &= ~NewBits;
1230 } else { // Input sign bit unknown
1231 KnownZero &= ~NewBits;
1232 KnownOne &= ~NewBits;
1239 MVT::ValueType VT = Op.getValueType();
1240 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1241 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1246 if (ISD::isZEXTLoad(Op.Val)) {
1247 LoadSDNode *LD = cast<LoadSDNode>(Op);
1248 MVT::ValueType VT = LD->getLoadedVT();
1249 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1253 case ISD::ZERO_EXTEND: {
1254 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1255 uint64_t NewBits = (~InMask) & Mask;
1256 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1258 KnownZero |= NewBits & Mask;
1259 KnownOne &= ~NewBits;
1262 case ISD::SIGN_EXTEND: {
1263 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1264 unsigned InBits = MVT::getSizeInBits(InVT);
1265 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1266 uint64_t InSignBit = 1ULL << (InBits-1);
1267 uint64_t NewBits = (~InMask) & Mask;
1268 uint64_t InDemandedBits = Mask & InMask;
1270 // If any of the sign extended bits are demanded, we know that the sign
1273 InDemandedBits |= InSignBit;
1275 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1277 // If the sign bit is known zero or one, the top bits match.
1278 if (KnownZero & InSignBit) {
1279 KnownZero |= NewBits;
1280 KnownOne &= ~NewBits;
1281 } else if (KnownOne & InSignBit) {
1282 KnownOne |= NewBits;
1283 KnownZero &= ~NewBits;
1284 } else { // Otherwise, top bits aren't known.
1285 KnownOne &= ~NewBits;
1286 KnownZero &= ~NewBits;
1290 case ISD::ANY_EXTEND: {
1291 MVT::ValueType VT = Op.getOperand(0).getValueType();
1292 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1293 KnownZero, KnownOne, Depth+1);
1296 case ISD::TRUNCATE: {
1297 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1298 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1299 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1300 KnownZero &= OutMask;
1301 KnownOne &= OutMask;
1304 case ISD::AssertZext: {
1305 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1306 uint64_t InMask = MVT::getIntVTBitMask(VT);
1307 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1309 KnownZero |= (~InMask) & Mask;
1313 // If either the LHS or the RHS are Zero, the result is zero.
1314 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1315 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1316 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1317 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1319 // Output known-0 bits are known if clear or set in both the low clear bits
1320 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1321 // low 3 bits clear.
1322 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1323 CountTrailingZeros_64(~KnownZero2));
1325 KnownZero = (1ULL << KnownZeroOut) - 1;
1330 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1333 // We know that the top bits of C-X are clear if X contains less bits
1334 // than C (i.e. no wrap-around can happen). For example, 20-X is
1335 // positive if we can prove that X is >= 0 and < 16.
1336 MVT::ValueType VT = CLHS->getValueType(0);
1337 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1338 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1339 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1340 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1341 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1343 // If all of the MaskV bits are known to be zero, then we know the output
1344 // top bits are zero, because we now know that the output is from [0-C].
1345 if ((KnownZero & MaskV) == MaskV) {
1346 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1347 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1348 KnownOne = 0; // No one bits known.
1350 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1356 // Allow the target to implement this method for its nodes.
1357 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1358 case ISD::INTRINSIC_WO_CHAIN:
1359 case ISD::INTRINSIC_W_CHAIN:
1360 case ISD::INTRINSIC_VOID:
1361 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1367 /// ComputeNumSignBits - Return the number of times the sign bit of the
1368 /// register is replicated into the other bits. We know that at least 1 bit
1369 /// is always equal to the sign bit (itself), but other cases can give us
1370 /// information. For example, immediately after an "SRA X, 2", we know that
1371 /// the top 3 bits are all equal to each other, so we return 3.
1372 unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1373 MVT::ValueType VT = Op.getValueType();
1374 assert(MVT::isInteger(VT) && "Invalid VT!");
1375 unsigned VTBits = MVT::getSizeInBits(VT);
1379 return 1; // Limit search depth.
1381 switch (Op.getOpcode()) {
1383 case ISD::AssertSext:
1384 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1385 return VTBits-Tmp+1;
1386 case ISD::AssertZext:
1387 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1390 case ISD::Constant: {
1391 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1392 // If negative, invert the bits, then look at it.
1393 if (Val & MVT::getIntVTSignBit(VT))
1396 // Shift the bits so they are the leading bits in the int64_t.
1399 // Return # leading zeros. We use 'min' here in case Val was zero before
1400 // shifting. We don't want to return '64' as for an i32 "0".
1401 return std::min(VTBits, CountLeadingZeros_64(Val));
1404 case ISD::SIGN_EXTEND:
1405 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1406 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1408 case ISD::SIGN_EXTEND_INREG:
1409 // Max of the input and what this extends.
1410 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1413 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1414 return std::max(Tmp, Tmp2);
1417 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1418 // SRA X, C -> adds C sign bits.
1419 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1420 Tmp += C->getValue();
1421 if (Tmp > VTBits) Tmp = VTBits;
1425 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1426 // shl destroys sign bits.
1427 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1428 if (C->getValue() >= VTBits || // Bad shift.
1429 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1430 return Tmp - C->getValue();
1435 case ISD::XOR: // NOT is handled here.
1436 // Logical binary ops preserve the number of sign bits.
1437 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1438 if (Tmp == 1) return 1; // Early out.
1439 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1440 return std::min(Tmp, Tmp2);
1443 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1444 if (Tmp == 1) return 1; // Early out.
1445 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1446 return std::min(Tmp, Tmp2);
1449 // If setcc returns 0/-1, all bits are sign bits.
1450 if (TLI.getSetCCResultContents() ==
1451 TargetLowering::ZeroOrNegativeOneSetCCResult)
1456 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1457 unsigned RotAmt = C->getValue() & (VTBits-1);
1459 // Handle rotate right by N like a rotate left by 32-N.
1460 if (Op.getOpcode() == ISD::ROTR)
1461 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1463 // If we aren't rotating out all of the known-in sign bits, return the
1464 // number that are left. This handles rotl(sext(x), 1) for example.
1465 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1466 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1470 // Add can have at most one carry bit. Thus we know that the output
1471 // is, at worst, one more bit than the inputs.
1472 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1473 if (Tmp == 1) return 1; // Early out.
1475 // Special case decrementing a value (ADD X, -1):
1476 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1477 if (CRHS->isAllOnesValue()) {
1478 uint64_t KnownZero, KnownOne;
1479 uint64_t Mask = MVT::getIntVTBitMask(VT);
1480 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1482 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1484 if ((KnownZero|1) == Mask)
1487 // If we are subtracting one from a positive number, there is no carry
1488 // out of the result.
1489 if (KnownZero & MVT::getIntVTSignBit(VT))
1493 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1494 if (Tmp2 == 1) return 1;
1495 return std::min(Tmp, Tmp2)-1;
1499 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1500 if (Tmp2 == 1) return 1;
1503 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1504 if (CLHS->getValue() == 0) {
1505 uint64_t KnownZero, KnownOne;
1506 uint64_t Mask = MVT::getIntVTBitMask(VT);
1507 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1508 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1510 if ((KnownZero|1) == Mask)
1513 // If the input is known to be positive (the sign bit is known clear),
1514 // the output of the NEG has the same number of sign bits as the input.
1515 if (KnownZero & MVT::getIntVTSignBit(VT))
1518 // Otherwise, we treat this like a SUB.
1521 // Sub can have at most one carry bit. Thus we know that the output
1522 // is, at worst, one more bit than the inputs.
1523 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1524 if (Tmp == 1) return 1; // Early out.
1525 return std::min(Tmp, Tmp2)-1;
1528 // FIXME: it's tricky to do anything useful for this, but it is an important
1529 // case for targets like X86.
1533 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1534 if (Op.getOpcode() == ISD::LOAD) {
1535 LoadSDNode *LD = cast<LoadSDNode>(Op);
1536 unsigned ExtType = LD->getExtensionType();
1539 case ISD::SEXTLOAD: // '17' bits known
1540 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1541 return VTBits-Tmp+1;
1542 case ISD::ZEXTLOAD: // '16' bits known
1543 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1548 // Allow the target to implement this method for its nodes.
1549 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1550 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1551 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1552 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1553 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1554 if (NumBits > 1) return NumBits;
1557 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1558 // use this information.
1559 uint64_t KnownZero, KnownOne;
1560 uint64_t Mask = MVT::getIntVTBitMask(VT);
1561 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1563 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1564 if (KnownZero & SignBit) { // SignBit is 0
1566 } else if (KnownOne & SignBit) { // SignBit is 1;
1573 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1574 // the number of identical bits in the top of the input value.
1577 // Return # leading zeros. We use 'min' here in case Val was zero before
1578 // shifting. We don't want to return '64' as for an i32 "0".
1579 return std::min(VTBits, CountLeadingZeros_64(Mask));
1583 /// getNode - Gets or creates the specified node.
1585 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
1586 FoldingSetNodeID ID;
1587 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
1589 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1590 return SDOperand(E, 0);
1591 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
1592 CSEMap.InsertNode(N, IP);
1594 AllNodes.push_back(N);
1595 return SDOperand(N, 0);
1598 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1599 SDOperand Operand) {
1601 // Constant fold unary operations with an integer constant operand.
1602 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1603 uint64_t Val = C->getValue();
1606 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
1607 case ISD::ANY_EXTEND:
1608 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1609 case ISD::TRUNCATE: return getConstant(Val, VT);
1610 case ISD::UINT_TO_FP:
1611 case ISD::SINT_TO_FP: {
1612 const uint64_t zero[] = {0, 0};
1613 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
1614 (void)apf.convertFromZeroExtendedInteger(&Val,
1615 MVT::getSizeInBits(Operand.getValueType()),
1616 Opcode==ISD::SINT_TO_FP,
1617 APFloat::rmNearestTiesToEven);
1618 return getConstantFP(apf, VT);
1620 case ISD::BIT_CONVERT:
1621 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
1622 return getConstantFP(BitsToFloat(Val), VT);
1623 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
1624 return getConstantFP(BitsToDouble(Val), VT);
1628 default: assert(0 && "Invalid bswap!"); break;
1629 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1630 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1631 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1636 default: assert(0 && "Invalid ctpop!"); break;
1637 case MVT::i1: return getConstant(Val != 0, VT);
1639 Tmp1 = (unsigned)Val & 0xFF;
1640 return getConstant(CountPopulation_32(Tmp1), VT);
1642 Tmp1 = (unsigned)Val & 0xFFFF;
1643 return getConstant(CountPopulation_32(Tmp1), VT);
1645 return getConstant(CountPopulation_32((unsigned)Val), VT);
1647 return getConstant(CountPopulation_64(Val), VT);
1651 default: assert(0 && "Invalid ctlz!"); break;
1652 case MVT::i1: return getConstant(Val == 0, VT);
1654 Tmp1 = (unsigned)Val & 0xFF;
1655 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1657 Tmp1 = (unsigned)Val & 0xFFFF;
1658 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1660 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1662 return getConstant(CountLeadingZeros_64(Val), VT);
1666 default: assert(0 && "Invalid cttz!"); break;
1667 case MVT::i1: return getConstant(Val == 0, VT);
1669 Tmp1 = (unsigned)Val | 0x100;
1670 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1672 Tmp1 = (unsigned)Val | 0x10000;
1673 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1675 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1677 return getConstant(CountTrailingZeros_64(Val), VT);
1682 // Constant fold unary operations with a floating point constant operand.
1683 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1684 APFloat V = C->getValueAPF(); // make copy
1688 return getConstantFP(V, VT);
1691 return getConstantFP(V, VT);
1693 case ISD::FP_EXTEND:
1694 // This can return overflow, underflow, or inexact; we don't care.
1695 // FIXME need to be more flexible about rounding mode.
1696 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle :
1697 VT==MVT::f64 ? APFloat::IEEEdouble :
1698 VT==MVT::f80 ? APFloat::x87DoubleExtended :
1699 VT==MVT::f128 ? APFloat::IEEEquad :
1701 APFloat::rmNearestTiesToEven);
1702 return getConstantFP(V, VT);
1703 case ISD::FP_TO_SINT:
1704 case ISD::FP_TO_UINT: {
1706 assert(integerPartWidth >= 64);
1707 // FIXME need to be more flexible about rounding mode.
1708 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1709 Opcode==ISD::FP_TO_SINT,
1710 APFloat::rmTowardZero);
1711 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1713 return getConstant(x, VT);
1715 case ISD::BIT_CONVERT:
1716 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1717 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1718 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1719 return getConstant(V.convertToAPInt().getZExtValue(), VT);
1724 unsigned OpOpcode = Operand.Val->getOpcode();
1726 case ISD::TokenFactor:
1727 return Operand; // Factor of one node? No factor.
1729 case ISD::FP_EXTEND:
1730 assert(MVT::isFloatingPoint(VT) &&
1731 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
1733 case ISD::SIGN_EXTEND:
1734 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1735 "Invalid SIGN_EXTEND!");
1736 if (Operand.getValueType() == VT) return Operand; // noop extension
1737 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
1738 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1739 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1741 case ISD::ZERO_EXTEND:
1742 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1743 "Invalid ZERO_EXTEND!");
1744 if (Operand.getValueType() == VT) return Operand; // noop extension
1745 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
1746 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1747 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1749 case ISD::ANY_EXTEND:
1750 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1751 "Invalid ANY_EXTEND!");
1752 if (Operand.getValueType() == VT) return Operand; // noop extension
1753 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
1754 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1755 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1756 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1759 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1760 "Invalid TRUNCATE!");
1761 if (Operand.getValueType() == VT) return Operand; // noop truncate
1762 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
1763 if (OpOpcode == ISD::TRUNCATE)
1764 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1765 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1766 OpOpcode == ISD::ANY_EXTEND) {
1767 // If the source is smaller than the dest, we still need an extend.
1768 if (Operand.Val->getOperand(0).getValueType() < VT)
1769 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1770 else if (Operand.Val->getOperand(0).getValueType() > VT)
1771 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1773 return Operand.Val->getOperand(0);
1776 case ISD::BIT_CONVERT:
1777 // Basic sanity checking.
1778 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
1779 && "Cannot BIT_CONVERT between types of different sizes!");
1780 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1781 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1782 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1783 if (OpOpcode == ISD::UNDEF)
1784 return getNode(ISD::UNDEF, VT);
1786 case ISD::SCALAR_TO_VECTOR:
1787 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1788 MVT::getVectorElementType(VT) == Operand.getValueType() &&
1789 "Illegal SCALAR_TO_VECTOR node!");
1792 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1793 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1794 Operand.Val->getOperand(0));
1795 if (OpOpcode == ISD::FNEG) // --X -> X
1796 return Operand.Val->getOperand(0);
1799 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1800 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1805 SDVTList VTs = getVTList(VT);
1806 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1807 FoldingSetNodeID ID;
1808 SDOperand Ops[1] = { Operand };
1809 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
1811 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1812 return SDOperand(E, 0);
1813 N = new UnarySDNode(Opcode, VTs, Operand);
1814 CSEMap.InsertNode(N, IP);
1816 N = new UnarySDNode(Opcode, VTs, Operand);
1818 AllNodes.push_back(N);
1819 return SDOperand(N, 0);
1824 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1825 SDOperand N1, SDOperand N2) {
1828 case ISD::TokenFactor:
1829 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1830 N2.getValueType() == MVT::Other && "Invalid token factor!");
1839 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1846 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1853 assert(N1.getValueType() == N2.getValueType() &&
1854 N1.getValueType() == VT && "Binary operator types must match!");
1856 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1857 assert(N1.getValueType() == VT &&
1858 MVT::isFloatingPoint(N1.getValueType()) &&
1859 MVT::isFloatingPoint(N2.getValueType()) &&
1860 "Invalid FCOPYSIGN!");
1867 assert(VT == N1.getValueType() &&
1868 "Shift operators return type must be the same as their first arg");
1869 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1870 VT != MVT::i1 && "Shifts only work on integers");
1872 case ISD::FP_ROUND_INREG: {
1873 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1874 assert(VT == N1.getValueType() && "Not an inreg round!");
1875 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1876 "Cannot FP_ROUND_INREG integer types");
1877 assert(EVT <= VT && "Not rounding down!");
1880 case ISD::AssertSext:
1881 case ISD::AssertZext:
1882 case ISD::SIGN_EXTEND_INREG: {
1883 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1884 assert(VT == N1.getValueType() && "Not an inreg extend!");
1885 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1886 "Cannot *_EXTEND_INREG FP types");
1887 assert(EVT <= VT && "Not extending!");
1894 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1895 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1897 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1898 int64_t Val = N1C->getValue();
1899 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1900 Val <<= 64-FromBits;
1901 Val >>= 64-FromBits;
1902 return getConstant(Val, VT);
1906 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1908 case ISD::ADD: return getConstant(C1 + C2, VT);
1909 case ISD::SUB: return getConstant(C1 - C2, VT);
1910 case ISD::MUL: return getConstant(C1 * C2, VT);
1912 if (C2) return getConstant(C1 / C2, VT);
1915 if (C2) return getConstant(C1 % C2, VT);
1918 if (C2) return getConstant(N1C->getSignExtended() /
1919 N2C->getSignExtended(), VT);
1922 if (C2) return getConstant(N1C->getSignExtended() %
1923 N2C->getSignExtended(), VT);
1925 case ISD::AND : return getConstant(C1 & C2, VT);
1926 case ISD::OR : return getConstant(C1 | C2, VT);
1927 case ISD::XOR : return getConstant(C1 ^ C2, VT);
1928 case ISD::SHL : return getConstant(C1 << C2, VT);
1929 case ISD::SRL : return getConstant(C1 >> C2, VT);
1930 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
1932 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1935 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1939 } else { // Cannonicalize constant to RHS if commutative
1940 if (isCommutativeBinOp(Opcode)) {
1941 std::swap(N1C, N2C);
1947 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1948 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1951 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
1952 APFloat::opStatus s;
1955 s = V1.add(V2, APFloat::rmNearestTiesToEven);
1956 if (s!=APFloat::opInvalidOp)
1957 return getConstantFP(V1, VT);
1960 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
1961 if (s!=APFloat::opInvalidOp)
1962 return getConstantFP(V1, VT);
1965 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
1966 if (s!=APFloat::opInvalidOp)
1967 return getConstantFP(V1, VT);
1970 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
1971 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1972 return getConstantFP(V1, VT);
1975 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
1976 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1977 return getConstantFP(V1, VT);
1979 case ISD::FCOPYSIGN:
1981 return getConstantFP(V1, VT);
1984 } else { // Cannonicalize constant to RHS if commutative
1985 if (isCommutativeBinOp(Opcode)) {
1986 std::swap(N1CFP, N2CFP);
1992 // Canonicalize an UNDEF to the RHS, even over a constant.
1993 if (N1.getOpcode() == ISD::UNDEF) {
1994 if (isCommutativeBinOp(Opcode)) {
1998 case ISD::FP_ROUND_INREG:
1999 case ISD::SIGN_EXTEND_INREG:
2005 return N1; // fold op(undef, arg2) -> undef
2012 if (!MVT::isVector(VT))
2013 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2014 // For vectors, we can't easily build an all zero vector, just return
2021 // Fold a bunch of operators when the RHS is undef.
2022 if (N2.getOpcode() == ISD::UNDEF) {
2038 return N2; // fold op(arg1, undef) -> undef
2043 if (!MVT::isVector(VT))
2044 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2045 // For vectors, we can't easily build an all zero vector, just return
2049 if (!MVT::isVector(VT))
2050 return getConstant(MVT::getIntVTBitMask(VT), VT);
2051 // For vectors, we can't easily build an all one vector, just return
2061 case ISD::TokenFactor:
2062 // Fold trivial token factors.
2063 if (N1.getOpcode() == ISD::EntryToken) return N2;
2064 if (N2.getOpcode() == ISD::EntryToken) return N1;
2068 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2069 // worth handling here.
2070 if (N2C && N2C->getValue() == 0)
2075 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
2076 // worth handling here.
2077 if (N2C && N2C->getValue() == 0)
2080 case ISD::FP_ROUND_INREG:
2081 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
2083 case ISD::SIGN_EXTEND_INREG: {
2084 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2085 if (EVT == VT) return N1; // Not actually extending
2088 case ISD::EXTRACT_VECTOR_ELT:
2089 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2091 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2092 // expanding copies of large vectors from registers.
2093 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2094 N1.getNumOperands() > 0) {
2096 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2097 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2098 N1.getOperand(N2C->getValue() / Factor),
2099 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2102 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2103 // expanding large vector constants.
2104 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2105 return N1.getOperand(N2C->getValue());
2107 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2108 // operations are lowered to scalars.
2109 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2110 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2112 return N1.getOperand(1);
2114 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2117 case ISD::EXTRACT_ELEMENT:
2118 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
2120 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2121 // 64-bit integers into 32-bit parts. Instead of building the extract of
2122 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2123 if (N1.getOpcode() == ISD::BUILD_PAIR)
2124 return N1.getOperand(N2C->getValue());
2126 // EXTRACT_ELEMENT of a constant int is also very common.
2127 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2128 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2129 return getConstant(C->getValue() >> Shift, VT);
2133 // FIXME: figure out how to safely handle things like
2134 // int foo(int x) { return 1 << (x & 255); }
2135 // int bar() { return foo(256); }
2140 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2141 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
2142 return getNode(Opcode, VT, N1, N2.getOperand(0));
2143 else if (N2.getOpcode() == ISD::AND)
2144 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
2145 // If the and is only masking out bits that cannot effect the shift,
2146 // eliminate the and.
2147 unsigned NumBits = MVT::getSizeInBits(VT);
2148 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2149 return getNode(Opcode, VT, N1, N2.getOperand(0));
2155 // Memoize this node if possible.
2157 SDVTList VTs = getVTList(VT);
2158 if (VT != MVT::Flag) {
2159 SDOperand Ops[] = { N1, N2 };
2160 FoldingSetNodeID ID;
2161 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
2163 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2164 return SDOperand(E, 0);
2165 N = new BinarySDNode(Opcode, VTs, N1, N2);
2166 CSEMap.InsertNode(N, IP);
2168 N = new BinarySDNode(Opcode, VTs, N1, N2);
2171 AllNodes.push_back(N);
2172 return SDOperand(N, 0);
2175 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2176 SDOperand N1, SDOperand N2, SDOperand N3) {
2177 // Perform various simplifications.
2178 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2179 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2182 // Use FoldSetCC to simplify SETCC's.
2183 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
2184 if (Simp.Val) return Simp;
2189 if (N1C->getValue())
2190 return N2; // select true, X, Y -> X
2192 return N3; // select false, X, Y -> Y
2194 if (N2 == N3) return N2; // select C, X, X -> X
2198 if (N2C->getValue()) // Unconditional branch
2199 return getNode(ISD::BR, MVT::Other, N1, N3);
2201 return N1; // Never-taken branch
2203 case ISD::VECTOR_SHUFFLE:
2204 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2205 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2206 N3.getOpcode() == ISD::BUILD_VECTOR &&
2207 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2208 "Illegal VECTOR_SHUFFLE node!");
2210 case ISD::BIT_CONVERT:
2211 // Fold bit_convert nodes from a type to themselves.
2212 if (N1.getValueType() == VT)
2217 // Memoize node if it doesn't produce a flag.
2219 SDVTList VTs = getVTList(VT);
2220 if (VT != MVT::Flag) {
2221 SDOperand Ops[] = { N1, N2, N3 };
2222 FoldingSetNodeID ID;
2223 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2225 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2226 return SDOperand(E, 0);
2227 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2228 CSEMap.InsertNode(N, IP);
2230 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2232 AllNodes.push_back(N);
2233 return SDOperand(N, 0);
2236 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2237 SDOperand N1, SDOperand N2, SDOperand N3,
2239 SDOperand Ops[] = { N1, N2, N3, N4 };
2240 return getNode(Opcode, VT, Ops, 4);
2243 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2244 SDOperand N1, SDOperand N2, SDOperand N3,
2245 SDOperand N4, SDOperand N5) {
2246 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2247 return getNode(Opcode, VT, Ops, 5);
2250 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2251 SDOperand Chain, SDOperand Ptr,
2252 const Value *SV, int SVOffset,
2253 bool isVolatile, unsigned Alignment) {
2254 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2256 if (VT != MVT::iPTR) {
2257 Ty = MVT::getTypeForValueType(VT);
2259 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2260 assert(PT && "Value for load must be a pointer");
2261 Ty = PT->getElementType();
2263 assert(Ty && "Could not get type information for load");
2264 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2266 SDVTList VTs = getVTList(VT, MVT::Other);
2267 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2268 SDOperand Ops[] = { Chain, Ptr, Undef };
2269 FoldingSetNodeID ID;
2270 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2271 ID.AddInteger(ISD::UNINDEXED);
2272 ID.AddInteger(ISD::NON_EXTLOAD);
2273 ID.AddInteger((unsigned int)VT);
2275 ID.AddInteger(SVOffset);
2276 ID.AddInteger(Alignment);
2277 ID.AddInteger(isVolatile);
2279 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2280 return SDOperand(E, 0);
2281 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
2282 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2284 CSEMap.InsertNode(N, IP);
2285 AllNodes.push_back(N);
2286 return SDOperand(N, 0);
2289 SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
2290 SDOperand Chain, SDOperand Ptr,
2292 int SVOffset, MVT::ValueType EVT,
2293 bool isVolatile, unsigned Alignment) {
2294 // If they are asking for an extending load from/to the same thing, return a
2297 ExtType = ISD::NON_EXTLOAD;
2299 if (MVT::isVector(VT))
2300 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
2302 assert(EVT < VT && "Should only be an extending load, not truncating!");
2303 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2304 "Cannot sign/zero extend a FP/Vector load!");
2305 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2306 "Cannot convert from FP to Int or Int -> FP!");
2308 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2310 if (VT != MVT::iPTR) {
2311 Ty = MVT::getTypeForValueType(VT);
2313 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2314 assert(PT && "Value for load must be a pointer");
2315 Ty = PT->getElementType();
2317 assert(Ty && "Could not get type information for load");
2318 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2320 SDVTList VTs = getVTList(VT, MVT::Other);
2321 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2322 SDOperand Ops[] = { Chain, Ptr, Undef };
2323 FoldingSetNodeID ID;
2324 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2325 ID.AddInteger(ISD::UNINDEXED);
2326 ID.AddInteger(ExtType);
2327 ID.AddInteger((unsigned int)EVT);
2329 ID.AddInteger(SVOffset);
2330 ID.AddInteger(Alignment);
2331 ID.AddInteger(isVolatile);
2333 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2334 return SDOperand(E, 0);
2335 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
2336 SV, SVOffset, Alignment, isVolatile);
2337 CSEMap.InsertNode(N, IP);
2338 AllNodes.push_back(N);
2339 return SDOperand(N, 0);
2343 SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2344 SDOperand Offset, ISD::MemIndexedMode AM) {
2345 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
2346 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2347 "Load is already a indexed load!");
2348 MVT::ValueType VT = OrigLoad.getValueType();
2349 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
2350 SDOperand Ops[] = { LD->getChain(), Base, Offset };
2351 FoldingSetNodeID ID;
2352 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2354 ID.AddInteger(LD->getExtensionType());
2355 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
2356 ID.AddPointer(LD->getSrcValue());
2357 ID.AddInteger(LD->getSrcValueOffset());
2358 ID.AddInteger(LD->getAlignment());
2359 ID.AddInteger(LD->isVolatile());
2361 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2362 return SDOperand(E, 0);
2363 SDNode *N = new LoadSDNode(Ops, VTs, AM,
2364 LD->getExtensionType(), LD->getLoadedVT(),
2365 LD->getSrcValue(), LD->getSrcValueOffset(),
2366 LD->getAlignment(), LD->isVolatile());
2367 CSEMap.InsertNode(N, IP);
2368 AllNodes.push_back(N);
2369 return SDOperand(N, 0);
2372 SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
2373 SDOperand Ptr, const Value *SV, int SVOffset,
2374 bool isVolatile, unsigned Alignment) {
2375 MVT::ValueType VT = Val.getValueType();
2377 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2379 if (VT != MVT::iPTR) {
2380 Ty = MVT::getTypeForValueType(VT);
2382 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2383 assert(PT && "Value for store must be a pointer");
2384 Ty = PT->getElementType();
2386 assert(Ty && "Could not get type information for store");
2387 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2389 SDVTList VTs = getVTList(MVT::Other);
2390 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2391 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2392 FoldingSetNodeID ID;
2393 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2394 ID.AddInteger(ISD::UNINDEXED);
2395 ID.AddInteger(false);
2396 ID.AddInteger((unsigned int)VT);
2398 ID.AddInteger(SVOffset);
2399 ID.AddInteger(Alignment);
2400 ID.AddInteger(isVolatile);
2402 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2403 return SDOperand(E, 0);
2404 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
2405 VT, SV, SVOffset, Alignment, isVolatile);
2406 CSEMap.InsertNode(N, IP);
2407 AllNodes.push_back(N);
2408 return SDOperand(N, 0);
2411 SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
2412 SDOperand Ptr, const Value *SV,
2413 int SVOffset, MVT::ValueType SVT,
2414 bool isVolatile, unsigned Alignment) {
2415 MVT::ValueType VT = Val.getValueType();
2416 bool isTrunc = VT != SVT;
2418 assert(VT > SVT && "Not a truncation?");
2419 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2420 "Can't do FP-INT conversion!");
2422 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2424 if (VT != MVT::iPTR) {
2425 Ty = MVT::getTypeForValueType(VT);
2427 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2428 assert(PT && "Value for store must be a pointer");
2429 Ty = PT->getElementType();
2431 assert(Ty && "Could not get type information for store");
2432 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2434 SDVTList VTs = getVTList(MVT::Other);
2435 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2436 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2437 FoldingSetNodeID ID;
2438 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2439 ID.AddInteger(ISD::UNINDEXED);
2440 ID.AddInteger(isTrunc);
2441 ID.AddInteger((unsigned int)SVT);
2443 ID.AddInteger(SVOffset);
2444 ID.AddInteger(Alignment);
2445 ID.AddInteger(isVolatile);
2447 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2448 return SDOperand(E, 0);
2449 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, isTrunc,
2450 SVT, SV, SVOffset, Alignment, isVolatile);
2451 CSEMap.InsertNode(N, IP);
2452 AllNodes.push_back(N);
2453 return SDOperand(N, 0);
2457 SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2458 SDOperand Offset, ISD::MemIndexedMode AM) {
2459 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2460 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2461 "Store is already a indexed store!");
2462 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2463 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2464 FoldingSetNodeID ID;
2465 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2467 ID.AddInteger(ST->isTruncatingStore());
2468 ID.AddInteger((unsigned int)(ST->getStoredVT()));
2469 ID.AddPointer(ST->getSrcValue());
2470 ID.AddInteger(ST->getSrcValueOffset());
2471 ID.AddInteger(ST->getAlignment());
2472 ID.AddInteger(ST->isVolatile());
2474 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2475 return SDOperand(E, 0);
2476 SDNode *N = new StoreSDNode(Ops, VTs, AM,
2477 ST->isTruncatingStore(), ST->getStoredVT(),
2478 ST->getSrcValue(), ST->getSrcValueOffset(),
2479 ST->getAlignment(), ST->isVolatile());
2480 CSEMap.InsertNode(N, IP);
2481 AllNodes.push_back(N);
2482 return SDOperand(N, 0);
2485 SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2486 SDOperand Chain, SDOperand Ptr,
2488 SDOperand Ops[] = { Chain, Ptr, SV };
2489 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
2492 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2493 const SDOperand *Ops, unsigned NumOps) {
2495 case 0: return getNode(Opcode, VT);
2496 case 1: return getNode(Opcode, VT, Ops[0]);
2497 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2498 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
2504 case ISD::SELECT_CC: {
2505 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
2506 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2507 "LHS and RHS of condition must have same type!");
2508 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2509 "True and False arms of SelectCC must have same type!");
2510 assert(Ops[2].getValueType() == VT &&
2511 "select_cc node must be of same type as true and false value!");
2515 assert(NumOps == 5 && "BR_CC takes 5 operands!");
2516 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2517 "LHS/RHS of comparison should match types!");
2524 SDVTList VTs = getVTList(VT);
2525 if (VT != MVT::Flag) {
2526 FoldingSetNodeID ID;
2527 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
2529 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2530 return SDOperand(E, 0);
2531 N = new SDNode(Opcode, VTs, Ops, NumOps);
2532 CSEMap.InsertNode(N, IP);
2534 N = new SDNode(Opcode, VTs, Ops, NumOps);
2536 AllNodes.push_back(N);
2537 return SDOperand(N, 0);
2540 SDOperand SelectionDAG::getNode(unsigned Opcode,
2541 std::vector<MVT::ValueType> &ResultTys,
2542 const SDOperand *Ops, unsigned NumOps) {
2543 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2547 SDOperand SelectionDAG::getNode(unsigned Opcode,
2548 const MVT::ValueType *VTs, unsigned NumVTs,
2549 const SDOperand *Ops, unsigned NumOps) {
2551 return getNode(Opcode, VTs[0], Ops, NumOps);
2552 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2555 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2556 const SDOperand *Ops, unsigned NumOps) {
2557 if (VTList.NumVTs == 1)
2558 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
2561 // FIXME: figure out how to safely handle things like
2562 // int foo(int x) { return 1 << (x & 255); }
2563 // int bar() { return foo(256); }
2565 case ISD::SRA_PARTS:
2566 case ISD::SRL_PARTS:
2567 case ISD::SHL_PARTS:
2568 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2569 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
2570 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2571 else if (N3.getOpcode() == ISD::AND)
2572 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2573 // If the and is only masking out bits that cannot effect the shift,
2574 // eliminate the and.
2575 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2576 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2577 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2583 // Memoize the node unless it returns a flag.
2585 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
2586 FoldingSetNodeID ID;
2587 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
2589 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2590 return SDOperand(E, 0);
2592 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2593 else if (NumOps == 2)
2594 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2595 else if (NumOps == 3)
2596 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2598 N = new SDNode(Opcode, VTList, Ops, NumOps);
2599 CSEMap.InsertNode(N, IP);
2602 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2603 else if (NumOps == 2)
2604 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2605 else if (NumOps == 3)
2606 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2608 N = new SDNode(Opcode, VTList, Ops, NumOps);
2610 AllNodes.push_back(N);
2611 return SDOperand(N, 0);
2614 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2615 return getNode(Opcode, VTList, 0, 0);
2618 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2620 SDOperand Ops[] = { N1 };
2621 return getNode(Opcode, VTList, Ops, 1);
2624 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2625 SDOperand N1, SDOperand N2) {
2626 SDOperand Ops[] = { N1, N2 };
2627 return getNode(Opcode, VTList, Ops, 2);
2630 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2631 SDOperand N1, SDOperand N2, SDOperand N3) {
2632 SDOperand Ops[] = { N1, N2, N3 };
2633 return getNode(Opcode, VTList, Ops, 3);
2636 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2637 SDOperand N1, SDOperand N2, SDOperand N3,
2639 SDOperand Ops[] = { N1, N2, N3, N4 };
2640 return getNode(Opcode, VTList, Ops, 4);
2643 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2644 SDOperand N1, SDOperand N2, SDOperand N3,
2645 SDOperand N4, SDOperand N5) {
2646 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2647 return getNode(Opcode, VTList, Ops, 5);
2650 SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
2651 if (!MVT::isExtendedVT(VT))
2652 return makeVTList(SDNode::getValueTypeList(VT), 1);
2654 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2655 E = VTList.end(); I != E; ++I) {
2656 if (I->size() == 1 && (*I)[0] == VT)
2657 return makeVTList(&(*I)[0], 1);
2659 std::vector<MVT::ValueType> V;
2661 VTList.push_front(V);
2662 return makeVTList(&(*VTList.begin())[0], 1);
2665 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
2666 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2667 E = VTList.end(); I != E; ++I) {
2668 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
2669 return makeVTList(&(*I)[0], 2);
2671 std::vector<MVT::ValueType> V;
2674 VTList.push_front(V);
2675 return makeVTList(&(*VTList.begin())[0], 2);
2677 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2678 MVT::ValueType VT3) {
2679 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2680 E = VTList.end(); I != E; ++I) {
2681 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2683 return makeVTList(&(*I)[0], 3);
2685 std::vector<MVT::ValueType> V;
2689 VTList.push_front(V);
2690 return makeVTList(&(*VTList.begin())[0], 3);
2693 SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2695 case 0: assert(0 && "Cannot have nodes without results!");
2696 case 1: return getVTList(VTs[0]);
2697 case 2: return getVTList(VTs[0], VTs[1]);
2698 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2702 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2703 E = VTList.end(); I != E; ++I) {
2704 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2706 bool NoMatch = false;
2707 for (unsigned i = 2; i != NumVTs; ++i)
2708 if (VTs[i] != (*I)[i]) {
2713 return makeVTList(&*I->begin(), NumVTs);
2716 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2717 return makeVTList(&*VTList.begin()->begin(), NumVTs);
2721 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2722 /// specified operands. If the resultant node already exists in the DAG,
2723 /// this does not modify the specified node, instead it returns the node that
2724 /// already exists. If the resultant node does not exist in the DAG, the
2725 /// input node is returned. As a degenerate case, if you specify the same
2726 /// input operands as the node already has, the input node is returned.
2727 SDOperand SelectionDAG::
2728 UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2729 SDNode *N = InN.Val;
2730 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2732 // Check to see if there is no change.
2733 if (Op == N->getOperand(0)) return InN;
2735 // See if the modified node already exists.
2736 void *InsertPos = 0;
2737 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2738 return SDOperand(Existing, InN.ResNo);
2740 // Nope it doesn't. Remove the node from it's current place in the maps.
2742 RemoveNodeFromCSEMaps(N);
2744 // Now we update the operands.
2745 N->OperandList[0].Val->removeUser(N);
2747 N->OperandList[0] = Op;
2749 // If this gets put into a CSE map, add it.
2750 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2754 SDOperand SelectionDAG::
2755 UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2756 SDNode *N = InN.Val;
2757 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2759 // Check to see if there is no change.
2760 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2761 return InN; // No operands changed, just return the input node.
2763 // See if the modified node already exists.
2764 void *InsertPos = 0;
2765 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2766 return SDOperand(Existing, InN.ResNo);
2768 // Nope it doesn't. Remove the node from it's current place in the maps.
2770 RemoveNodeFromCSEMaps(N);
2772 // Now we update the operands.
2773 if (N->OperandList[0] != Op1) {
2774 N->OperandList[0].Val->removeUser(N);
2775 Op1.Val->addUser(N);
2776 N->OperandList[0] = Op1;
2778 if (N->OperandList[1] != Op2) {
2779 N->OperandList[1].Val->removeUser(N);
2780 Op2.Val->addUser(N);
2781 N->OperandList[1] = Op2;
2784 // If this gets put into a CSE map, add it.
2785 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2789 SDOperand SelectionDAG::
2790 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2791 SDOperand Ops[] = { Op1, Op2, Op3 };
2792 return UpdateNodeOperands(N, Ops, 3);
2795 SDOperand SelectionDAG::
2796 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2797 SDOperand Op3, SDOperand Op4) {
2798 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2799 return UpdateNodeOperands(N, Ops, 4);
2802 SDOperand SelectionDAG::
2803 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2804 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2805 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2806 return UpdateNodeOperands(N, Ops, 5);
2810 SDOperand SelectionDAG::
2811 UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
2812 SDNode *N = InN.Val;
2813 assert(N->getNumOperands() == NumOps &&
2814 "Update with wrong number of operands");
2816 // Check to see if there is no change.
2817 bool AnyChange = false;
2818 for (unsigned i = 0; i != NumOps; ++i) {
2819 if (Ops[i] != N->getOperand(i)) {
2825 // No operands changed, just return the input node.
2826 if (!AnyChange) return InN;
2828 // See if the modified node already exists.
2829 void *InsertPos = 0;
2830 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
2831 return SDOperand(Existing, InN.ResNo);
2833 // Nope it doesn't. Remove the node from it's current place in the maps.
2835 RemoveNodeFromCSEMaps(N);
2837 // Now we update the operands.
2838 for (unsigned i = 0; i != NumOps; ++i) {
2839 if (N->OperandList[i] != Ops[i]) {
2840 N->OperandList[i].Val->removeUser(N);
2841 Ops[i].Val->addUser(N);
2842 N->OperandList[i] = Ops[i];
2846 // If this gets put into a CSE map, add it.
2847 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2852 /// MorphNodeTo - This frees the operands of the current node, resets the
2853 /// opcode, types, and operands to the specified value. This should only be
2854 /// used by the SelectionDAG class.
2855 void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2856 const SDOperand *Ops, unsigned NumOps) {
2859 NumValues = L.NumVTs;
2861 // Clear the operands list, updating used nodes to remove this from their
2863 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2864 I->Val->removeUser(this);
2866 // If NumOps is larger than the # of operands we currently have, reallocate
2867 // the operand list.
2868 if (NumOps > NumOperands) {
2869 if (OperandsNeedDelete)
2870 delete [] OperandList;
2871 OperandList = new SDOperand[NumOps];
2872 OperandsNeedDelete = true;
2875 // Assign the new operands.
2876 NumOperands = NumOps;
2878 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2879 OperandList[i] = Ops[i];
2880 SDNode *N = OperandList[i].Val;
2881 N->Uses.push_back(this);
2885 /// SelectNodeTo - These are used for target selectors to *mutate* the
2886 /// specified node to have the specified return type, Target opcode, and
2887 /// operands. Note that target opcodes are stored as
2888 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
2890 /// Note that SelectNodeTo returns the resultant node. If there is already a
2891 /// node of the specified opcode and operands, it returns that node instead of
2892 /// the current one.
2893 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2894 MVT::ValueType VT) {
2895 SDVTList VTs = getVTList(VT);
2896 FoldingSetNodeID ID;
2897 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
2899 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2902 RemoveNodeFromCSEMaps(N);
2904 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
2906 CSEMap.InsertNode(N, IP);
2910 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2911 MVT::ValueType VT, SDOperand Op1) {
2912 // If an identical node already exists, use it.
2913 SDVTList VTs = getVTList(VT);
2914 SDOperand Ops[] = { Op1 };
2916 FoldingSetNodeID ID;
2917 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
2919 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2922 RemoveNodeFromCSEMaps(N);
2923 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
2924 CSEMap.InsertNode(N, IP);
2928 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2929 MVT::ValueType VT, SDOperand Op1,
2931 // If an identical node already exists, use it.
2932 SDVTList VTs = getVTList(VT);
2933 SDOperand Ops[] = { Op1, Op2 };
2935 FoldingSetNodeID ID;
2936 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2938 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2941 RemoveNodeFromCSEMaps(N);
2943 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2945 CSEMap.InsertNode(N, IP); // Memoize the new node.
2949 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2950 MVT::ValueType VT, SDOperand Op1,
2951 SDOperand Op2, SDOperand Op3) {
2952 // If an identical node already exists, use it.
2953 SDVTList VTs = getVTList(VT);
2954 SDOperand Ops[] = { Op1, Op2, Op3 };
2955 FoldingSetNodeID ID;
2956 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
2958 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2961 RemoveNodeFromCSEMaps(N);
2963 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
2965 CSEMap.InsertNode(N, IP); // Memoize the new node.
2969 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2970 MVT::ValueType VT, const SDOperand *Ops,
2972 // If an identical node already exists, use it.
2973 SDVTList VTs = getVTList(VT);
2974 FoldingSetNodeID ID;
2975 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
2977 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2980 RemoveNodeFromCSEMaps(N);
2981 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
2983 CSEMap.InsertNode(N, IP); // Memoize the new node.
2987 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2988 MVT::ValueType VT1, MVT::ValueType VT2,
2989 SDOperand Op1, SDOperand Op2) {
2990 SDVTList VTs = getVTList(VT1, VT2);
2991 FoldingSetNodeID ID;
2992 SDOperand Ops[] = { Op1, Op2 };
2993 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2995 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2998 RemoveNodeFromCSEMaps(N);
2999 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3000 CSEMap.InsertNode(N, IP); // Memoize the new node.
3004 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3005 MVT::ValueType VT1, MVT::ValueType VT2,
3006 SDOperand Op1, SDOperand Op2,
3008 // If an identical node already exists, use it.
3009 SDVTList VTs = getVTList(VT1, VT2);
3010 SDOperand Ops[] = { Op1, Op2, Op3 };
3011 FoldingSetNodeID ID;
3012 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3014 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3017 RemoveNodeFromCSEMaps(N);
3019 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3020 CSEMap.InsertNode(N, IP); // Memoize the new node.
3025 /// getTargetNode - These are used for target selectors to create a new node
3026 /// with specified return type(s), target opcode, and operands.
3028 /// Note that getTargetNode returns the resultant node. If there is already a
3029 /// node of the specified opcode and operands, it returns that node instead of
3030 /// the current one.
3031 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3032 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3034 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3036 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3038 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3039 SDOperand Op1, SDOperand Op2) {
3040 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3042 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3043 SDOperand Op1, SDOperand Op2,
3045 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3047 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3048 const SDOperand *Ops, unsigned NumOps) {
3049 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
3051 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3052 MVT::ValueType VT2) {
3053 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3055 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3057 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3058 MVT::ValueType VT2, SDOperand Op1) {
3059 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3060 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
3062 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3063 MVT::ValueType VT2, SDOperand Op1,
3065 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3066 SDOperand Ops[] = { Op1, Op2 };
3067 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
3069 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3070 MVT::ValueType VT2, SDOperand Op1,
3071 SDOperand Op2, SDOperand Op3) {
3072 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3073 SDOperand Ops[] = { Op1, Op2, Op3 };
3074 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
3076 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3078 const SDOperand *Ops, unsigned NumOps) {
3079 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3080 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
3082 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3083 MVT::ValueType VT2, MVT::ValueType VT3,
3084 SDOperand Op1, SDOperand Op2) {
3085 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3086 SDOperand Ops[] = { Op1, Op2 };
3087 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
3089 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3090 MVT::ValueType VT2, MVT::ValueType VT3,
3091 SDOperand Op1, SDOperand Op2,
3093 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3094 SDOperand Ops[] = { Op1, Op2, Op3 };
3095 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3097 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3098 MVT::ValueType VT2, MVT::ValueType VT3,
3099 const SDOperand *Ops, unsigned NumOps) {
3100 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3101 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
3103 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3104 MVT::ValueType VT2, MVT::ValueType VT3,
3106 const SDOperand *Ops, unsigned NumOps) {
3107 std::vector<MVT::ValueType> VTList;
3108 VTList.push_back(VT1);
3109 VTList.push_back(VT2);
3110 VTList.push_back(VT3);
3111 VTList.push_back(VT4);
3112 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3113 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3115 SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3116 std::vector<MVT::ValueType> &ResultTys,
3117 const SDOperand *Ops, unsigned NumOps) {
3118 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3119 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3123 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3124 /// This can cause recursive merging of nodes in the DAG.
3126 /// This version assumes From/To have a single result value.
3128 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
3129 std::vector<SDNode*> *Deleted) {
3130 SDNode *From = FromN.Val, *To = ToN.Val;
3131 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
3132 "Cannot replace with this method!");
3133 assert(From != To && "Cannot replace uses of with self");
3135 while (!From->use_empty()) {
3136 // Process users until they are all gone.
3137 SDNode *U = *From->use_begin();
3139 // This node is about to morph, remove its old self from the CSE maps.
3140 RemoveNodeFromCSEMaps(U);
3142 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3144 if (I->Val == From) {
3145 From->removeUser(U);
3150 // Now that we have modified U, add it back to the CSE maps. If it already
3151 // exists there, recursively merge the results together.
3152 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3153 ReplaceAllUsesWith(U, Existing, Deleted);
3155 if (Deleted) Deleted->push_back(U);
3156 DeleteNodeNotInCSEMaps(U);
3161 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3162 /// This can cause recursive merging of nodes in the DAG.
3164 /// This version assumes From/To have matching types and numbers of result
3167 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3168 std::vector<SDNode*> *Deleted) {
3169 assert(From != To && "Cannot replace uses of with self");
3170 assert(From->getNumValues() == To->getNumValues() &&
3171 "Cannot use this version of ReplaceAllUsesWith!");
3172 if (From->getNumValues() == 1) { // If possible, use the faster version.
3173 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
3177 while (!From->use_empty()) {
3178 // Process users until they are all gone.
3179 SDNode *U = *From->use_begin();
3181 // This node is about to morph, remove its old self from the CSE maps.
3182 RemoveNodeFromCSEMaps(U);
3184 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3186 if (I->Val == From) {
3187 From->removeUser(U);
3192 // Now that we have modified U, add it back to the CSE maps. If it already
3193 // exists there, recursively merge the results together.
3194 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3195 ReplaceAllUsesWith(U, Existing, Deleted);
3197 if (Deleted) Deleted->push_back(U);
3198 DeleteNodeNotInCSEMaps(U);
3203 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3204 /// This can cause recursive merging of nodes in the DAG.
3206 /// This version can replace From with any result values. To must match the
3207 /// number and types of values returned by From.
3208 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
3209 const SDOperand *To,
3210 std::vector<SDNode*> *Deleted) {
3211 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
3212 // Degenerate case handled above.
3213 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
3217 while (!From->use_empty()) {
3218 // Process users until they are all gone.
3219 SDNode *U = *From->use_begin();
3221 // This node is about to morph, remove its old self from the CSE maps.
3222 RemoveNodeFromCSEMaps(U);
3224 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3226 if (I->Val == From) {
3227 const SDOperand &ToOp = To[I->ResNo];
3228 From->removeUser(U);
3230 ToOp.Val->addUser(U);
3233 // Now that we have modified U, add it back to the CSE maps. If it already
3234 // exists there, recursively merge the results together.
3235 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3236 ReplaceAllUsesWith(U, Existing, Deleted);
3238 if (Deleted) Deleted->push_back(U);
3239 DeleteNodeNotInCSEMaps(U);
3244 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3245 /// uses of other values produced by From.Val alone. The Deleted vector is
3246 /// handled the same was as for ReplaceAllUsesWith.
3247 void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
3248 std::vector<SDNode*> *Deleted) {
3249 assert(From != To && "Cannot replace a value with itself");
3250 // Handle the simple, trivial, case efficiently.
3251 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
3252 ReplaceAllUsesWith(From, To, Deleted);
3256 // Get all of the users of From.Val. We want these in a nice,
3257 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3258 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
3260 std::vector<SDNode*> LocalDeletionVector;
3262 // Pick a deletion vector to use. If the user specified one, use theirs,
3263 // otherwise use a local one.
3264 std::vector<SDNode*> *DeleteVector = Deleted ? Deleted : &LocalDeletionVector;
3265 while (!Users.empty()) {
3266 // We know that this user uses some value of From. If it is the right
3267 // value, update it.
3268 SDNode *User = Users.back();
3271 // Scan for an operand that matches From.
3272 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3273 for (; Op != E; ++Op)
3274 if (*Op == From) break;
3276 // If there are no matches, the user must use some other result of From.
3277 if (Op == E) continue;
3279 // Okay, we know this user needs to be updated. Remove its old self
3280 // from the CSE maps.
3281 RemoveNodeFromCSEMaps(User);
3283 // Update all operands that match "From".
3284 for (; Op != E; ++Op) {
3286 From.Val->removeUser(User);
3288 To.Val->addUser(User);
3292 // Now that we have modified User, add it back to the CSE maps. If it
3293 // already exists there, recursively merge the results together.
3294 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
3295 if (!Existing) continue; // Continue on to next user.
3297 // If there was already an existing matching node, use ReplaceAllUsesWith
3298 // to replace the dead one with the existing one. However, this can cause
3299 // recursive merging of other unrelated nodes down the line. The merging
3300 // can cause deletion of nodes that used the old value. In this case,
3301 // we have to be certain to remove them from the Users set.
3302 unsigned NumDeleted = DeleteVector->size();
3303 ReplaceAllUsesWith(User, Existing, DeleteVector);
3305 // User is now dead.
3306 DeleteVector->push_back(User);
3307 DeleteNodeNotInCSEMaps(User);
3309 // We have to be careful here, because ReplaceAllUsesWith could have
3310 // deleted a user of From, which means there may be dangling pointers
3311 // in the "Users" setvector. Scan over the deleted node pointers and
3312 // remove them from the setvector.
3313 for (unsigned i = NumDeleted, e = DeleteVector->size(); i != e; ++i)
3314 Users.remove((*DeleteVector)[i]);
3316 // If the user doesn't need the set of deleted elements, don't retain them
3317 // to the next loop iteration.
3319 LocalDeletionVector.clear();
3324 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3325 /// their allnodes order. It returns the maximum id.
3326 unsigned SelectionDAG::AssignNodeIds() {
3328 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3335 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
3336 /// based on their topological order. It returns the maximum id and a vector
3337 /// of the SDNodes* in assigned order by reference.
3338 unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
3339 unsigned DAGSize = AllNodes.size();
3340 std::vector<unsigned> InDegree(DAGSize);
3341 std::vector<SDNode*> Sources;
3343 // Use a two pass approach to avoid using a std::map which is slow.
3345 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3348 unsigned Degree = N->use_size();
3349 InDegree[N->getNodeId()] = Degree;
3351 Sources.push_back(N);
3355 while (!Sources.empty()) {
3356 SDNode *N = Sources.back();
3358 TopOrder.push_back(N);
3359 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3361 unsigned Degree = --InDegree[P->getNodeId()];
3363 Sources.push_back(P);
3367 // Second pass, assign the actual topological order as node ids.
3369 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3371 (*TI)->setNodeId(Id++);
3378 //===----------------------------------------------------------------------===//
3380 //===----------------------------------------------------------------------===//
3382 // Out-of-line virtual method to give class a home.
3383 void SDNode::ANCHOR() {}
3384 void UnarySDNode::ANCHOR() {}
3385 void BinarySDNode::ANCHOR() {}
3386 void TernarySDNode::ANCHOR() {}
3387 void HandleSDNode::ANCHOR() {}
3388 void StringSDNode::ANCHOR() {}
3389 void ConstantSDNode::ANCHOR() {}
3390 void ConstantFPSDNode::ANCHOR() {}
3391 void GlobalAddressSDNode::ANCHOR() {}
3392 void FrameIndexSDNode::ANCHOR() {}
3393 void JumpTableSDNode::ANCHOR() {}
3394 void ConstantPoolSDNode::ANCHOR() {}
3395 void BasicBlockSDNode::ANCHOR() {}
3396 void SrcValueSDNode::ANCHOR() {}
3397 void RegisterSDNode::ANCHOR() {}
3398 void ExternalSymbolSDNode::ANCHOR() {}
3399 void CondCodeSDNode::ANCHOR() {}
3400 void VTSDNode::ANCHOR() {}
3401 void LoadSDNode::ANCHOR() {}
3402 void StoreSDNode::ANCHOR() {}
3404 HandleSDNode::~HandleSDNode() {
3405 SDVTList VTs = { 0, 0 };
3406 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
3409 GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3410 MVT::ValueType VT, int o)
3411 : SDNode(isa<GlobalVariable>(GA) &&
3412 cast<GlobalVariable>(GA)->isThreadLocal() ?
3414 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3416 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3417 getSDVTList(VT)), Offset(o) {
3418 TheGlobal = const_cast<GlobalValue*>(GA);
3421 /// Profile - Gather unique data for the node.
3423 void SDNode::Profile(FoldingSetNodeID &ID) {
3424 AddNodeIDNode(ID, this);
3427 /// getValueTypeList - Return a pointer to the specified value type.
3429 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
3430 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3435 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3436 /// indicated value. This method ignores uses of other values defined by this
3438 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
3439 assert(Value < getNumValues() && "Bad value!");
3441 // If there is only one value, this is easy.
3442 if (getNumValues() == 1)
3443 return use_size() == NUses;
3444 if (use_size() < NUses) return false;
3446 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3448 SmallPtrSet<SDNode*, 32> UsersHandled;
3450 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3452 if (User->getNumOperands() == 1 ||
3453 UsersHandled.insert(User)) // First time we've seen this?
3454 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3455 if (User->getOperand(i) == TheValue) {
3457 return false; // too many uses
3462 // Found exactly the right number of uses?
3467 /// hasAnyUseOfValue - Return true if there are any use of the indicated
3468 /// value. This method ignores uses of other values defined by this operation.
3469 bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3470 assert(Value < getNumValues() && "Bad value!");
3472 if (use_size() == 0) return false;
3474 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3476 SmallPtrSet<SDNode*, 32> UsersHandled;
3478 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3480 if (User->getNumOperands() == 1 ||
3481 UsersHandled.insert(User)) // First time we've seen this?
3482 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3483 if (User->getOperand(i) == TheValue) {
3492 /// isOnlyUse - Return true if this node is the only use of N.
3494 bool SDNode::isOnlyUse(SDNode *N) const {
3496 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3507 /// isOperand - Return true if this node is an operand of N.
3509 bool SDOperand::isOperand(SDNode *N) const {
3510 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3511 if (*this == N->getOperand(i))
3516 bool SDNode::isOperand(SDNode *N) const {
3517 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3518 if (this == N->OperandList[i].Val)
3523 static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
3524 SmallPtrSet<SDNode *, 32> &Visited) {
3525 if (found || !Visited.insert(N))
3528 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3529 SDNode *Op = N->getOperand(i).Val;
3534 findPredecessor(Op, P, found, Visited);
3538 /// isPredecessor - Return true if this node is a predecessor of N. This node
3539 /// is either an operand of N or it can be reached by recursively traversing
3540 /// up the operands.
3541 /// NOTE: this is an expensive method. Use it carefully.
3542 bool SDNode::isPredecessor(SDNode *N) const {
3543 SmallPtrSet<SDNode *, 32> Visited;
3545 findPredecessor(N, this, found, Visited);
3549 uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3550 assert(Num < NumOperands && "Invalid child # of SDNode!");
3551 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3554 std::string SDNode::getOperationName(const SelectionDAG *G) const {
3555 switch (getOpcode()) {
3557 if (getOpcode() < ISD::BUILTIN_OP_END)
3558 return "<<Unknown DAG Node>>";
3561 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
3562 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
3563 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
3565 TargetLowering &TLI = G->getTargetLoweringInfo();
3567 TLI.getTargetNodeName(getOpcode());
3568 if (Name) return Name;
3571 return "<<Unknown Target Node>>";
3574 case ISD::PCMARKER: return "PCMarker";
3575 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
3576 case ISD::SRCVALUE: return "SrcValue";
3577 case ISD::EntryToken: return "EntryToken";
3578 case ISD::TokenFactor: return "TokenFactor";
3579 case ISD::AssertSext: return "AssertSext";
3580 case ISD::AssertZext: return "AssertZext";
3582 case ISD::STRING: return "String";
3583 case ISD::BasicBlock: return "BasicBlock";
3584 case ISD::VALUETYPE: return "ValueType";
3585 case ISD::Register: return "Register";
3587 case ISD::Constant: return "Constant";
3588 case ISD::ConstantFP: return "ConstantFP";
3589 case ISD::GlobalAddress: return "GlobalAddress";
3590 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
3591 case ISD::FrameIndex: return "FrameIndex";
3592 case ISD::JumpTable: return "JumpTable";
3593 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
3594 case ISD::RETURNADDR: return "RETURNADDR";
3595 case ISD::FRAMEADDR: return "FRAMEADDR";
3596 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
3597 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3598 case ISD::EHSELECTION: return "EHSELECTION";
3599 case ISD::EH_RETURN: return "EH_RETURN";
3600 case ISD::ConstantPool: return "ConstantPool";
3601 case ISD::ExternalSymbol: return "ExternalSymbol";
3602 case ISD::INTRINSIC_WO_CHAIN: {
3603 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3604 return Intrinsic::getName((Intrinsic::ID)IID);
3606 case ISD::INTRINSIC_VOID:
3607 case ISD::INTRINSIC_W_CHAIN: {
3608 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
3609 return Intrinsic::getName((Intrinsic::ID)IID);
3612 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
3613 case ISD::TargetConstant: return "TargetConstant";
3614 case ISD::TargetConstantFP:return "TargetConstantFP";
3615 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
3616 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
3617 case ISD::TargetFrameIndex: return "TargetFrameIndex";
3618 case ISD::TargetJumpTable: return "TargetJumpTable";
3619 case ISD::TargetConstantPool: return "TargetConstantPool";
3620 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
3622 case ISD::CopyToReg: return "CopyToReg";
3623 case ISD::CopyFromReg: return "CopyFromReg";
3624 case ISD::UNDEF: return "undef";
3625 case ISD::MERGE_VALUES: return "merge_values";
3626 case ISD::INLINEASM: return "inlineasm";
3627 case ISD::LABEL: return "label";
3628 case ISD::HANDLENODE: return "handlenode";
3629 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
3630 case ISD::CALL: return "call";
3633 case ISD::FABS: return "fabs";
3634 case ISD::FNEG: return "fneg";
3635 case ISD::FSQRT: return "fsqrt";
3636 case ISD::FSIN: return "fsin";
3637 case ISD::FCOS: return "fcos";
3638 case ISD::FPOWI: return "fpowi";
3639 case ISD::FPOW: return "fpow";
3642 case ISD::ADD: return "add";
3643 case ISD::SUB: return "sub";
3644 case ISD::MUL: return "mul";
3645 case ISD::MULHU: return "mulhu";
3646 case ISD::MULHS: return "mulhs";
3647 case ISD::SDIV: return "sdiv";
3648 case ISD::UDIV: return "udiv";
3649 case ISD::SREM: return "srem";
3650 case ISD::UREM: return "urem";
3651 case ISD::SMUL_LOHI: return "smul_lohi";
3652 case ISD::UMUL_LOHI: return "umul_lohi";
3653 case ISD::SDIVREM: return "sdivrem";
3654 case ISD::UDIVREM: return "divrem";
3655 case ISD::AND: return "and";
3656 case ISD::OR: return "or";
3657 case ISD::XOR: return "xor";
3658 case ISD::SHL: return "shl";
3659 case ISD::SRA: return "sra";
3660 case ISD::SRL: return "srl";
3661 case ISD::ROTL: return "rotl";
3662 case ISD::ROTR: return "rotr";
3663 case ISD::FADD: return "fadd";
3664 case ISD::FSUB: return "fsub";
3665 case ISD::FMUL: return "fmul";
3666 case ISD::FDIV: return "fdiv";
3667 case ISD::FREM: return "frem";
3668 case ISD::FCOPYSIGN: return "fcopysign";
3670 case ISD::SETCC: return "setcc";
3671 case ISD::SELECT: return "select";
3672 case ISD::SELECT_CC: return "select_cc";
3673 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
3674 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
3675 case ISD::CONCAT_VECTORS: return "concat_vectors";
3676 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
3677 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
3678 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
3679 case ISD::CARRY_FALSE: return "carry_false";
3680 case ISD::ADDC: return "addc";
3681 case ISD::ADDE: return "adde";
3682 case ISD::SUBC: return "subc";
3683 case ISD::SUBE: return "sube";
3684 case ISD::SHL_PARTS: return "shl_parts";
3685 case ISD::SRA_PARTS: return "sra_parts";
3686 case ISD::SRL_PARTS: return "srl_parts";
3688 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3689 case ISD::INSERT_SUBREG: return "insert_subreg";
3691 // Conversion operators.
3692 case ISD::SIGN_EXTEND: return "sign_extend";
3693 case ISD::ZERO_EXTEND: return "zero_extend";
3694 case ISD::ANY_EXTEND: return "any_extend";
3695 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
3696 case ISD::TRUNCATE: return "truncate";
3697 case ISD::FP_ROUND: return "fp_round";
3698 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
3699 case ISD::FP_EXTEND: return "fp_extend";
3701 case ISD::SINT_TO_FP: return "sint_to_fp";
3702 case ISD::UINT_TO_FP: return "uint_to_fp";
3703 case ISD::FP_TO_SINT: return "fp_to_sint";
3704 case ISD::FP_TO_UINT: return "fp_to_uint";
3705 case ISD::BIT_CONVERT: return "bit_convert";
3707 // Control flow instructions
3708 case ISD::BR: return "br";
3709 case ISD::BRIND: return "brind";
3710 case ISD::BR_JT: return "br_jt";
3711 case ISD::BRCOND: return "brcond";
3712 case ISD::BR_CC: return "br_cc";
3713 case ISD::RET: return "ret";
3714 case ISD::CALLSEQ_START: return "callseq_start";
3715 case ISD::CALLSEQ_END: return "callseq_end";
3718 case ISD::LOAD: return "load";
3719 case ISD::STORE: return "store";
3720 case ISD::VAARG: return "vaarg";
3721 case ISD::VACOPY: return "vacopy";
3722 case ISD::VAEND: return "vaend";
3723 case ISD::VASTART: return "vastart";
3724 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
3725 case ISD::EXTRACT_ELEMENT: return "extract_element";
3726 case ISD::BUILD_PAIR: return "build_pair";
3727 case ISD::STACKSAVE: return "stacksave";
3728 case ISD::STACKRESTORE: return "stackrestore";
3730 // Block memory operations.
3731 case ISD::MEMSET: return "memset";
3732 case ISD::MEMCPY: return "memcpy";
3733 case ISD::MEMMOVE: return "memmove";
3736 case ISD::BSWAP: return "bswap";
3737 case ISD::CTPOP: return "ctpop";
3738 case ISD::CTTZ: return "cttz";
3739 case ISD::CTLZ: return "ctlz";
3742 case ISD::LOCATION: return "location";
3743 case ISD::DEBUG_LOC: return "debug_loc";
3746 case ISD::TRAMPOLINE: return "trampoline";
3749 switch (cast<CondCodeSDNode>(this)->get()) {
3750 default: assert(0 && "Unknown setcc condition!");
3751 case ISD::SETOEQ: return "setoeq";
3752 case ISD::SETOGT: return "setogt";
3753 case ISD::SETOGE: return "setoge";
3754 case ISD::SETOLT: return "setolt";
3755 case ISD::SETOLE: return "setole";
3756 case ISD::SETONE: return "setone";
3758 case ISD::SETO: return "seto";
3759 case ISD::SETUO: return "setuo";
3760 case ISD::SETUEQ: return "setue";
3761 case ISD::SETUGT: return "setugt";
3762 case ISD::SETUGE: return "setuge";
3763 case ISD::SETULT: return "setult";
3764 case ISD::SETULE: return "setule";
3765 case ISD::SETUNE: return "setune";
3767 case ISD::SETEQ: return "seteq";
3768 case ISD::SETGT: return "setgt";
3769 case ISD::SETGE: return "setge";
3770 case ISD::SETLT: return "setlt";
3771 case ISD::SETLE: return "setle";
3772 case ISD::SETNE: return "setne";
3777 const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
3786 return "<post-inc>";
3788 return "<post-dec>";
3792 void SDNode::dump() const { dump(0); }
3793 void SDNode::dump(const SelectionDAG *G) const {
3794 cerr << (void*)this << ": ";
3796 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
3798 if (getValueType(i) == MVT::Other)
3801 cerr << MVT::getValueTypeString(getValueType(i));
3803 cerr << " = " << getOperationName(G);
3806 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
3807 if (i) cerr << ", ";
3808 cerr << (void*)getOperand(i).Val;
3809 if (unsigned RN = getOperand(i).ResNo)
3813 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
3814 cerr << "<" << CSDN->getValue() << ">";
3815 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
3816 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
3817 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
3818 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
3819 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
3821 cerr << "<APFloat(";
3822 CSDN->getValueAPF().convertToAPInt().dump();
3825 } else if (const GlobalAddressSDNode *GADN =
3826 dyn_cast<GlobalAddressSDNode>(this)) {
3827 int offset = GADN->getOffset();
3829 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
3831 cerr << " + " << offset;
3833 cerr << " " << offset;
3834 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
3835 cerr << "<" << FIDN->getIndex() << ">";
3836 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
3837 cerr << "<" << JTDN->getIndex() << ">";
3838 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
3839 int offset = CP->getOffset();
3840 if (CP->isMachineConstantPoolEntry())
3841 cerr << "<" << *CP->getMachineCPVal() << ">";
3843 cerr << "<" << *CP->getConstVal() << ">";
3845 cerr << " + " << offset;
3847 cerr << " " << offset;
3848 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
3850 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3852 cerr << LBB->getName() << " ";
3853 cerr << (const void*)BBDN->getBasicBlock() << ">";
3854 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
3855 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
3856 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
3858 cerr << " #" << R->getReg();
3860 } else if (const ExternalSymbolSDNode *ES =
3861 dyn_cast<ExternalSymbolSDNode>(this)) {
3862 cerr << "'" << ES->getSymbol() << "'";
3863 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3865 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
3867 cerr << "<null:" << M->getOffset() << ">";
3868 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
3869 cerr << ":" << MVT::getValueTypeString(N->getVT());
3870 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
3872 switch (LD->getExtensionType()) {
3873 default: doExt = false; break;
3875 cerr << " <anyext ";
3885 cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
3887 const char *AM = getIndexedModeName(LD->getAddressingMode());
3890 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
3891 if (ST->isTruncatingStore())
3893 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
3895 const char *AM = getIndexedModeName(ST->getAddressingMode());
3901 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
3902 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3903 if (N->getOperand(i).Val->hasOneUse())
3904 DumpNodes(N->getOperand(i).Val, indent+2, G);
3906 cerr << "\n" << std::string(indent+2, ' ')
3907 << (void*)N->getOperand(i).Val << ": <multiple use>";
3910 cerr << "\n" << std::string(indent, ' ');
3914 void SelectionDAG::dump() const {
3915 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
3916 std::vector<const SDNode*> Nodes;
3917 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3921 std::sort(Nodes.begin(), Nodes.end());
3923 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
3924 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
3925 DumpNodes(Nodes[i], 2, this);
3928 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
3933 const Type *ConstantPoolSDNode::getType() const {
3934 if (isMachineConstantPoolEntry())
3935 return Val.MachineCPVal->getType();
3936 return Val.ConstVal->getType();