1 //===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/CodeGen/MachineModuleInfo.h"
24 #include "llvm/Support/MathExtras.h"
25 #include "llvm/Target/MRegisterInfo.h"
26 #include "llvm/Target/TargetData.h"
27 #include "llvm/Target/TargetLowering.h"
28 #include "llvm/Target/TargetInstrInfo.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include "llvm/ADT/SetVector.h"
31 #include "llvm/ADT/SmallPtrSet.h"
32 #include "llvm/ADT/SmallSet.h"
33 #include "llvm/ADT/SmallVector.h"
34 #include "llvm/ADT/StringExtras.h"
39 /// makeVTList - Return an instance of the SDVTList struct initialized with the
40 /// specified members.
41 static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
42 SDVTList Res = {VTs, NumVTs};
46 //===----------------------------------------------------------------------===//
47 // ConstantFPSDNode Class
48 //===----------------------------------------------------------------------===//
50 /// isExactlyValue - We don't rely on operator== working on double values, as
51 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
52 /// As such, this method can be used to do an exact bit-for-bit comparison of
53 /// two floating point values.
54 bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
55 return Value.bitwiseIsEqual(V);
58 bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
60 // convert modifies in place, so make a copy.
61 APFloat Val2 = APFloat(Val);
64 return false; // These can't be represented as floating point!
66 // FIXME rounding mode needs to be more flexible
68 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
69 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) ==
72 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
73 &Val2.getSemantics() == &APFloat::IEEEdouble ||
74 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
76 // TODO: Figure out how to test if we can use a shorter type instead!
84 //===----------------------------------------------------------------------===//
86 //===----------------------------------------------------------------------===//
88 /// isBuildVectorAllOnes - Return true if the specified node is a
89 /// BUILD_VECTOR where all of the elements are ~0 or undef.
90 bool ISD::isBuildVectorAllOnes(const SDNode *N) {
91 // Look through a bit convert.
92 if (N->getOpcode() == ISD::BIT_CONVERT)
93 N = N->getOperand(0).Val;
95 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
97 unsigned i = 0, e = N->getNumOperands();
99 // Skip over all of the undef values.
100 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
103 // Do not accept an all-undef vector.
104 if (i == e) return false;
106 // Do not accept build_vectors that aren't all constants or which have non-~0
108 SDOperand NotZero = N->getOperand(i);
109 if (isa<ConstantSDNode>(NotZero)) {
110 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
112 } else if (isa<ConstantFPSDNode>(NotZero)) {
113 MVT::ValueType VT = NotZero.getValueType();
115 if (((cast<ConstantFPSDNode>(NotZero)->getValueAPF().
116 convertToAPInt().getZExtValue())) != (uint64_t)-1)
119 if ((uint32_t)cast<ConstantFPSDNode>(NotZero)->
120 getValueAPF().convertToAPInt().getZExtValue() !=
127 // Okay, we have at least one ~0 value, check to see if the rest match or are
129 for (++i; i != e; ++i)
130 if (N->getOperand(i) != NotZero &&
131 N->getOperand(i).getOpcode() != ISD::UNDEF)
137 /// isBuildVectorAllZeros - Return true if the specified node is a
138 /// BUILD_VECTOR where all of the elements are 0 or undef.
139 bool ISD::isBuildVectorAllZeros(const SDNode *N) {
140 // Look through a bit convert.
141 if (N->getOpcode() == ISD::BIT_CONVERT)
142 N = N->getOperand(0).Val;
144 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
146 unsigned i = 0, e = N->getNumOperands();
148 // Skip over all of the undef values.
149 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
152 // Do not accept an all-undef vector.
153 if (i == e) return false;
155 // Do not accept build_vectors that aren't all constants or which have non-~0
157 SDOperand Zero = N->getOperand(i);
158 if (isa<ConstantSDNode>(Zero)) {
159 if (!cast<ConstantSDNode>(Zero)->isNullValue())
161 } else if (isa<ConstantFPSDNode>(Zero)) {
162 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
167 // Okay, we have at least one ~0 value, check to see if the rest match or are
169 for (++i; i != e; ++i)
170 if (N->getOperand(i) != Zero &&
171 N->getOperand(i).getOpcode() != ISD::UNDEF)
176 /// isDebugLabel - Return true if the specified node represents a debug
177 /// label (i.e. ISD::LABEL or TargetInstrInfo::LANEL node and third operand
179 bool ISD::isDebugLabel(const SDNode *N) {
181 if (N->getOpcode() == ISD::LABEL)
182 Zero = N->getOperand(2);
183 else if (N->isTargetOpcode() &&
184 N->getTargetOpcode() == TargetInstrInfo::LABEL)
185 // Chain moved to last operand.
186 Zero = N->getOperand(1);
189 return isa<ConstantSDNode>(Zero) && cast<ConstantSDNode>(Zero)->isNullValue();
192 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
193 /// when given the operation for (X op Y).
194 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
195 // To perform this operation, we just need to swap the L and G bits of the
197 unsigned OldL = (Operation >> 2) & 1;
198 unsigned OldG = (Operation >> 1) & 1;
199 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
200 (OldL << 1) | // New G bit
201 (OldG << 2)); // New L bit.
204 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
205 /// 'op' is a valid SetCC operation.
206 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
207 unsigned Operation = Op;
209 Operation ^= 7; // Flip L, G, E bits, but not U.
211 Operation ^= 15; // Flip all of the condition bits.
212 if (Operation > ISD::SETTRUE2)
213 Operation &= ~8; // Don't let N and U bits get set.
214 return ISD::CondCode(Operation);
218 /// isSignedOp - For an integer comparison, return 1 if the comparison is a
219 /// signed operation and 2 if the result is an unsigned comparison. Return zero
220 /// if the operation does not depend on the sign of the input (setne and seteq).
221 static int isSignedOp(ISD::CondCode Opcode) {
223 default: assert(0 && "Illegal integer setcc operation!");
225 case ISD::SETNE: return 0;
229 case ISD::SETGE: return 1;
233 case ISD::SETUGE: return 2;
237 /// getSetCCOrOperation - Return the result of a logical OR between different
238 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
239 /// returns SETCC_INVALID if it is not possible to represent the resultant
241 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
243 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
244 // Cannot fold a signed integer setcc with an unsigned integer setcc.
245 return ISD::SETCC_INVALID;
247 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
249 // If the N and U bits get set then the resultant comparison DOES suddenly
250 // care about orderedness, and is true when ordered.
251 if (Op > ISD::SETTRUE2)
252 Op &= ~16; // Clear the U bit if the N bit is set.
254 // Canonicalize illegal integer setcc's.
255 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
258 return ISD::CondCode(Op);
261 /// getSetCCAndOperation - Return the result of a logical AND between different
262 /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
263 /// function returns zero if it is not possible to represent the resultant
265 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
267 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
268 // Cannot fold a signed setcc with an unsigned setcc.
269 return ISD::SETCC_INVALID;
271 // Combine all of the condition bits.
272 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
274 // Canonicalize illegal integer setcc's.
278 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
279 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
280 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
281 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
288 const TargetMachine &SelectionDAG::getTarget() const {
289 return TLI.getTargetMachine();
292 //===----------------------------------------------------------------------===//
293 // SDNode Profile Support
294 //===----------------------------------------------------------------------===//
296 /// AddNodeIDOpcode - Add the node opcode to the NodeID data.
298 static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
302 /// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
303 /// solely with their pointer.
304 void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
305 ID.AddPointer(VTList.VTs);
308 /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
310 static void AddNodeIDOperands(FoldingSetNodeID &ID,
311 const SDOperand *Ops, unsigned NumOps) {
312 for (; NumOps; --NumOps, ++Ops) {
313 ID.AddPointer(Ops->Val);
314 ID.AddInteger(Ops->ResNo);
318 static void AddNodeIDNode(FoldingSetNodeID &ID,
319 unsigned short OpC, SDVTList VTList,
320 const SDOperand *OpList, unsigned N) {
321 AddNodeIDOpcode(ID, OpC);
322 AddNodeIDValueTypes(ID, VTList);
323 AddNodeIDOperands(ID, OpList, N);
326 /// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
328 static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
329 AddNodeIDOpcode(ID, N->getOpcode());
330 // Add the return value info.
331 AddNodeIDValueTypes(ID, N->getVTList());
332 // Add the operand info.
333 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
335 // Handle SDNode leafs with special info.
336 switch (N->getOpcode()) {
337 default: break; // Normal nodes don't need extra info.
338 case ISD::TargetConstant:
340 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
342 case ISD::TargetConstantFP:
343 case ISD::ConstantFP: {
344 ID.AddAPFloat(cast<ConstantFPSDNode>(N)->getValueAPF());
347 case ISD::TargetGlobalAddress:
348 case ISD::GlobalAddress:
349 case ISD::TargetGlobalTLSAddress:
350 case ISD::GlobalTLSAddress: {
351 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
352 ID.AddPointer(GA->getGlobal());
353 ID.AddInteger(GA->getOffset());
356 case ISD::BasicBlock:
357 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
360 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
362 case ISD::SRCVALUE: {
363 SrcValueSDNode *SV = cast<SrcValueSDNode>(N);
364 ID.AddPointer(SV->getValue());
365 ID.AddInteger(SV->getOffset());
368 case ISD::FrameIndex:
369 case ISD::TargetFrameIndex:
370 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
373 case ISD::TargetJumpTable:
374 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
376 case ISD::ConstantPool:
377 case ISD::TargetConstantPool: {
378 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
379 ID.AddInteger(CP->getAlignment());
380 ID.AddInteger(CP->getOffset());
381 if (CP->isMachineConstantPoolEntry())
382 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
384 ID.AddPointer(CP->getConstVal());
388 LoadSDNode *LD = cast<LoadSDNode>(N);
389 ID.AddInteger(LD->getAddressingMode());
390 ID.AddInteger(LD->getExtensionType());
391 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
392 ID.AddInteger(LD->getAlignment());
393 ID.AddInteger(LD->isVolatile());
397 StoreSDNode *ST = cast<StoreSDNode>(N);
398 ID.AddInteger(ST->getAddressingMode());
399 ID.AddInteger(ST->isTruncatingStore());
400 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
401 ID.AddInteger(ST->getAlignment());
402 ID.AddInteger(ST->isVolatile());
408 //===----------------------------------------------------------------------===//
409 // SelectionDAG Class
410 //===----------------------------------------------------------------------===//
412 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
414 void SelectionDAG::RemoveDeadNodes() {
415 // Create a dummy node (which is not added to allnodes), that adds a reference
416 // to the root node, preventing it from being deleted.
417 HandleSDNode Dummy(getRoot());
419 SmallVector<SDNode*, 128> DeadNodes;
421 // Add all obviously-dead nodes to the DeadNodes worklist.
422 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
424 DeadNodes.push_back(I);
426 // Process the worklist, deleting the nodes and adding their uses to the
428 while (!DeadNodes.empty()) {
429 SDNode *N = DeadNodes.back();
430 DeadNodes.pop_back();
432 // Take the node out of the appropriate CSE map.
433 RemoveNodeFromCSEMaps(N);
435 // Next, brutally remove the operand list. This is safe to do, as there are
436 // no cycles in the graph.
437 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
438 SDNode *Operand = I->Val;
439 Operand->removeUser(N);
441 // Now that we removed this operand, see if there are no uses of it left.
442 if (Operand->use_empty())
443 DeadNodes.push_back(Operand);
445 if (N->OperandsNeedDelete)
446 delete[] N->OperandList;
450 // Finally, remove N itself.
454 // If the root changed (e.g. it was a dead load, update the root).
455 setRoot(Dummy.getValue());
458 void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
459 SmallVector<SDNode*, 16> DeadNodes;
460 DeadNodes.push_back(N);
462 // Process the worklist, deleting the nodes and adding their uses to the
464 while (!DeadNodes.empty()) {
465 SDNode *N = DeadNodes.back();
466 DeadNodes.pop_back();
468 // Take the node out of the appropriate CSE map.
469 RemoveNodeFromCSEMaps(N);
471 // Next, brutally remove the operand list. This is safe to do, as there are
472 // no cycles in the graph.
473 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
474 SDNode *Operand = I->Val;
475 Operand->removeUser(N);
477 // Now that we removed this operand, see if there are no uses of it left.
478 if (Operand->use_empty())
479 DeadNodes.push_back(Operand);
481 if (N->OperandsNeedDelete)
482 delete[] N->OperandList;
486 // Finally, remove N itself.
487 Deleted.push_back(N);
492 void SelectionDAG::DeleteNode(SDNode *N) {
493 assert(N->use_empty() && "Cannot delete a node that is not dead!");
495 // First take this out of the appropriate CSE map.
496 RemoveNodeFromCSEMaps(N);
498 // Finally, remove uses due to operands of this node, remove from the
499 // AllNodes list, and delete the node.
500 DeleteNodeNotInCSEMaps(N);
503 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
505 // Remove it from the AllNodes list.
508 // Drop all of the operands and decrement used nodes use counts.
509 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
510 I->Val->removeUser(N);
511 if (N->OperandsNeedDelete)
512 delete[] N->OperandList;
519 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
520 /// correspond to it. This is useful when we're about to delete or repurpose
521 /// the node. We don't want future request for structurally identical nodes
522 /// to return N anymore.
523 void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
525 switch (N->getOpcode()) {
526 case ISD::HANDLENODE: return; // noop.
528 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
531 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
532 "Cond code doesn't exist!");
533 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
534 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
536 case ISD::ExternalSymbol:
537 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
539 case ISD::TargetExternalSymbol:
541 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
543 case ISD::VALUETYPE: {
544 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
545 if (MVT::isExtendedVT(VT)) {
546 Erased = ExtendedValueTypeNodes.erase(VT);
548 Erased = ValueTypeNodes[VT] != 0;
549 ValueTypeNodes[VT] = 0;
554 // Remove it from the CSE Map.
555 Erased = CSEMap.RemoveNode(N);
559 // Verify that the node was actually in one of the CSE maps, unless it has a
560 // flag result (which cannot be CSE'd) or is one of the special cases that are
561 // not subject to CSE.
562 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
563 !N->isTargetOpcode()) {
566 assert(0 && "Node is not in map!");
571 /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
572 /// has been taken out and modified in some way. If the specified node already
573 /// exists in the CSE maps, do not modify the maps, but return the existing node
574 /// instead. If it doesn't exist, add it and return null.
576 SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
577 assert(N->getNumOperands() && "This is a leaf node!");
578 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
579 return 0; // Never add these nodes.
581 // Check that remaining values produced are not flags.
582 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
583 if (N->getValueType(i) == MVT::Flag)
584 return 0; // Never CSE anything that produces a flag.
586 SDNode *New = CSEMap.GetOrInsertNode(N);
587 if (New != N) return New; // Node already existed.
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, SDOperand Op,
597 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
598 return 0; // Never add these nodes.
600 // Check that remaining values produced are not flags.
601 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
602 if (N->getValueType(i) == MVT::Flag)
603 return 0; // Never CSE anything that produces a flag.
605 SDOperand Ops[] = { Op };
607 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
608 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
611 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
612 /// were replaced with those specified. If this node is never memoized,
613 /// return null, otherwise return a pointer to the slot it would take. If a
614 /// node already exists with these operands, the slot will be non-null.
615 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
616 SDOperand Op1, SDOperand Op2,
618 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
619 return 0; // Never add these nodes.
621 // Check that remaining values produced are not flags.
622 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
623 if (N->getValueType(i) == MVT::Flag)
624 return 0; // Never CSE anything that produces a flag.
626 SDOperand Ops[] = { Op1, Op2 };
628 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
629 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
633 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
634 /// were replaced with those specified. If this node is never memoized,
635 /// return null, otherwise return a pointer to the slot it would take. If a
636 /// node already exists with these operands, the slot will be non-null.
637 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
638 const SDOperand *Ops,unsigned NumOps,
640 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
641 return 0; // Never add these nodes.
643 // Check that remaining values produced are not flags.
644 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
645 if (N->getValueType(i) == MVT::Flag)
646 return 0; // Never CSE anything that produces a flag.
649 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
651 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
652 ID.AddInteger(LD->getAddressingMode());
653 ID.AddInteger(LD->getExtensionType());
654 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
655 ID.AddInteger(LD->getAlignment());
656 ID.AddInteger(LD->isVolatile());
657 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
658 ID.AddInteger(ST->getAddressingMode());
659 ID.AddInteger(ST->isTruncatingStore());
660 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
661 ID.AddInteger(ST->getAlignment());
662 ID.AddInteger(ST->isVolatile());
665 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
669 SelectionDAG::~SelectionDAG() {
670 while (!AllNodes.empty()) {
671 SDNode *N = AllNodes.begin();
672 N->SetNextInBucket(0);
673 if (N->OperandsNeedDelete)
674 delete [] N->OperandList;
677 AllNodes.pop_front();
681 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
682 if (Op.getValueType() == VT) return Op;
683 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
684 return getNode(ISD::AND, Op.getValueType(), Op,
685 getConstant(Imm, Op.getValueType()));
688 SDOperand SelectionDAG::getString(const std::string &Val) {
689 StringSDNode *&N = StringNodes[Val];
691 N = new StringSDNode(Val);
692 AllNodes.push_back(N);
694 return SDOperand(N, 0);
697 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
698 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
700 MVT::ValueType EltVT =
701 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
703 // Mask out any bits that are not valid for this constant.
704 Val &= MVT::getIntVTBitMask(EltVT);
706 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
708 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
712 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
713 if (!MVT::isVector(VT))
714 return SDOperand(N, 0);
716 N = new ConstantSDNode(isT, Val, EltVT);
717 CSEMap.InsertNode(N, IP);
718 AllNodes.push_back(N);
721 SDOperand Result(N, 0);
722 if (MVT::isVector(VT)) {
723 SmallVector<SDOperand, 8> Ops;
724 Ops.assign(MVT::getVectorNumElements(VT), Result);
725 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
730 SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
731 return getConstant(Val, TLI.getPointerTy(), isTarget);
735 SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
737 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
739 MVT::ValueType EltVT =
740 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
742 // Do the map lookup using the actual bit pattern for the floating point
743 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
744 // we don't have issues with SNANs.
745 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
747 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
751 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
752 if (!MVT::isVector(VT))
753 return SDOperand(N, 0);
755 N = new ConstantFPSDNode(isTarget, V, EltVT);
756 CSEMap.InsertNode(N, IP);
757 AllNodes.push_back(N);
760 SDOperand Result(N, 0);
761 if (MVT::isVector(VT)) {
762 SmallVector<SDOperand, 8> Ops;
763 Ops.assign(MVT::getVectorNumElements(VT), Result);
764 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
769 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
771 MVT::ValueType EltVT =
772 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
774 return getConstantFP(APFloat((float)Val), VT, isTarget);
776 return getConstantFP(APFloat(Val), VT, isTarget);
779 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
780 MVT::ValueType VT, int Offset,
782 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
784 if (GVar && GVar->isThreadLocal())
785 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
787 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
789 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
791 ID.AddInteger(Offset);
793 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
794 return SDOperand(E, 0);
795 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
796 CSEMap.InsertNode(N, IP);
797 AllNodes.push_back(N);
798 return SDOperand(N, 0);
801 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
803 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
805 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
808 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
809 return SDOperand(E, 0);
810 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
811 CSEMap.InsertNode(N, IP);
812 AllNodes.push_back(N);
813 return SDOperand(N, 0);
816 SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
817 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
819 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
822 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
823 return SDOperand(E, 0);
824 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
825 CSEMap.InsertNode(N, IP);
826 AllNodes.push_back(N);
827 return SDOperand(N, 0);
830 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
831 unsigned Alignment, int Offset,
833 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
835 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
836 ID.AddInteger(Alignment);
837 ID.AddInteger(Offset);
840 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
841 return SDOperand(E, 0);
842 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
843 CSEMap.InsertNode(N, IP);
844 AllNodes.push_back(N);
845 return SDOperand(N, 0);
849 SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
851 unsigned Alignment, int Offset,
853 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
855 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
856 ID.AddInteger(Alignment);
857 ID.AddInteger(Offset);
858 C->AddSelectionDAGCSEId(ID);
860 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
861 return SDOperand(E, 0);
862 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
863 CSEMap.InsertNode(N, IP);
864 AllNodes.push_back(N);
865 return SDOperand(N, 0);
869 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
871 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
874 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
875 return SDOperand(E, 0);
876 SDNode *N = new BasicBlockSDNode(MBB);
877 CSEMap.InsertNode(N, IP);
878 AllNodes.push_back(N);
879 return SDOperand(N, 0);
882 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
883 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
884 ValueTypeNodes.resize(VT+1);
886 SDNode *&N = MVT::isExtendedVT(VT) ?
887 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
889 if (N) return SDOperand(N, 0);
890 N = new VTSDNode(VT);
891 AllNodes.push_back(N);
892 return SDOperand(N, 0);
895 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
896 SDNode *&N = ExternalSymbols[Sym];
897 if (N) return SDOperand(N, 0);
898 N = new ExternalSymbolSDNode(false, Sym, VT);
899 AllNodes.push_back(N);
900 return SDOperand(N, 0);
903 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
905 SDNode *&N = TargetExternalSymbols[Sym];
906 if (N) return SDOperand(N, 0);
907 N = new ExternalSymbolSDNode(true, Sym, VT);
908 AllNodes.push_back(N);
909 return SDOperand(N, 0);
912 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
913 if ((unsigned)Cond >= CondCodeNodes.size())
914 CondCodeNodes.resize(Cond+1);
916 if (CondCodeNodes[Cond] == 0) {
917 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
918 AllNodes.push_back(CondCodeNodes[Cond]);
920 return SDOperand(CondCodeNodes[Cond], 0);
923 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
925 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
926 ID.AddInteger(RegNo);
928 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
929 return SDOperand(E, 0);
930 SDNode *N = new RegisterSDNode(RegNo, VT);
931 CSEMap.InsertNode(N, IP);
932 AllNodes.push_back(N);
933 return SDOperand(N, 0);
936 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
937 assert((!V || isa<PointerType>(V->getType())) &&
938 "SrcValue is not a pointer?");
941 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
943 ID.AddInteger(Offset);
945 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
946 return SDOperand(E, 0);
947 SDNode *N = new SrcValueSDNode(V, Offset);
948 CSEMap.InsertNode(N, IP);
949 AllNodes.push_back(N);
950 return SDOperand(N, 0);
953 /// CreateStackTemporary - Create a stack temporary, suitable for holding the
954 /// specified value type.
955 SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
956 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
957 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
958 const Type *Ty = MVT::getTypeForValueType(VT);
959 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
960 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
961 return getFrameIndex(FrameIdx, TLI.getPointerTy());
965 SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
966 SDOperand N2, ISD::CondCode Cond) {
967 // These setcc operations always fold.
971 case ISD::SETFALSE2: return getConstant(0, VT);
973 case ISD::SETTRUE2: return getConstant(1, VT);
985 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
989 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
990 uint64_t C2 = N2C->getValue();
991 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
992 uint64_t C1 = N1C->getValue();
994 // Sign extend the operands if required
995 if (ISD::isSignedIntSetCC(Cond)) {
996 C1 = N1C->getSignExtended();
997 C2 = N2C->getSignExtended();
1001 default: assert(0 && "Unknown integer setcc!");
1002 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1003 case ISD::SETNE: return getConstant(C1 != C2, VT);
1004 case ISD::SETULT: return getConstant(C1 < C2, VT);
1005 case ISD::SETUGT: return getConstant(C1 > C2, VT);
1006 case ISD::SETULE: return getConstant(C1 <= C2, VT);
1007 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
1008 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
1009 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
1010 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
1011 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
1015 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
1016 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
1017 // No compile time operations on this type yet.
1018 if (N1C->getValueType(0) == MVT::ppcf128)
1021 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
1024 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1025 return getNode(ISD::UNDEF, VT);
1027 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1028 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1029 return getNode(ISD::UNDEF, VT);
1031 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
1032 R==APFloat::cmpLessThan, VT);
1033 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1034 return getNode(ISD::UNDEF, VT);
1036 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1037 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1038 return getNode(ISD::UNDEF, VT);
1040 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1041 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1042 return getNode(ISD::UNDEF, VT);
1044 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
1045 R==APFloat::cmpEqual, VT);
1046 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1047 return getNode(ISD::UNDEF, VT);
1049 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
1050 R==APFloat::cmpEqual, VT);
1051 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1052 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1053 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1054 R==APFloat::cmpEqual, VT);
1055 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1056 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1057 R==APFloat::cmpLessThan, VT);
1058 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1059 R==APFloat::cmpUnordered, VT);
1060 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1061 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
1064 // Ensure that the constant occurs on the RHS.
1065 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
1068 // Could not fold it.
1072 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1073 /// this predicate to simplify operations downstream. Mask is known to be zero
1074 /// for bits that V cannot have.
1075 bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1076 unsigned Depth) const {
1077 // The masks are not wide enough to represent this type! Should use APInt.
1078 if (Op.getValueType() == MVT::i128)
1081 uint64_t KnownZero, KnownOne;
1082 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1083 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1084 return (KnownZero & Mask) == Mask;
1087 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
1088 /// known to be either zero or one and return them in the KnownZero/KnownOne
1089 /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1091 void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1092 uint64_t &KnownZero, uint64_t &KnownOne,
1093 unsigned Depth) const {
1094 KnownZero = KnownOne = 0; // Don't know anything.
1095 if (Depth == 6 || Mask == 0)
1096 return; // Limit search depth.
1098 // The masks are not wide enough to represent this type! Should use APInt.
1099 if (Op.getValueType() == MVT::i128)
1102 uint64_t KnownZero2, KnownOne2;
1104 switch (Op.getOpcode()) {
1106 // We know all of the bits for a constant!
1107 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1108 KnownZero = ~KnownOne & Mask;
1111 // If either the LHS or the RHS are Zero, the result is zero.
1112 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1114 ComputeMaskedBits(Op.getOperand(0), 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 // Output known-1 bits are only known if set in both the LHS & RHS.
1119 KnownOne &= KnownOne2;
1120 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1121 KnownZero |= KnownZero2;
1124 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1126 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1127 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1128 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1130 // Output known-0 bits are only known if clear in both the LHS & RHS.
1131 KnownZero &= KnownZero2;
1132 // Output known-1 are known to be set if set in either the LHS | RHS.
1133 KnownOne |= KnownOne2;
1136 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1137 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1138 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1139 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1141 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1142 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1143 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1144 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1145 KnownZero = KnownZeroOut;
1149 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1150 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1151 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1152 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1154 // Only known if known in both the LHS and RHS.
1155 KnownOne &= KnownOne2;
1156 KnownZero &= KnownZero2;
1158 case ISD::SELECT_CC:
1159 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1160 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1161 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1162 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1164 // Only known if known in both the LHS and RHS.
1165 KnownOne &= KnownOne2;
1166 KnownZero &= KnownZero2;
1169 // If we know the result of a setcc has the top bits zero, use this info.
1170 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1171 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1174 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1175 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1176 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1177 KnownZero, KnownOne, Depth+1);
1178 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1179 KnownZero <<= SA->getValue();
1180 KnownOne <<= SA->getValue();
1181 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1185 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1186 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1187 MVT::ValueType VT = Op.getValueType();
1188 unsigned ShAmt = SA->getValue();
1190 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1191 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1192 KnownZero, KnownOne, Depth+1);
1193 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1194 KnownZero &= TypeMask;
1195 KnownOne &= TypeMask;
1196 KnownZero >>= ShAmt;
1199 uint64_t HighBits = (1ULL << ShAmt)-1;
1200 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1201 KnownZero |= HighBits; // High bits known zero.
1205 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1206 MVT::ValueType VT = Op.getValueType();
1207 unsigned ShAmt = SA->getValue();
1209 // Compute the new bits that are at the top now.
1210 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1212 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1213 // If any of the demanded bits are produced by the sign extension, we also
1214 // demand the input sign bit.
1215 uint64_t HighBits = (1ULL << ShAmt)-1;
1216 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1217 if (HighBits & Mask)
1218 InDemandedMask |= MVT::getIntVTSignBit(VT);
1220 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1222 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1223 KnownZero &= TypeMask;
1224 KnownOne &= TypeMask;
1225 KnownZero >>= ShAmt;
1228 // Handle the sign bits.
1229 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1230 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1232 if (KnownZero & SignBit) {
1233 KnownZero |= HighBits; // New bits are known zero.
1234 } else if (KnownOne & SignBit) {
1235 KnownOne |= HighBits; // New bits are known one.
1239 case ISD::SIGN_EXTEND_INREG: {
1240 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1242 // Sign extension. Compute the demanded bits in the result that are not
1243 // present in the input.
1244 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1246 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1247 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1249 // If the sign extended bits are demanded, we know that the sign
1252 InputDemandedBits |= InSignBit;
1254 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1255 KnownZero, KnownOne, Depth+1);
1256 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1258 // If the sign bit of the input is known set or clear, then we know the
1259 // top bits of the result.
1260 if (KnownZero & InSignBit) { // Input sign bit known clear
1261 KnownZero |= NewBits;
1262 KnownOne &= ~NewBits;
1263 } else if (KnownOne & InSignBit) { // Input sign bit known set
1264 KnownOne |= NewBits;
1265 KnownZero &= ~NewBits;
1266 } else { // Input sign bit unknown
1267 KnownZero &= ~NewBits;
1268 KnownOne &= ~NewBits;
1275 MVT::ValueType VT = Op.getValueType();
1276 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1277 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1282 if (ISD::isZEXTLoad(Op.Val)) {
1283 LoadSDNode *LD = cast<LoadSDNode>(Op);
1284 MVT::ValueType VT = LD->getMemoryVT();
1285 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1289 case ISD::ZERO_EXTEND: {
1290 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1291 uint64_t NewBits = (~InMask) & Mask;
1292 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1294 KnownZero |= NewBits & Mask;
1295 KnownOne &= ~NewBits;
1298 case ISD::SIGN_EXTEND: {
1299 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1300 unsigned InBits = MVT::getSizeInBits(InVT);
1301 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1302 uint64_t InSignBit = 1ULL << (InBits-1);
1303 uint64_t NewBits = (~InMask) & Mask;
1304 uint64_t InDemandedBits = Mask & InMask;
1306 // If any of the sign extended bits are demanded, we know that the sign
1309 InDemandedBits |= InSignBit;
1311 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1313 // If the sign bit is known zero or one, the top bits match.
1314 if (KnownZero & InSignBit) {
1315 KnownZero |= NewBits;
1316 KnownOne &= ~NewBits;
1317 } else if (KnownOne & InSignBit) {
1318 KnownOne |= NewBits;
1319 KnownZero &= ~NewBits;
1320 } else { // Otherwise, top bits aren't known.
1321 KnownOne &= ~NewBits;
1322 KnownZero &= ~NewBits;
1326 case ISD::ANY_EXTEND: {
1327 MVT::ValueType VT = Op.getOperand(0).getValueType();
1328 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1329 KnownZero, KnownOne, Depth+1);
1332 case ISD::TRUNCATE: {
1333 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1334 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1335 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1336 KnownZero &= OutMask;
1337 KnownOne &= OutMask;
1340 case ISD::AssertZext: {
1341 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1342 uint64_t InMask = MVT::getIntVTBitMask(VT);
1343 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1345 KnownZero |= (~InMask) & Mask;
1349 // All bits are zero except the low bit.
1350 KnownZero = MVT::getIntVTBitMask(Op.getValueType()) ^ 1;
1354 // If either the LHS or the RHS are Zero, the result is zero.
1355 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1356 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1357 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1358 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1360 // Output known-0 bits are known if clear or set in both the low clear bits
1361 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1362 // low 3 bits clear.
1363 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1364 CountTrailingZeros_64(~KnownZero2));
1366 KnownZero = (1ULL << KnownZeroOut) - 1;
1371 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1374 // We know that the top bits of C-X are clear if X contains less bits
1375 // than C (i.e. no wrap-around can happen). For example, 20-X is
1376 // positive if we can prove that X is >= 0 and < 16.
1377 MVT::ValueType VT = CLHS->getValueType(0);
1378 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1379 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1380 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1381 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1382 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1384 // If all of the MaskV bits are known to be zero, then we know the output
1385 // top bits are zero, because we now know that the output is from [0-C].
1386 if ((KnownZero & MaskV) == MaskV) {
1387 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1388 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1389 KnownOne = 0; // No one bits known.
1391 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1397 // Allow the target to implement this method for its nodes.
1398 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1399 case ISD::INTRINSIC_WO_CHAIN:
1400 case ISD::INTRINSIC_W_CHAIN:
1401 case ISD::INTRINSIC_VOID:
1402 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1408 /// ComputeNumSignBits - Return the number of times the sign bit of the
1409 /// register is replicated into the other bits. We know that at least 1 bit
1410 /// is always equal to the sign bit (itself), but other cases can give us
1411 /// information. For example, immediately after an "SRA X, 2", we know that
1412 /// the top 3 bits are all equal to each other, so we return 3.
1413 unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1414 MVT::ValueType VT = Op.getValueType();
1415 assert(MVT::isInteger(VT) && "Invalid VT!");
1416 unsigned VTBits = MVT::getSizeInBits(VT);
1420 return 1; // Limit search depth.
1422 switch (Op.getOpcode()) {
1424 case ISD::AssertSext:
1425 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1426 return VTBits-Tmp+1;
1427 case ISD::AssertZext:
1428 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1431 case ISD::Constant: {
1432 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1433 // If negative, invert the bits, then look at it.
1434 if (Val & MVT::getIntVTSignBit(VT))
1437 // Shift the bits so they are the leading bits in the int64_t.
1440 // Return # leading zeros. We use 'min' here in case Val was zero before
1441 // shifting. We don't want to return '64' as for an i32 "0".
1442 return std::min(VTBits, CountLeadingZeros_64(Val));
1445 case ISD::SIGN_EXTEND:
1446 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1447 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1449 case ISD::SIGN_EXTEND_INREG:
1450 // Max of the input and what this extends.
1451 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1454 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1455 return std::max(Tmp, Tmp2);
1458 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1459 // SRA X, C -> adds C sign bits.
1460 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1461 Tmp += C->getValue();
1462 if (Tmp > VTBits) Tmp = VTBits;
1466 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1467 // shl destroys sign bits.
1468 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1469 if (C->getValue() >= VTBits || // Bad shift.
1470 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1471 return Tmp - C->getValue();
1476 case ISD::XOR: // NOT is handled here.
1477 // Logical binary ops preserve the number of sign bits.
1478 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1479 if (Tmp == 1) return 1; // Early out.
1480 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1481 return std::min(Tmp, Tmp2);
1484 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1485 if (Tmp == 1) return 1; // Early out.
1486 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1487 return std::min(Tmp, Tmp2);
1490 // If setcc returns 0/-1, all bits are sign bits.
1491 if (TLI.getSetCCResultContents() ==
1492 TargetLowering::ZeroOrNegativeOneSetCCResult)
1497 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1498 unsigned RotAmt = C->getValue() & (VTBits-1);
1500 // Handle rotate right by N like a rotate left by 32-N.
1501 if (Op.getOpcode() == ISD::ROTR)
1502 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1504 // If we aren't rotating out all of the known-in sign bits, return the
1505 // number that are left. This handles rotl(sext(x), 1) for example.
1506 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1507 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1511 // Add can have at most one carry bit. Thus we know that the output
1512 // is, at worst, one more bit than the inputs.
1513 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1514 if (Tmp == 1) return 1; // Early out.
1516 // Special case decrementing a value (ADD X, -1):
1517 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1518 if (CRHS->isAllOnesValue()) {
1519 uint64_t KnownZero, KnownOne;
1520 uint64_t Mask = MVT::getIntVTBitMask(VT);
1521 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1523 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1525 if ((KnownZero|1) == Mask)
1528 // If we are subtracting one from a positive number, there is no carry
1529 // out of the result.
1530 if (KnownZero & MVT::getIntVTSignBit(VT))
1534 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1535 if (Tmp2 == 1) return 1;
1536 return std::min(Tmp, Tmp2)-1;
1540 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1541 if (Tmp2 == 1) return 1;
1544 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1545 if (CLHS->getValue() == 0) {
1546 uint64_t KnownZero, KnownOne;
1547 uint64_t Mask = MVT::getIntVTBitMask(VT);
1548 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1549 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1551 if ((KnownZero|1) == Mask)
1554 // If the input is known to be positive (the sign bit is known clear),
1555 // the output of the NEG has the same number of sign bits as the input.
1556 if (KnownZero & MVT::getIntVTSignBit(VT))
1559 // Otherwise, we treat this like a SUB.
1562 // Sub can have at most one carry bit. Thus we know that the output
1563 // is, at worst, one more bit than the inputs.
1564 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1565 if (Tmp == 1) return 1; // Early out.
1566 return std::min(Tmp, Tmp2)-1;
1569 // FIXME: it's tricky to do anything useful for this, but it is an important
1570 // case for targets like X86.
1574 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1575 if (Op.getOpcode() == ISD::LOAD) {
1576 LoadSDNode *LD = cast<LoadSDNode>(Op);
1577 unsigned ExtType = LD->getExtensionType();
1580 case ISD::SEXTLOAD: // '17' bits known
1581 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
1582 return VTBits-Tmp+1;
1583 case ISD::ZEXTLOAD: // '16' bits known
1584 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
1589 // Allow the target to implement this method for its nodes.
1590 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1591 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1592 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1593 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1594 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1595 if (NumBits > 1) return NumBits;
1598 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1599 // use this information.
1600 uint64_t KnownZero, KnownOne;
1601 uint64_t Mask = MVT::getIntVTBitMask(VT);
1602 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1604 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1605 if (KnownZero & SignBit) { // SignBit is 0
1607 } else if (KnownOne & SignBit) { // SignBit is 1;
1614 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1615 // the number of identical bits in the top of the input value.
1618 // Return # leading zeros. We use 'min' here in case Val was zero before
1619 // shifting. We don't want to return '64' as for an i32 "0".
1620 return std::min(VTBits, CountLeadingZeros_64(Mask));
1624 bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1625 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1626 if (!GA) return false;
1627 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1628 if (!GV) return false;
1629 MachineModuleInfo *MMI = getMachineModuleInfo();
1630 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1634 /// getNode - Gets or creates the specified node.
1636 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
1637 FoldingSetNodeID ID;
1638 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
1640 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1641 return SDOperand(E, 0);
1642 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
1643 CSEMap.InsertNode(N, IP);
1645 AllNodes.push_back(N);
1646 return SDOperand(N, 0);
1649 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1650 SDOperand Operand) {
1652 // Constant fold unary operations with an integer constant operand.
1653 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1654 uint64_t Val = C->getValue();
1657 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
1658 case ISD::ANY_EXTEND:
1659 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1660 case ISD::TRUNCATE: return getConstant(Val, VT);
1661 case ISD::UINT_TO_FP:
1662 case ISD::SINT_TO_FP: {
1663 const uint64_t zero[] = {0, 0};
1664 // No compile time operations on this type.
1665 if (VT==MVT::ppcf128)
1667 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
1668 (void)apf.convertFromZeroExtendedInteger(&Val,
1669 MVT::getSizeInBits(Operand.getValueType()),
1670 Opcode==ISD::SINT_TO_FP,
1671 APFloat::rmNearestTiesToEven);
1672 return getConstantFP(apf, VT);
1674 case ISD::BIT_CONVERT:
1675 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
1676 return getConstantFP(BitsToFloat(Val), VT);
1677 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
1678 return getConstantFP(BitsToDouble(Val), VT);
1682 default: assert(0 && "Invalid bswap!"); break;
1683 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1684 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1685 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1690 default: assert(0 && "Invalid ctpop!"); break;
1691 case MVT::i1: return getConstant(Val != 0, VT);
1693 Tmp1 = (unsigned)Val & 0xFF;
1694 return getConstant(CountPopulation_32(Tmp1), VT);
1696 Tmp1 = (unsigned)Val & 0xFFFF;
1697 return getConstant(CountPopulation_32(Tmp1), VT);
1699 return getConstant(CountPopulation_32((unsigned)Val), VT);
1701 return getConstant(CountPopulation_64(Val), VT);
1705 default: assert(0 && "Invalid ctlz!"); break;
1706 case MVT::i1: return getConstant(Val == 0, VT);
1708 Tmp1 = (unsigned)Val & 0xFF;
1709 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1711 Tmp1 = (unsigned)Val & 0xFFFF;
1712 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1714 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1716 return getConstant(CountLeadingZeros_64(Val), VT);
1720 default: assert(0 && "Invalid cttz!"); break;
1721 case MVT::i1: return getConstant(Val == 0, VT);
1723 Tmp1 = (unsigned)Val | 0x100;
1724 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1726 Tmp1 = (unsigned)Val | 0x10000;
1727 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1729 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1731 return getConstant(CountTrailingZeros_64(Val), VT);
1736 // Constant fold unary operations with a floating point constant operand.
1737 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1738 APFloat V = C->getValueAPF(); // make copy
1739 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
1743 return getConstantFP(V, VT);
1746 return getConstantFP(V, VT);
1748 case ISD::FP_EXTEND:
1749 // This can return overflow, underflow, or inexact; we don't care.
1750 // FIXME need to be more flexible about rounding mode.
1751 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle :
1752 VT==MVT::f64 ? APFloat::IEEEdouble :
1753 VT==MVT::f80 ? APFloat::x87DoubleExtended :
1754 VT==MVT::f128 ? APFloat::IEEEquad :
1756 APFloat::rmNearestTiesToEven);
1757 return getConstantFP(V, VT);
1758 case ISD::FP_TO_SINT:
1759 case ISD::FP_TO_UINT: {
1761 assert(integerPartWidth >= 64);
1762 // FIXME need to be more flexible about rounding mode.
1763 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1764 Opcode==ISD::FP_TO_SINT,
1765 APFloat::rmTowardZero);
1766 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1768 return getConstant(x, VT);
1770 case ISD::BIT_CONVERT:
1771 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1772 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1773 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1774 return getConstant(V.convertToAPInt().getZExtValue(), VT);
1780 unsigned OpOpcode = Operand.Val->getOpcode();
1782 case ISD::TokenFactor:
1783 return Operand; // Factor of one node? No factor.
1784 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
1785 case ISD::FP_EXTEND:
1786 assert(MVT::isFloatingPoint(VT) &&
1787 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
1788 if (Operand.getValueType() == VT) return Operand; // noop conversion.
1790 case ISD::SIGN_EXTEND:
1791 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1792 "Invalid SIGN_EXTEND!");
1793 if (Operand.getValueType() == VT) return Operand; // noop extension
1794 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1795 && "Invalid sext node, dst < src!");
1796 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1797 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1799 case ISD::ZERO_EXTEND:
1800 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1801 "Invalid ZERO_EXTEND!");
1802 if (Operand.getValueType() == VT) return Operand; // noop extension
1803 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1804 && "Invalid zext node, dst < src!");
1805 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1806 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1808 case ISD::ANY_EXTEND:
1809 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1810 "Invalid ANY_EXTEND!");
1811 if (Operand.getValueType() == VT) return Operand; // noop extension
1812 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1813 && "Invalid anyext node, dst < src!");
1814 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1815 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1816 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1819 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1820 "Invalid TRUNCATE!");
1821 if (Operand.getValueType() == VT) return Operand; // noop truncate
1822 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1823 && "Invalid truncate node, src < dst!");
1824 if (OpOpcode == ISD::TRUNCATE)
1825 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1826 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1827 OpOpcode == ISD::ANY_EXTEND) {
1828 // If the source is smaller than the dest, we still need an extend.
1829 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1830 < MVT::getSizeInBits(VT))
1831 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1832 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1833 > MVT::getSizeInBits(VT))
1834 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1836 return Operand.Val->getOperand(0);
1839 case ISD::BIT_CONVERT:
1840 // Basic sanity checking.
1841 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
1842 && "Cannot BIT_CONVERT between types of different sizes!");
1843 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1844 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1845 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1846 if (OpOpcode == ISD::UNDEF)
1847 return getNode(ISD::UNDEF, VT);
1849 case ISD::SCALAR_TO_VECTOR:
1850 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1851 MVT::getVectorElementType(VT) == Operand.getValueType() &&
1852 "Illegal SCALAR_TO_VECTOR node!");
1855 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1856 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1857 Operand.Val->getOperand(0));
1858 if (OpOpcode == ISD::FNEG) // --X -> X
1859 return Operand.Val->getOperand(0);
1862 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1863 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1868 SDVTList VTs = getVTList(VT);
1869 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1870 FoldingSetNodeID ID;
1871 SDOperand Ops[1] = { Operand };
1872 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
1874 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1875 return SDOperand(E, 0);
1876 N = new UnarySDNode(Opcode, VTs, Operand);
1877 CSEMap.InsertNode(N, IP);
1879 N = new UnarySDNode(Opcode, VTs, Operand);
1881 AllNodes.push_back(N);
1882 return SDOperand(N, 0);
1887 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1888 SDOperand N1, SDOperand N2) {
1889 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1890 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1893 case ISD::TokenFactor:
1894 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1895 N2.getValueType() == MVT::Other && "Invalid token factor!");
1896 // Fold trivial token factors.
1897 if (N1.getOpcode() == ISD::EntryToken) return N2;
1898 if (N2.getOpcode() == ISD::EntryToken) return N1;
1901 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1902 N1.getValueType() == VT && "Binary operator types must match!");
1903 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1904 // worth handling here.
1905 if (N2C && N2C->getValue() == 0)
1907 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
1912 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1913 N1.getValueType() == VT && "Binary operator types must match!");
1914 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1915 // worth handling here.
1916 if (N2C && N2C->getValue() == 0)
1923 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1935 assert(N1.getValueType() == N2.getValueType() &&
1936 N1.getValueType() == VT && "Binary operator types must match!");
1938 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1939 assert(N1.getValueType() == VT &&
1940 MVT::isFloatingPoint(N1.getValueType()) &&
1941 MVT::isFloatingPoint(N2.getValueType()) &&
1942 "Invalid FCOPYSIGN!");
1949 assert(VT == N1.getValueType() &&
1950 "Shift operators return type must be the same as their first arg");
1951 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1952 VT != MVT::i1 && "Shifts only work on integers");
1954 case ISD::FP_ROUND_INREG: {
1955 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1956 assert(VT == N1.getValueType() && "Not an inreg round!");
1957 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1958 "Cannot FP_ROUND_INREG integer types");
1959 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1960 "Not rounding down!");
1961 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1965 assert(MVT::isFloatingPoint(VT) &&
1966 MVT::isFloatingPoint(N1.getValueType()) &&
1967 MVT::getSizeInBits(VT) <= MVT::getSizeInBits(N1.getValueType()) &&
1968 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
1969 if (N1.getValueType() == VT) return N1; // noop conversion.
1971 case ISD::AssertSext:
1972 case ISD::AssertZext: {
1973 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1974 assert(VT == N1.getValueType() && "Not an inreg extend!");
1975 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1976 "Cannot *_EXTEND_INREG FP types");
1977 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1981 case ISD::SIGN_EXTEND_INREG: {
1982 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1983 assert(VT == N1.getValueType() && "Not an inreg extend!");
1984 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1985 "Cannot *_EXTEND_INREG FP types");
1986 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1988 if (EVT == VT) return N1; // Not actually extending
1991 int64_t Val = N1C->getValue();
1992 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1993 Val <<= 64-FromBits;
1994 Val >>= 64-FromBits;
1995 return getConstant(Val, VT);
1999 case ISD::EXTRACT_VECTOR_ELT:
2000 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2002 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2003 // expanding copies of large vectors from registers.
2004 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2005 N1.getNumOperands() > 0) {
2007 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2008 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2009 N1.getOperand(N2C->getValue() / Factor),
2010 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2013 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2014 // expanding large vector constants.
2015 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2016 return N1.getOperand(N2C->getValue());
2018 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2019 // operations are lowered to scalars.
2020 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2021 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2023 return N1.getOperand(1);
2025 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2028 case ISD::EXTRACT_ELEMENT:
2029 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
2031 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2032 // 64-bit integers into 32-bit parts. Instead of building the extract of
2033 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2034 if (N1.getOpcode() == ISD::BUILD_PAIR)
2035 return N1.getOperand(N2C->getValue());
2037 // EXTRACT_ELEMENT of a constant int is also very common.
2038 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2039 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2040 return getConstant(C->getValue() >> Shift, VT);
2047 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
2049 case ISD::ADD: return getConstant(C1 + C2, VT);
2050 case ISD::SUB: return getConstant(C1 - C2, VT);
2051 case ISD::MUL: return getConstant(C1 * C2, VT);
2053 if (C2) return getConstant(C1 / C2, VT);
2056 if (C2) return getConstant(C1 % C2, VT);
2059 if (C2) return getConstant(N1C->getSignExtended() /
2060 N2C->getSignExtended(), VT);
2063 if (C2) return getConstant(N1C->getSignExtended() %
2064 N2C->getSignExtended(), VT);
2066 case ISD::AND : return getConstant(C1 & C2, VT);
2067 case ISD::OR : return getConstant(C1 | C2, VT);
2068 case ISD::XOR : return getConstant(C1 ^ C2, VT);
2069 case ISD::SHL : return getConstant(C1 << C2, VT);
2070 case ISD::SRL : return getConstant(C1 >> C2, VT);
2071 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
2073 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
2076 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
2080 } else { // Cannonicalize constant to RHS if commutative
2081 if (isCommutativeBinOp(Opcode)) {
2082 std::swap(N1C, N2C);
2088 // Constant fold FP operations.
2089 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2090 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
2092 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2093 // Cannonicalize constant to RHS if commutative
2094 std::swap(N1CFP, N2CFP);
2096 } else if (N2CFP && VT != MVT::ppcf128) {
2097 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2098 APFloat::opStatus s;
2101 s = V1.add(V2, APFloat::rmNearestTiesToEven);
2102 if (s != APFloat::opInvalidOp)
2103 return getConstantFP(V1, VT);
2106 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2107 if (s!=APFloat::opInvalidOp)
2108 return getConstantFP(V1, VT);
2111 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2112 if (s!=APFloat::opInvalidOp)
2113 return getConstantFP(V1, VT);
2116 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2117 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2118 return getConstantFP(V1, VT);
2121 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2122 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2123 return getConstantFP(V1, VT);
2125 case ISD::FCOPYSIGN:
2127 return getConstantFP(V1, VT);
2133 // Canonicalize an UNDEF to the RHS, even over a constant.
2134 if (N1.getOpcode() == ISD::UNDEF) {
2135 if (isCommutativeBinOp(Opcode)) {
2139 case ISD::FP_ROUND_INREG:
2140 case ISD::SIGN_EXTEND_INREG:
2146 return N1; // fold op(undef, arg2) -> undef
2153 if (!MVT::isVector(VT))
2154 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2155 // For vectors, we can't easily build an all zero vector, just return
2162 // Fold a bunch of operators when the RHS is undef.
2163 if (N2.getOpcode() == ISD::UNDEF) {
2179 return N2; // fold op(arg1, undef) -> undef
2184 if (!MVT::isVector(VT))
2185 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2186 // For vectors, we can't easily build an all zero vector, just return
2190 if (!MVT::isVector(VT))
2191 return getConstant(MVT::getIntVTBitMask(VT), VT);
2192 // For vectors, we can't easily build an all one vector, just return
2200 // Memoize this node if possible.
2202 SDVTList VTs = getVTList(VT);
2203 if (VT != MVT::Flag) {
2204 SDOperand Ops[] = { N1, N2 };
2205 FoldingSetNodeID ID;
2206 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
2208 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2209 return SDOperand(E, 0);
2210 N = new BinarySDNode(Opcode, VTs, N1, N2);
2211 CSEMap.InsertNode(N, IP);
2213 N = new BinarySDNode(Opcode, VTs, N1, N2);
2216 AllNodes.push_back(N);
2217 return SDOperand(N, 0);
2220 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2221 SDOperand N1, SDOperand N2, SDOperand N3) {
2222 // Perform various simplifications.
2223 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2224 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2227 // Use FoldSetCC to simplify SETCC's.
2228 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
2229 if (Simp.Val) return Simp;
2234 if (N1C->getValue())
2235 return N2; // select true, X, Y -> X
2237 return N3; // select false, X, Y -> Y
2239 if (N2 == N3) return N2; // select C, X, X -> X
2243 if (N2C->getValue()) // Unconditional branch
2244 return getNode(ISD::BR, MVT::Other, N1, N3);
2246 return N1; // Never-taken branch
2248 case ISD::VECTOR_SHUFFLE:
2249 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2250 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2251 N3.getOpcode() == ISD::BUILD_VECTOR &&
2252 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2253 "Illegal VECTOR_SHUFFLE node!");
2255 case ISD::BIT_CONVERT:
2256 // Fold bit_convert nodes from a type to themselves.
2257 if (N1.getValueType() == VT)
2262 // Memoize node if it doesn't produce a flag.
2264 SDVTList VTs = getVTList(VT);
2265 if (VT != MVT::Flag) {
2266 SDOperand Ops[] = { N1, N2, N3 };
2267 FoldingSetNodeID ID;
2268 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2270 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2271 return SDOperand(E, 0);
2272 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2273 CSEMap.InsertNode(N, IP);
2275 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2277 AllNodes.push_back(N);
2278 return SDOperand(N, 0);
2281 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2282 SDOperand N1, SDOperand N2, SDOperand N3,
2284 SDOperand Ops[] = { N1, N2, N3, N4 };
2285 return getNode(Opcode, VT, Ops, 4);
2288 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2289 SDOperand N1, SDOperand N2, SDOperand N3,
2290 SDOperand N4, SDOperand N5) {
2291 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2292 return getNode(Opcode, VT, Ops, 5);
2295 SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dest,
2296 SDOperand Src, SDOperand Size,
2298 SDOperand AlwaysInline) {
2299 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2300 return getNode(ISD::MEMCPY, MVT::Other, Ops, 6);
2303 SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dest,
2304 SDOperand Src, SDOperand Size,
2306 SDOperand AlwaysInline) {
2307 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2308 return getNode(ISD::MEMMOVE, MVT::Other, Ops, 6);
2311 SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dest,
2312 SDOperand Src, SDOperand Size,
2314 SDOperand AlwaysInline) {
2315 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2316 return getNode(ISD::MEMSET, MVT::Other, Ops, 6);
2319 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2320 SDOperand Chain, SDOperand Ptr,
2321 const Value *SV, int SVOffset,
2322 bool isVolatile, unsigned Alignment) {
2323 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2325 if (VT != MVT::iPTR) {
2326 Ty = MVT::getTypeForValueType(VT);
2328 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2329 assert(PT && "Value for load must be a pointer");
2330 Ty = PT->getElementType();
2332 assert(Ty && "Could not get type information for load");
2333 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2335 SDVTList VTs = getVTList(VT, MVT::Other);
2336 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2337 SDOperand Ops[] = { Chain, Ptr, Undef };
2338 FoldingSetNodeID ID;
2339 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2340 ID.AddInteger(ISD::UNINDEXED);
2341 ID.AddInteger(ISD::NON_EXTLOAD);
2342 ID.AddInteger((unsigned int)VT);
2343 ID.AddInteger(Alignment);
2344 ID.AddInteger(isVolatile);
2346 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2347 return SDOperand(E, 0);
2348 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
2349 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2351 CSEMap.InsertNode(N, IP);
2352 AllNodes.push_back(N);
2353 return SDOperand(N, 0);
2356 SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
2357 SDOperand Chain, SDOperand Ptr,
2359 int SVOffset, MVT::ValueType EVT,
2360 bool isVolatile, unsigned Alignment) {
2361 // If they are asking for an extending load from/to the same thing, return a
2364 return getLoad(VT, Chain, Ptr, SV, SVOffset, isVolatile, Alignment);
2366 if (MVT::isVector(VT))
2367 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
2369 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2370 "Should only be an extending load, not truncating!");
2371 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2372 "Cannot sign/zero extend a FP/Vector load!");
2373 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2374 "Cannot convert from FP to Int or Int -> FP!");
2376 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2378 if (VT != MVT::iPTR) {
2379 Ty = MVT::getTypeForValueType(VT);
2381 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2382 assert(PT && "Value for load must be a pointer");
2383 Ty = PT->getElementType();
2385 assert(Ty && "Could not get type information for load");
2386 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2388 SDVTList VTs = getVTList(VT, MVT::Other);
2389 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2390 SDOperand Ops[] = { Chain, Ptr, Undef };
2391 FoldingSetNodeID ID;
2392 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2393 ID.AddInteger(ISD::UNINDEXED);
2394 ID.AddInteger(ExtType);
2395 ID.AddInteger((unsigned int)EVT);
2396 ID.AddInteger(Alignment);
2397 ID.AddInteger(isVolatile);
2399 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2400 return SDOperand(E, 0);
2401 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
2402 SV, SVOffset, Alignment, isVolatile);
2403 CSEMap.InsertNode(N, IP);
2404 AllNodes.push_back(N);
2405 return SDOperand(N, 0);
2409 SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2410 SDOperand Offset, ISD::MemIndexedMode AM) {
2411 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
2412 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2413 "Load is already a indexed load!");
2414 MVT::ValueType VT = OrigLoad.getValueType();
2415 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
2416 SDOperand Ops[] = { LD->getChain(), Base, Offset };
2417 FoldingSetNodeID ID;
2418 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2420 ID.AddInteger(LD->getExtensionType());
2421 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
2422 ID.AddInteger(LD->getAlignment());
2423 ID.AddInteger(LD->isVolatile());
2425 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2426 return SDOperand(E, 0);
2427 SDNode *N = new LoadSDNode(Ops, VTs, AM,
2428 LD->getExtensionType(), LD->getMemoryVT(),
2429 LD->getSrcValue(), LD->getSrcValueOffset(),
2430 LD->getAlignment(), LD->isVolatile());
2431 CSEMap.InsertNode(N, IP);
2432 AllNodes.push_back(N);
2433 return SDOperand(N, 0);
2436 SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
2437 SDOperand Ptr, const Value *SV, int SVOffset,
2438 bool isVolatile, unsigned Alignment) {
2439 MVT::ValueType VT = Val.getValueType();
2441 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2443 if (VT != MVT::iPTR) {
2444 Ty = MVT::getTypeForValueType(VT);
2446 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2447 assert(PT && "Value for store must be a pointer");
2448 Ty = PT->getElementType();
2450 assert(Ty && "Could not get type information for store");
2451 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2453 SDVTList VTs = getVTList(MVT::Other);
2454 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2455 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2456 FoldingSetNodeID ID;
2457 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2458 ID.AddInteger(ISD::UNINDEXED);
2459 ID.AddInteger(false);
2460 ID.AddInteger((unsigned int)VT);
2461 ID.AddInteger(Alignment);
2462 ID.AddInteger(isVolatile);
2464 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2465 return SDOperand(E, 0);
2466 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
2467 VT, SV, SVOffset, Alignment, isVolatile);
2468 CSEMap.InsertNode(N, IP);
2469 AllNodes.push_back(N);
2470 return SDOperand(N, 0);
2473 SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
2474 SDOperand Ptr, const Value *SV,
2475 int SVOffset, MVT::ValueType SVT,
2476 bool isVolatile, unsigned Alignment) {
2477 MVT::ValueType VT = Val.getValueType();
2480 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
2482 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2483 "Not a truncation?");
2484 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2485 "Can't do FP-INT conversion!");
2487 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2489 if (VT != MVT::iPTR) {
2490 Ty = MVT::getTypeForValueType(VT);
2492 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2493 assert(PT && "Value for store must be a pointer");
2494 Ty = PT->getElementType();
2496 assert(Ty && "Could not get type information for store");
2497 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2499 SDVTList VTs = getVTList(MVT::Other);
2500 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2501 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2502 FoldingSetNodeID ID;
2503 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2504 ID.AddInteger(ISD::UNINDEXED);
2506 ID.AddInteger((unsigned int)SVT);
2507 ID.AddInteger(Alignment);
2508 ID.AddInteger(isVolatile);
2510 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2511 return SDOperand(E, 0);
2512 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
2513 SVT, SV, SVOffset, Alignment, isVolatile);
2514 CSEMap.InsertNode(N, IP);
2515 AllNodes.push_back(N);
2516 return SDOperand(N, 0);
2520 SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2521 SDOperand Offset, ISD::MemIndexedMode AM) {
2522 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2523 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2524 "Store is already a indexed store!");
2525 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2526 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2527 FoldingSetNodeID ID;
2528 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2530 ID.AddInteger(ST->isTruncatingStore());
2531 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
2532 ID.AddInteger(ST->getAlignment());
2533 ID.AddInteger(ST->isVolatile());
2535 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2536 return SDOperand(E, 0);
2537 SDNode *N = new StoreSDNode(Ops, VTs, AM,
2538 ST->isTruncatingStore(), ST->getMemoryVT(),
2539 ST->getSrcValue(), ST->getSrcValueOffset(),
2540 ST->getAlignment(), ST->isVolatile());
2541 CSEMap.InsertNode(N, IP);
2542 AllNodes.push_back(N);
2543 return SDOperand(N, 0);
2546 SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2547 SDOperand Chain, SDOperand Ptr,
2549 SDOperand Ops[] = { Chain, Ptr, SV };
2550 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
2553 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2554 const SDOperand *Ops, unsigned NumOps) {
2556 case 0: return getNode(Opcode, VT);
2557 case 1: return getNode(Opcode, VT, Ops[0]);
2558 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2559 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
2565 case ISD::SELECT_CC: {
2566 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
2567 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2568 "LHS and RHS of condition must have same type!");
2569 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2570 "True and False arms of SelectCC must have same type!");
2571 assert(Ops[2].getValueType() == VT &&
2572 "select_cc node must be of same type as true and false value!");
2576 assert(NumOps == 5 && "BR_CC takes 5 operands!");
2577 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2578 "LHS/RHS of comparison should match types!");
2585 SDVTList VTs = getVTList(VT);
2586 if (VT != MVT::Flag) {
2587 FoldingSetNodeID ID;
2588 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
2590 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2591 return SDOperand(E, 0);
2592 N = new SDNode(Opcode, VTs, Ops, NumOps);
2593 CSEMap.InsertNode(N, IP);
2595 N = new SDNode(Opcode, VTs, Ops, NumOps);
2597 AllNodes.push_back(N);
2598 return SDOperand(N, 0);
2601 SDOperand SelectionDAG::getNode(unsigned Opcode,
2602 std::vector<MVT::ValueType> &ResultTys,
2603 const SDOperand *Ops, unsigned NumOps) {
2604 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2608 SDOperand SelectionDAG::getNode(unsigned Opcode,
2609 const MVT::ValueType *VTs, unsigned NumVTs,
2610 const SDOperand *Ops, unsigned NumOps) {
2612 return getNode(Opcode, VTs[0], Ops, NumOps);
2613 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2616 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2617 const SDOperand *Ops, unsigned NumOps) {
2618 if (VTList.NumVTs == 1)
2619 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
2622 // FIXME: figure out how to safely handle things like
2623 // int foo(int x) { return 1 << (x & 255); }
2624 // int bar() { return foo(256); }
2626 case ISD::SRA_PARTS:
2627 case ISD::SRL_PARTS:
2628 case ISD::SHL_PARTS:
2629 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2630 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
2631 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2632 else if (N3.getOpcode() == ISD::AND)
2633 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2634 // If the and is only masking out bits that cannot effect the shift,
2635 // eliminate the and.
2636 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2637 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2638 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2644 // Memoize the node unless it returns a flag.
2646 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
2647 FoldingSetNodeID ID;
2648 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
2650 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2651 return SDOperand(E, 0);
2653 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2654 else if (NumOps == 2)
2655 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2656 else if (NumOps == 3)
2657 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2659 N = new SDNode(Opcode, VTList, Ops, NumOps);
2660 CSEMap.InsertNode(N, IP);
2663 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2664 else if (NumOps == 2)
2665 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2666 else if (NumOps == 3)
2667 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2669 N = new SDNode(Opcode, VTList, Ops, NumOps);
2671 AllNodes.push_back(N);
2672 return SDOperand(N, 0);
2675 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2676 return getNode(Opcode, VTList, 0, 0);
2679 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2681 SDOperand Ops[] = { N1 };
2682 return getNode(Opcode, VTList, Ops, 1);
2685 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2686 SDOperand N1, SDOperand N2) {
2687 SDOperand Ops[] = { N1, N2 };
2688 return getNode(Opcode, VTList, Ops, 2);
2691 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2692 SDOperand N1, SDOperand N2, SDOperand N3) {
2693 SDOperand Ops[] = { N1, N2, N3 };
2694 return getNode(Opcode, VTList, Ops, 3);
2697 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2698 SDOperand N1, SDOperand N2, SDOperand N3,
2700 SDOperand Ops[] = { N1, N2, N3, N4 };
2701 return getNode(Opcode, VTList, Ops, 4);
2704 SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2705 SDOperand N1, SDOperand N2, SDOperand N3,
2706 SDOperand N4, SDOperand N5) {
2707 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2708 return getNode(Opcode, VTList, Ops, 5);
2711 SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
2712 return makeVTList(SDNode::getValueTypeList(VT), 1);
2715 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
2716 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2717 E = VTList.end(); I != E; ++I) {
2718 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
2719 return makeVTList(&(*I)[0], 2);
2721 std::vector<MVT::ValueType> V;
2724 VTList.push_front(V);
2725 return makeVTList(&(*VTList.begin())[0], 2);
2727 SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2728 MVT::ValueType VT3) {
2729 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2730 E = VTList.end(); I != E; ++I) {
2731 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2733 return makeVTList(&(*I)[0], 3);
2735 std::vector<MVT::ValueType> V;
2739 VTList.push_front(V);
2740 return makeVTList(&(*VTList.begin())[0], 3);
2743 SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2745 case 0: assert(0 && "Cannot have nodes without results!");
2746 case 1: return getVTList(VTs[0]);
2747 case 2: return getVTList(VTs[0], VTs[1]);
2748 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2752 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2753 E = VTList.end(); I != E; ++I) {
2754 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2756 bool NoMatch = false;
2757 for (unsigned i = 2; i != NumVTs; ++i)
2758 if (VTs[i] != (*I)[i]) {
2763 return makeVTList(&*I->begin(), NumVTs);
2766 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2767 return makeVTList(&*VTList.begin()->begin(), NumVTs);
2771 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2772 /// specified operands. If the resultant node already exists in the DAG,
2773 /// this does not modify the specified node, instead it returns the node that
2774 /// already exists. If the resultant node does not exist in the DAG, the
2775 /// input node is returned. As a degenerate case, if you specify the same
2776 /// input operands as the node already has, the input node is returned.
2777 SDOperand SelectionDAG::
2778 UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2779 SDNode *N = InN.Val;
2780 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2782 // Check to see if there is no change.
2783 if (Op == N->getOperand(0)) return InN;
2785 // See if the modified node already exists.
2786 void *InsertPos = 0;
2787 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2788 return SDOperand(Existing, InN.ResNo);
2790 // Nope it doesn't. Remove the node from it's current place in the maps.
2792 RemoveNodeFromCSEMaps(N);
2794 // Now we update the operands.
2795 N->OperandList[0].Val->removeUser(N);
2797 N->OperandList[0] = Op;
2799 // If this gets put into a CSE map, add it.
2800 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2804 SDOperand SelectionDAG::
2805 UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2806 SDNode *N = InN.Val;
2807 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2809 // Check to see if there is no change.
2810 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2811 return InN; // No operands changed, just return the input node.
2813 // See if the modified node already exists.
2814 void *InsertPos = 0;
2815 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2816 return SDOperand(Existing, InN.ResNo);
2818 // Nope it doesn't. Remove the node from it's current place in the maps.
2820 RemoveNodeFromCSEMaps(N);
2822 // Now we update the operands.
2823 if (N->OperandList[0] != Op1) {
2824 N->OperandList[0].Val->removeUser(N);
2825 Op1.Val->addUser(N);
2826 N->OperandList[0] = Op1;
2828 if (N->OperandList[1] != Op2) {
2829 N->OperandList[1].Val->removeUser(N);
2830 Op2.Val->addUser(N);
2831 N->OperandList[1] = Op2;
2834 // If this gets put into a CSE map, add it.
2835 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2839 SDOperand SelectionDAG::
2840 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2841 SDOperand Ops[] = { Op1, Op2, Op3 };
2842 return UpdateNodeOperands(N, Ops, 3);
2845 SDOperand SelectionDAG::
2846 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2847 SDOperand Op3, SDOperand Op4) {
2848 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2849 return UpdateNodeOperands(N, Ops, 4);
2852 SDOperand SelectionDAG::
2853 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2854 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2855 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2856 return UpdateNodeOperands(N, Ops, 5);
2860 SDOperand SelectionDAG::
2861 UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
2862 SDNode *N = InN.Val;
2863 assert(N->getNumOperands() == NumOps &&
2864 "Update with wrong number of operands");
2866 // Check to see if there is no change.
2867 bool AnyChange = false;
2868 for (unsigned i = 0; i != NumOps; ++i) {
2869 if (Ops[i] != N->getOperand(i)) {
2875 // No operands changed, just return the input node.
2876 if (!AnyChange) return InN;
2878 // See if the modified node already exists.
2879 void *InsertPos = 0;
2880 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
2881 return SDOperand(Existing, InN.ResNo);
2883 // Nope it doesn't. Remove the node from it's current place in the maps.
2885 RemoveNodeFromCSEMaps(N);
2887 // Now we update the operands.
2888 for (unsigned i = 0; i != NumOps; ++i) {
2889 if (N->OperandList[i] != Ops[i]) {
2890 N->OperandList[i].Val->removeUser(N);
2891 Ops[i].Val->addUser(N);
2892 N->OperandList[i] = Ops[i];
2896 // If this gets put into a CSE map, add it.
2897 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2902 /// MorphNodeTo - This frees the operands of the current node, resets the
2903 /// opcode, types, and operands to the specified value. This should only be
2904 /// used by the SelectionDAG class.
2905 void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2906 const SDOperand *Ops, unsigned NumOps) {
2909 NumValues = L.NumVTs;
2911 // Clear the operands list, updating used nodes to remove this from their
2913 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2914 I->Val->removeUser(this);
2916 // If NumOps is larger than the # of operands we currently have, reallocate
2917 // the operand list.
2918 if (NumOps > NumOperands) {
2919 if (OperandsNeedDelete)
2920 delete [] OperandList;
2921 OperandList = new SDOperand[NumOps];
2922 OperandsNeedDelete = true;
2925 // Assign the new operands.
2926 NumOperands = NumOps;
2928 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2929 OperandList[i] = Ops[i];
2930 SDNode *N = OperandList[i].Val;
2931 N->Uses.push_back(this);
2935 /// SelectNodeTo - These are used for target selectors to *mutate* the
2936 /// specified node to have the specified return type, Target opcode, and
2937 /// operands. Note that target opcodes are stored as
2938 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
2940 /// Note that SelectNodeTo returns the resultant node. If there is already a
2941 /// node of the specified opcode and operands, it returns that node instead of
2942 /// the current one.
2943 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2944 MVT::ValueType VT) {
2945 SDVTList VTs = getVTList(VT);
2946 FoldingSetNodeID ID;
2947 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
2949 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2952 RemoveNodeFromCSEMaps(N);
2954 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
2956 CSEMap.InsertNode(N, IP);
2960 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2961 MVT::ValueType VT, SDOperand Op1) {
2962 // If an identical node already exists, use it.
2963 SDVTList VTs = getVTList(VT);
2964 SDOperand Ops[] = { Op1 };
2966 FoldingSetNodeID ID;
2967 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
2969 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2972 RemoveNodeFromCSEMaps(N);
2973 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
2974 CSEMap.InsertNode(N, IP);
2978 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2979 MVT::ValueType VT, SDOperand Op1,
2981 // If an identical node already exists, use it.
2982 SDVTList VTs = getVTList(VT);
2983 SDOperand Ops[] = { Op1, Op2 };
2985 FoldingSetNodeID ID;
2986 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2988 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2991 RemoveNodeFromCSEMaps(N);
2993 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2995 CSEMap.InsertNode(N, IP); // Memoize the new node.
2999 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3000 MVT::ValueType VT, SDOperand Op1,
3001 SDOperand Op2, SDOperand Op3) {
3002 // If an identical node already exists, use it.
3003 SDVTList VTs = getVTList(VT);
3004 SDOperand Ops[] = { Op1, Op2, Op3 };
3005 FoldingSetNodeID ID;
3006 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3008 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3011 RemoveNodeFromCSEMaps(N);
3013 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3015 CSEMap.InsertNode(N, IP); // Memoize the new node.
3019 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3020 MVT::ValueType VT, const SDOperand *Ops,
3022 // If an identical node already exists, use it.
3023 SDVTList VTs = getVTList(VT);
3024 FoldingSetNodeID ID;
3025 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
3027 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3030 RemoveNodeFromCSEMaps(N);
3031 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
3033 CSEMap.InsertNode(N, IP); // Memoize the new node.
3037 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3038 MVT::ValueType VT1, MVT::ValueType VT2,
3039 SDOperand Op1, SDOperand Op2) {
3040 SDVTList VTs = getVTList(VT1, VT2);
3041 FoldingSetNodeID ID;
3042 SDOperand Ops[] = { Op1, Op2 };
3043 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3045 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3048 RemoveNodeFromCSEMaps(N);
3049 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3050 CSEMap.InsertNode(N, IP); // Memoize the new node.
3054 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3055 MVT::ValueType VT1, MVT::ValueType VT2,
3056 SDOperand Op1, SDOperand Op2,
3058 // If an identical node already exists, use it.
3059 SDVTList VTs = getVTList(VT1, VT2);
3060 SDOperand Ops[] = { Op1, Op2, Op3 };
3061 FoldingSetNodeID ID;
3062 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3064 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3067 RemoveNodeFromCSEMaps(N);
3069 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3070 CSEMap.InsertNode(N, IP); // Memoize the new node.
3075 /// getTargetNode - These are used for target selectors to create a new node
3076 /// with specified return type(s), target opcode, and operands.
3078 /// Note that getTargetNode returns the resultant node. If there is already a
3079 /// node of the specified opcode and operands, it returns that node instead of
3080 /// the current one.
3081 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3082 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3084 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3086 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3088 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3089 SDOperand Op1, SDOperand Op2) {
3090 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3092 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3093 SDOperand Op1, SDOperand Op2,
3095 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3097 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3098 const SDOperand *Ops, unsigned NumOps) {
3099 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
3101 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3102 MVT::ValueType VT2) {
3103 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3105 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3107 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3108 MVT::ValueType VT2, SDOperand Op1) {
3109 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3110 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
3112 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3113 MVT::ValueType VT2, SDOperand Op1,
3115 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3116 SDOperand Ops[] = { Op1, Op2 };
3117 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
3119 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3120 MVT::ValueType VT2, SDOperand Op1,
3121 SDOperand Op2, SDOperand Op3) {
3122 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3123 SDOperand Ops[] = { Op1, Op2, Op3 };
3124 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
3126 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3128 const SDOperand *Ops, unsigned NumOps) {
3129 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3130 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
3132 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3133 MVT::ValueType VT2, MVT::ValueType VT3,
3134 SDOperand Op1, SDOperand Op2) {
3135 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3136 SDOperand Ops[] = { Op1, Op2 };
3137 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
3139 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3140 MVT::ValueType VT2, MVT::ValueType VT3,
3141 SDOperand Op1, SDOperand Op2,
3143 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3144 SDOperand Ops[] = { Op1, Op2, Op3 };
3145 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3147 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3148 MVT::ValueType VT2, MVT::ValueType VT3,
3149 const SDOperand *Ops, unsigned NumOps) {
3150 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3151 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
3153 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3154 MVT::ValueType VT2, MVT::ValueType VT3,
3156 const SDOperand *Ops, unsigned NumOps) {
3157 std::vector<MVT::ValueType> VTList;
3158 VTList.push_back(VT1);
3159 VTList.push_back(VT2);
3160 VTList.push_back(VT3);
3161 VTList.push_back(VT4);
3162 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3163 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3165 SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3166 std::vector<MVT::ValueType> &ResultTys,
3167 const SDOperand *Ops, unsigned NumOps) {
3168 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3169 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3173 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3174 /// This can cause recursive merging of nodes in the DAG.
3176 /// This version assumes From/To have a single result value.
3178 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
3179 std::vector<SDNode*> *Deleted) {
3180 SDNode *From = FromN.Val, *To = ToN.Val;
3181 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
3182 "Cannot replace with this method!");
3183 assert(From != To && "Cannot replace uses of with self");
3185 while (!From->use_empty()) {
3186 // Process users until they are all gone.
3187 SDNode *U = *From->use_begin();
3189 // This node is about to morph, remove its old self from the CSE maps.
3190 RemoveNodeFromCSEMaps(U);
3192 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3194 if (I->Val == From) {
3195 From->removeUser(U);
3200 // Now that we have modified U, add it back to the CSE maps. If it already
3201 // exists there, recursively merge the results together.
3202 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3203 ReplaceAllUsesWith(U, Existing, Deleted);
3205 if (Deleted) Deleted->push_back(U);
3206 DeleteNodeNotInCSEMaps(U);
3211 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3212 /// This can cause recursive merging of nodes in the DAG.
3214 /// This version assumes From/To have matching types and numbers of result
3217 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3218 std::vector<SDNode*> *Deleted) {
3219 assert(From != To && "Cannot replace uses of with self");
3220 assert(From->getNumValues() == To->getNumValues() &&
3221 "Cannot use this version of ReplaceAllUsesWith!");
3222 if (From->getNumValues() == 1) { // If possible, use the faster version.
3223 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
3227 while (!From->use_empty()) {
3228 // Process users until they are all gone.
3229 SDNode *U = *From->use_begin();
3231 // This node is about to morph, remove its old self from the CSE maps.
3232 RemoveNodeFromCSEMaps(U);
3234 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3236 if (I->Val == From) {
3237 From->removeUser(U);
3242 // Now that we have modified U, add it back to the CSE maps. If it already
3243 // exists there, recursively merge the results together.
3244 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3245 ReplaceAllUsesWith(U, Existing, Deleted);
3247 if (Deleted) Deleted->push_back(U);
3248 DeleteNodeNotInCSEMaps(U);
3253 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3254 /// This can cause recursive merging of nodes in the DAG.
3256 /// This version can replace From with any result values. To must match the
3257 /// number and types of values returned by From.
3258 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
3259 const SDOperand *To,
3260 std::vector<SDNode*> *Deleted) {
3261 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
3262 // Degenerate case handled above.
3263 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
3267 while (!From->use_empty()) {
3268 // Process users until they are all gone.
3269 SDNode *U = *From->use_begin();
3271 // This node is about to morph, remove its old self from the CSE maps.
3272 RemoveNodeFromCSEMaps(U);
3274 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3276 if (I->Val == From) {
3277 const SDOperand &ToOp = To[I->ResNo];
3278 From->removeUser(U);
3280 ToOp.Val->addUser(U);
3283 // Now that we have modified U, add it back to the CSE maps. If it already
3284 // exists there, recursively merge the results together.
3285 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3286 ReplaceAllUsesWith(U, Existing, Deleted);
3288 if (Deleted) Deleted->push_back(U);
3289 DeleteNodeNotInCSEMaps(U);
3294 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3295 /// uses of other values produced by From.Val alone. The Deleted vector is
3296 /// handled the same was as for ReplaceAllUsesWith.
3297 void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
3298 std::vector<SDNode*> *Deleted) {
3299 assert(From != To && "Cannot replace a value with itself");
3300 // Handle the simple, trivial, case efficiently.
3301 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
3302 ReplaceAllUsesWith(From, To, Deleted);
3306 // Get all of the users of From.Val. We want these in a nice,
3307 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3308 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
3310 std::vector<SDNode*> LocalDeletionVector;
3312 // Pick a deletion vector to use. If the user specified one, use theirs,
3313 // otherwise use a local one.
3314 std::vector<SDNode*> *DeleteVector = Deleted ? Deleted : &LocalDeletionVector;
3315 while (!Users.empty()) {
3316 // We know that this user uses some value of From. If it is the right
3317 // value, update it.
3318 SDNode *User = Users.back();
3321 // Scan for an operand that matches From.
3322 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3323 for (; Op != E; ++Op)
3324 if (*Op == From) break;
3326 // If there are no matches, the user must use some other result of From.
3327 if (Op == E) continue;
3329 // Okay, we know this user needs to be updated. Remove its old self
3330 // from the CSE maps.
3331 RemoveNodeFromCSEMaps(User);
3333 // Update all operands that match "From".
3334 for (; Op != E; ++Op) {
3336 From.Val->removeUser(User);
3338 To.Val->addUser(User);
3342 // Now that we have modified User, add it back to the CSE maps. If it
3343 // already exists there, recursively merge the results together.
3344 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
3345 if (!Existing) continue; // Continue on to next user.
3347 // If there was already an existing matching node, use ReplaceAllUsesWith
3348 // to replace the dead one with the existing one. However, this can cause
3349 // recursive merging of other unrelated nodes down the line. The merging
3350 // can cause deletion of nodes that used the old value. In this case,
3351 // we have to be certain to remove them from the Users set.
3352 unsigned NumDeleted = DeleteVector->size();
3353 ReplaceAllUsesWith(User, Existing, DeleteVector);
3355 // User is now dead.
3356 DeleteVector->push_back(User);
3357 DeleteNodeNotInCSEMaps(User);
3359 // We have to be careful here, because ReplaceAllUsesWith could have
3360 // deleted a user of From, which means there may be dangling pointers
3361 // in the "Users" setvector. Scan over the deleted node pointers and
3362 // remove them from the setvector.
3363 for (unsigned i = NumDeleted, e = DeleteVector->size(); i != e; ++i)
3364 Users.remove((*DeleteVector)[i]);
3366 // If the user doesn't need the set of deleted elements, don't retain them
3367 // to the next loop iteration.
3369 LocalDeletionVector.clear();
3374 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3375 /// their allnodes order. It returns the maximum id.
3376 unsigned SelectionDAG::AssignNodeIds() {
3378 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3385 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
3386 /// based on their topological order. It returns the maximum id and a vector
3387 /// of the SDNodes* in assigned order by reference.
3388 unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
3389 unsigned DAGSize = AllNodes.size();
3390 std::vector<unsigned> InDegree(DAGSize);
3391 std::vector<SDNode*> Sources;
3393 // Use a two pass approach to avoid using a std::map which is slow.
3395 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3398 unsigned Degree = N->use_size();
3399 InDegree[N->getNodeId()] = Degree;
3401 Sources.push_back(N);
3405 while (!Sources.empty()) {
3406 SDNode *N = Sources.back();
3408 TopOrder.push_back(N);
3409 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3411 unsigned Degree = --InDegree[P->getNodeId()];
3413 Sources.push_back(P);
3417 // Second pass, assign the actual topological order as node ids.
3419 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3421 (*TI)->setNodeId(Id++);
3428 //===----------------------------------------------------------------------===//
3430 //===----------------------------------------------------------------------===//
3432 // Out-of-line virtual method to give class a home.
3433 void SDNode::ANCHOR() {}
3434 void UnarySDNode::ANCHOR() {}
3435 void BinarySDNode::ANCHOR() {}
3436 void TernarySDNode::ANCHOR() {}
3437 void HandleSDNode::ANCHOR() {}
3438 void StringSDNode::ANCHOR() {}
3439 void ConstantSDNode::ANCHOR() {}
3440 void ConstantFPSDNode::ANCHOR() {}
3441 void GlobalAddressSDNode::ANCHOR() {}
3442 void FrameIndexSDNode::ANCHOR() {}
3443 void JumpTableSDNode::ANCHOR() {}
3444 void ConstantPoolSDNode::ANCHOR() {}
3445 void BasicBlockSDNode::ANCHOR() {}
3446 void SrcValueSDNode::ANCHOR() {}
3447 void RegisterSDNode::ANCHOR() {}
3448 void ExternalSymbolSDNode::ANCHOR() {}
3449 void CondCodeSDNode::ANCHOR() {}
3450 void VTSDNode::ANCHOR() {}
3451 void LoadSDNode::ANCHOR() {}
3452 void StoreSDNode::ANCHOR() {}
3454 HandleSDNode::~HandleSDNode() {
3455 SDVTList VTs = { 0, 0 };
3456 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
3459 GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3460 MVT::ValueType VT, int o)
3461 : SDNode(isa<GlobalVariable>(GA) &&
3462 cast<GlobalVariable>(GA)->isThreadLocal() ?
3464 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3466 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3467 getSDVTList(VT)), Offset(o) {
3468 TheGlobal = const_cast<GlobalValue*>(GA);
3471 /// Profile - Gather unique data for the node.
3473 void SDNode::Profile(FoldingSetNodeID &ID) {
3474 AddNodeIDNode(ID, this);
3477 /// getValueTypeList - Return a pointer to the specified value type.
3479 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
3480 if (MVT::isExtendedVT(VT)) {
3481 static std::set<MVT::ValueType> EVTs;
3482 return (MVT::ValueType *)&(*EVTs.insert(VT).first);
3484 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3490 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3491 /// indicated value. This method ignores uses of other values defined by this
3493 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
3494 assert(Value < getNumValues() && "Bad value!");
3496 // If there is only one value, this is easy.
3497 if (getNumValues() == 1)
3498 return use_size() == NUses;
3499 if (use_size() < NUses) return false;
3501 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3503 SmallPtrSet<SDNode*, 32> UsersHandled;
3505 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3507 if (User->getNumOperands() == 1 ||
3508 UsersHandled.insert(User)) // First time we've seen this?
3509 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3510 if (User->getOperand(i) == TheValue) {
3512 return false; // too many uses
3517 // Found exactly the right number of uses?
3522 /// hasAnyUseOfValue - Return true if there are any use of the indicated
3523 /// value. This method ignores uses of other values defined by this operation.
3524 bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3525 assert(Value < getNumValues() && "Bad value!");
3527 if (use_empty()) return false;
3529 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3531 SmallPtrSet<SDNode*, 32> UsersHandled;
3533 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3535 if (User->getNumOperands() == 1 ||
3536 UsersHandled.insert(User)) // First time we've seen this?
3537 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3538 if (User->getOperand(i) == TheValue) {
3547 /// isOnlyUse - Return true if this node is the only use of N.
3549 bool SDNode::isOnlyUse(SDNode *N) const {
3551 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3562 /// isOperand - Return true if this node is an operand of N.
3564 bool SDOperand::isOperand(SDNode *N) const {
3565 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3566 if (*this == N->getOperand(i))
3571 bool SDNode::isOperand(SDNode *N) const {
3572 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3573 if (this == N->OperandList[i].Val)
3578 /// reachesChainWithoutSideEffects - Return true if this operand (which must
3579 /// be a chain) reaches the specified operand without crossing any
3580 /// side-effecting instructions. In practice, this looks through token
3581 /// factors and non-volatile loads. In order to remain efficient, this only
3582 /// looks a couple of nodes in, it does not do an exhaustive search.
3583 bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
3584 unsigned Depth) const {
3585 if (*this == Dest) return true;
3587 // Don't search too deeply, we just want to be able to see through
3588 // TokenFactor's etc.
3589 if (Depth == 0) return false;
3591 // If this is a token factor, all inputs to the TF happen in parallel. If any
3592 // of the operands of the TF reach dest, then we can do the xform.
3593 if (getOpcode() == ISD::TokenFactor) {
3594 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
3595 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
3600 // Loads don't have side effects, look through them.
3601 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
3602 if (!Ld->isVolatile())
3603 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
3609 static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
3610 SmallPtrSet<SDNode *, 32> &Visited) {
3611 if (found || !Visited.insert(N))
3614 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3615 SDNode *Op = N->getOperand(i).Val;
3620 findPredecessor(Op, P, found, Visited);
3624 /// isPredecessor - Return true if this node is a predecessor of N. This node
3625 /// is either an operand of N or it can be reached by recursively traversing
3626 /// up the operands.
3627 /// NOTE: this is an expensive method. Use it carefully.
3628 bool SDNode::isPredecessor(SDNode *N) const {
3629 SmallPtrSet<SDNode *, 32> Visited;
3631 findPredecessor(N, this, found, Visited);
3635 uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3636 assert(Num < NumOperands && "Invalid child # of SDNode!");
3637 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3640 std::string SDNode::getOperationName(const SelectionDAG *G) const {
3641 switch (getOpcode()) {
3643 if (getOpcode() < ISD::BUILTIN_OP_END)
3644 return "<<Unknown DAG Node>>";
3647 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
3648 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
3649 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
3651 TargetLowering &TLI = G->getTargetLoweringInfo();
3653 TLI.getTargetNodeName(getOpcode());
3654 if (Name) return Name;
3657 return "<<Unknown Target Node>>";
3660 case ISD::PCMARKER: return "PCMarker";
3661 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
3662 case ISD::SRCVALUE: return "SrcValue";
3663 case ISD::EntryToken: return "EntryToken";
3664 case ISD::TokenFactor: return "TokenFactor";
3665 case ISD::AssertSext: return "AssertSext";
3666 case ISD::AssertZext: return "AssertZext";
3668 case ISD::STRING: return "String";
3669 case ISD::BasicBlock: return "BasicBlock";
3670 case ISD::VALUETYPE: return "ValueType";
3671 case ISD::Register: return "Register";
3673 case ISD::Constant: return "Constant";
3674 case ISD::ConstantFP: return "ConstantFP";
3675 case ISD::GlobalAddress: return "GlobalAddress";
3676 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
3677 case ISD::FrameIndex: return "FrameIndex";
3678 case ISD::JumpTable: return "JumpTable";
3679 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
3680 case ISD::RETURNADDR: return "RETURNADDR";
3681 case ISD::FRAMEADDR: return "FRAMEADDR";
3682 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
3683 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3684 case ISD::EHSELECTION: return "EHSELECTION";
3685 case ISD::EH_RETURN: return "EH_RETURN";
3686 case ISD::ConstantPool: return "ConstantPool";
3687 case ISD::ExternalSymbol: return "ExternalSymbol";
3688 case ISD::INTRINSIC_WO_CHAIN: {
3689 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3690 return Intrinsic::getName((Intrinsic::ID)IID);
3692 case ISD::INTRINSIC_VOID:
3693 case ISD::INTRINSIC_W_CHAIN: {
3694 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
3695 return Intrinsic::getName((Intrinsic::ID)IID);
3698 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
3699 case ISD::TargetConstant: return "TargetConstant";
3700 case ISD::TargetConstantFP:return "TargetConstantFP";
3701 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
3702 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
3703 case ISD::TargetFrameIndex: return "TargetFrameIndex";
3704 case ISD::TargetJumpTable: return "TargetJumpTable";
3705 case ISD::TargetConstantPool: return "TargetConstantPool";
3706 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
3708 case ISD::CopyToReg: return "CopyToReg";
3709 case ISD::CopyFromReg: return "CopyFromReg";
3710 case ISD::UNDEF: return "undef";
3711 case ISD::MERGE_VALUES: return "merge_values";
3712 case ISD::INLINEASM: return "inlineasm";
3713 case ISD::LABEL: return "label";
3714 case ISD::DECLARE: return "declare";
3715 case ISD::HANDLENODE: return "handlenode";
3716 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
3717 case ISD::CALL: return "call";
3720 case ISD::FABS: return "fabs";
3721 case ISD::FNEG: return "fneg";
3722 case ISD::FSQRT: return "fsqrt";
3723 case ISD::FSIN: return "fsin";
3724 case ISD::FCOS: return "fcos";
3725 case ISD::FPOWI: return "fpowi";
3726 case ISD::FPOW: return "fpow";
3729 case ISD::ADD: return "add";
3730 case ISD::SUB: return "sub";
3731 case ISD::MUL: return "mul";
3732 case ISD::MULHU: return "mulhu";
3733 case ISD::MULHS: return "mulhs";
3734 case ISD::SDIV: return "sdiv";
3735 case ISD::UDIV: return "udiv";
3736 case ISD::SREM: return "srem";
3737 case ISD::UREM: return "urem";
3738 case ISD::SMUL_LOHI: return "smul_lohi";
3739 case ISD::UMUL_LOHI: return "umul_lohi";
3740 case ISD::SDIVREM: return "sdivrem";
3741 case ISD::UDIVREM: return "divrem";
3742 case ISD::AND: return "and";
3743 case ISD::OR: return "or";
3744 case ISD::XOR: return "xor";
3745 case ISD::SHL: return "shl";
3746 case ISD::SRA: return "sra";
3747 case ISD::SRL: return "srl";
3748 case ISD::ROTL: return "rotl";
3749 case ISD::ROTR: return "rotr";
3750 case ISD::FADD: return "fadd";
3751 case ISD::FSUB: return "fsub";
3752 case ISD::FMUL: return "fmul";
3753 case ISD::FDIV: return "fdiv";
3754 case ISD::FREM: return "frem";
3755 case ISD::FCOPYSIGN: return "fcopysign";
3756 case ISD::FGETSIGN: return "fgetsign";
3758 case ISD::SETCC: return "setcc";
3759 case ISD::SELECT: return "select";
3760 case ISD::SELECT_CC: return "select_cc";
3761 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
3762 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
3763 case ISD::CONCAT_VECTORS: return "concat_vectors";
3764 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
3765 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
3766 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
3767 case ISD::CARRY_FALSE: return "carry_false";
3768 case ISD::ADDC: return "addc";
3769 case ISD::ADDE: return "adde";
3770 case ISD::SUBC: return "subc";
3771 case ISD::SUBE: return "sube";
3772 case ISD::SHL_PARTS: return "shl_parts";
3773 case ISD::SRA_PARTS: return "sra_parts";
3774 case ISD::SRL_PARTS: return "srl_parts";
3776 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3777 case ISD::INSERT_SUBREG: return "insert_subreg";
3779 // Conversion operators.
3780 case ISD::SIGN_EXTEND: return "sign_extend";
3781 case ISD::ZERO_EXTEND: return "zero_extend";
3782 case ISD::ANY_EXTEND: return "any_extend";
3783 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
3784 case ISD::TRUNCATE: return "truncate";
3785 case ISD::FP_ROUND: return "fp_round";
3786 case ISD::FLT_ROUNDS_: return "flt_rounds";
3787 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
3788 case ISD::FP_EXTEND: return "fp_extend";
3790 case ISD::SINT_TO_FP: return "sint_to_fp";
3791 case ISD::UINT_TO_FP: return "uint_to_fp";
3792 case ISD::FP_TO_SINT: return "fp_to_sint";
3793 case ISD::FP_TO_UINT: return "fp_to_uint";
3794 case ISD::BIT_CONVERT: return "bit_convert";
3796 // Control flow instructions
3797 case ISD::BR: return "br";
3798 case ISD::BRIND: return "brind";
3799 case ISD::BR_JT: return "br_jt";
3800 case ISD::BRCOND: return "brcond";
3801 case ISD::BR_CC: return "br_cc";
3802 case ISD::RET: return "ret";
3803 case ISD::CALLSEQ_START: return "callseq_start";
3804 case ISD::CALLSEQ_END: return "callseq_end";
3807 case ISD::LOAD: return "load";
3808 case ISD::STORE: return "store";
3809 case ISD::VAARG: return "vaarg";
3810 case ISD::VACOPY: return "vacopy";
3811 case ISD::VAEND: return "vaend";
3812 case ISD::VASTART: return "vastart";
3813 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
3814 case ISD::EXTRACT_ELEMENT: return "extract_element";
3815 case ISD::BUILD_PAIR: return "build_pair";
3816 case ISD::STACKSAVE: return "stacksave";
3817 case ISD::STACKRESTORE: return "stackrestore";
3818 case ISD::TRAP: return "trap";
3820 // Block memory operations.
3821 case ISD::MEMSET: return "memset";
3822 case ISD::MEMCPY: return "memcpy";
3823 case ISD::MEMMOVE: return "memmove";
3826 case ISD::BSWAP: return "bswap";
3827 case ISD::CTPOP: return "ctpop";
3828 case ISD::CTTZ: return "cttz";
3829 case ISD::CTLZ: return "ctlz";
3832 case ISD::LOCATION: return "location";
3833 case ISD::DEBUG_LOC: return "debug_loc";
3836 case ISD::TRAMPOLINE: return "trampoline";
3839 switch (cast<CondCodeSDNode>(this)->get()) {
3840 default: assert(0 && "Unknown setcc condition!");
3841 case ISD::SETOEQ: return "setoeq";
3842 case ISD::SETOGT: return "setogt";
3843 case ISD::SETOGE: return "setoge";
3844 case ISD::SETOLT: return "setolt";
3845 case ISD::SETOLE: return "setole";
3846 case ISD::SETONE: return "setone";
3848 case ISD::SETO: return "seto";
3849 case ISD::SETUO: return "setuo";
3850 case ISD::SETUEQ: return "setue";
3851 case ISD::SETUGT: return "setugt";
3852 case ISD::SETUGE: return "setuge";
3853 case ISD::SETULT: return "setult";
3854 case ISD::SETULE: return "setule";
3855 case ISD::SETUNE: return "setune";
3857 case ISD::SETEQ: return "seteq";
3858 case ISD::SETGT: return "setgt";
3859 case ISD::SETGE: return "setge";
3860 case ISD::SETLT: return "setlt";
3861 case ISD::SETLE: return "setle";
3862 case ISD::SETNE: return "setne";
3867 const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
3876 return "<post-inc>";
3878 return "<post-dec>";
3882 void SDNode::dump() const { dump(0); }
3883 void SDNode::dump(const SelectionDAG *G) const {
3884 cerr << (void*)this << ": ";
3886 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
3888 if (getValueType(i) == MVT::Other)
3891 cerr << MVT::getValueTypeString(getValueType(i));
3893 cerr << " = " << getOperationName(G);
3896 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
3897 if (i) cerr << ", ";
3898 cerr << (void*)getOperand(i).Val;
3899 if (unsigned RN = getOperand(i).ResNo)
3903 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
3904 SDNode *Mask = getOperand(2).Val;
3906 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
3908 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
3911 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
3916 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
3917 cerr << "<" << CSDN->getValue() << ">";
3918 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
3919 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
3920 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
3921 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
3922 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
3924 cerr << "<APFloat(";
3925 CSDN->getValueAPF().convertToAPInt().dump();
3928 } else if (const GlobalAddressSDNode *GADN =
3929 dyn_cast<GlobalAddressSDNode>(this)) {
3930 int offset = GADN->getOffset();
3932 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
3934 cerr << " + " << offset;
3936 cerr << " " << offset;
3937 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
3938 cerr << "<" << FIDN->getIndex() << ">";
3939 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
3940 cerr << "<" << JTDN->getIndex() << ">";
3941 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
3942 int offset = CP->getOffset();
3943 if (CP->isMachineConstantPoolEntry())
3944 cerr << "<" << *CP->getMachineCPVal() << ">";
3946 cerr << "<" << *CP->getConstVal() << ">";
3948 cerr << " + " << offset;
3950 cerr << " " << offset;
3951 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
3953 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3955 cerr << LBB->getName() << " ";
3956 cerr << (const void*)BBDN->getBasicBlock() << ">";
3957 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
3958 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
3959 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
3961 cerr << " #" << R->getReg();
3963 } else if (const ExternalSymbolSDNode *ES =
3964 dyn_cast<ExternalSymbolSDNode>(this)) {
3965 cerr << "'" << ES->getSymbol() << "'";
3966 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3968 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
3970 cerr << "<null:" << M->getOffset() << ">";
3971 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
3972 cerr << ":" << MVT::getValueTypeString(N->getVT());
3973 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
3974 const Value *SrcValue = LD->getSrcValue();
3975 int SrcOffset = LD->getSrcValueOffset();
3981 cerr << ":" << SrcOffset << ">";
3984 switch (LD->getExtensionType()) {
3985 default: doExt = false; break;
3987 cerr << " <anyext ";
3997 cerr << MVT::getValueTypeString(LD->getMemoryVT()) << ">";
3999 const char *AM = getIndexedModeName(LD->getAddressingMode());
4002 if (LD->isVolatile())
4003 cerr << " <volatile>";
4004 cerr << " alignment=" << LD->getAlignment();
4005 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
4006 const Value *SrcValue = ST->getSrcValue();
4007 int SrcOffset = ST->getSrcValueOffset();
4013 cerr << ":" << SrcOffset << ">";
4015 if (ST->isTruncatingStore())
4017 << MVT::getValueTypeString(ST->getMemoryVT()) << ">";
4019 const char *AM = getIndexedModeName(ST->getAddressingMode());
4022 if (ST->isVolatile())
4023 cerr << " <volatile>";
4024 cerr << " alignment=" << ST->getAlignment();
4028 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
4029 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4030 if (N->getOperand(i).Val->hasOneUse())
4031 DumpNodes(N->getOperand(i).Val, indent+2, G);
4033 cerr << "\n" << std::string(indent+2, ' ')
4034 << (void*)N->getOperand(i).Val << ": <multiple use>";
4037 cerr << "\n" << std::string(indent, ' ');
4041 void SelectionDAG::dump() const {
4042 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
4043 std::vector<const SDNode*> Nodes;
4044 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4048 std::sort(Nodes.begin(), Nodes.end());
4050 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
4051 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
4052 DumpNodes(Nodes[i], 2, this);
4055 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
4060 const Type *ConstantPoolSDNode::getType() const {
4061 if (isMachineConstantPoolEntry())
4062 return Val.MachineCPVal->getType();
4063 return Val.ConstVal->getType();