Reverting back to original 1.8 version so I can manually merge in patch.
[llvm-complete.git] / lib / CodeGen / SelectionDAG / SelectionDAG.cpp
blobc6e20574456277a875e0db6df79fa3d78c8f2740
1 //===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This implements the SelectionDAG class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/SelectionDAG.h"
15 #include "llvm/Constants.h"
16 #include "llvm/GlobalValue.h"
17 #include "llvm/Intrinsics.h"
18 #include "llvm/Assembly/Writer.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/Support/MathExtras.h"
21 #include "llvm/Target/MRegisterInfo.h"
22 #include "llvm/Target/TargetLowering.h"
23 #include "llvm/Target/TargetInstrInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/ADT/SetVector.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include <iostream>
29 #include <set>
30 #include <cmath>
31 #include <algorithm>
32 using namespace llvm;
34 static bool isCommutativeBinOp(unsigned Opcode) {
35 switch (Opcode) {
36 case ISD::ADD:
37 case ISD::MUL:
38 case ISD::MULHU:
39 case ISD::MULHS:
40 case ISD::FADD:
41 case ISD::FMUL:
42 case ISD::AND:
43 case ISD::OR:
44 case ISD::XOR: return true;
45 default: return false; // FIXME: Need commutative info for user ops!
49 // isInvertibleForFree - Return true if there is no cost to emitting the logical
50 // inverse of this node.
51 static bool isInvertibleForFree(SDOperand N) {
52 if (isa<ConstantSDNode>(N.Val)) return true;
53 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
54 return true;
55 return false;
58 //===----------------------------------------------------------------------===//
59 // ConstantFPSDNode Class
60 //===----------------------------------------------------------------------===//
62 /// isExactlyValue - We don't rely on operator== working on double values, as
63 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
64 /// As such, this method can be used to do an exact bit-for-bit comparison of
65 /// two floating point values.
66 bool ConstantFPSDNode::isExactlyValue(double V) const {
67 return DoubleToBits(V) == DoubleToBits(Value);
70 //===----------------------------------------------------------------------===//
71 // ISD Namespace
72 //===----------------------------------------------------------------------===//
74 /// isBuildVectorAllOnes - Return true if the specified node is a
75 /// BUILD_VECTOR where all of the elements are ~0 or undef.
76 bool ISD::isBuildVectorAllOnes(const SDNode *N) {
77 // Look through a bit convert.
78 if (N->getOpcode() == ISD::BIT_CONVERT)
79 N = N->getOperand(0).Val;
81 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
83 unsigned i = 0, e = N->getNumOperands();
85 // Skip over all of the undef values.
86 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
87 ++i;
89 // Do not accept an all-undef vector.
90 if (i == e) return false;
92 // Do not accept build_vectors that aren't all constants or which have non-~0
93 // elements.
94 SDOperand NotZero = N->getOperand(i);
95 if (isa<ConstantSDNode>(NotZero)) {
96 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
97 return false;
98 } else if (isa<ConstantFPSDNode>(NotZero)) {
99 MVT::ValueType VT = NotZero.getValueType();
100 if (VT== MVT::f64) {
101 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
102 (uint64_t)-1)
103 return false;
104 } else {
105 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
106 (uint32_t)-1)
107 return false;
109 } else
110 return false;
112 // Okay, we have at least one ~0 value, check to see if the rest match or are
113 // undefs.
114 for (++i; i != e; ++i)
115 if (N->getOperand(i) != NotZero &&
116 N->getOperand(i).getOpcode() != ISD::UNDEF)
117 return false;
118 return true;
122 /// isBuildVectorAllZeros - Return true if the specified node is a
123 /// BUILD_VECTOR where all of the elements are 0 or undef.
124 bool ISD::isBuildVectorAllZeros(const SDNode *N) {
125 // Look through a bit convert.
126 if (N->getOpcode() == ISD::BIT_CONVERT)
127 N = N->getOperand(0).Val;
129 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
131 unsigned i = 0, e = N->getNumOperands();
133 // Skip over all of the undef values.
134 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
135 ++i;
137 // Do not accept an all-undef vector.
138 if (i == e) return false;
140 // Do not accept build_vectors that aren't all constants or which have non-~0
141 // elements.
142 SDOperand Zero = N->getOperand(i);
143 if (isa<ConstantSDNode>(Zero)) {
144 if (!cast<ConstantSDNode>(Zero)->isNullValue())
145 return false;
146 } else if (isa<ConstantFPSDNode>(Zero)) {
147 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
148 return false;
149 } else
150 return false;
152 // Okay, we have at least one ~0 value, check to see if the rest match or are
153 // undefs.
154 for (++i; i != e; ++i)
155 if (N->getOperand(i) != Zero &&
156 N->getOperand(i).getOpcode() != ISD::UNDEF)
157 return false;
158 return true;
161 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
162 /// when given the operation for (X op Y).
163 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
164 // To perform this operation, we just need to swap the L and G bits of the
165 // operation.
166 unsigned OldL = (Operation >> 2) & 1;
167 unsigned OldG = (Operation >> 1) & 1;
168 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
169 (OldL << 1) | // New G bit
170 (OldG << 2)); // New L bit.
173 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
174 /// 'op' is a valid SetCC operation.
175 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
176 unsigned Operation = Op;
177 if (isInteger)
178 Operation ^= 7; // Flip L, G, E bits, but not U.
179 else
180 Operation ^= 15; // Flip all of the condition bits.
181 if (Operation > ISD::SETTRUE2)
182 Operation &= ~8; // Don't let N and U bits get set.
183 return ISD::CondCode(Operation);
187 /// isSignedOp - For an integer comparison, return 1 if the comparison is a
188 /// signed operation and 2 if the result is an unsigned comparison. Return zero
189 /// if the operation does not depend on the sign of the input (setne and seteq).
190 static int isSignedOp(ISD::CondCode Opcode) {
191 switch (Opcode) {
192 default: assert(0 && "Illegal integer setcc operation!");
193 case ISD::SETEQ:
194 case ISD::SETNE: return 0;
195 case ISD::SETLT:
196 case ISD::SETLE:
197 case ISD::SETGT:
198 case ISD::SETGE: return 1;
199 case ISD::SETULT:
200 case ISD::SETULE:
201 case ISD::SETUGT:
202 case ISD::SETUGE: return 2;
206 /// getSetCCOrOperation - Return the result of a logical OR between different
207 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
208 /// returns SETCC_INVALID if it is not possible to represent the resultant
209 /// comparison.
210 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
211 bool isInteger) {
212 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
213 // Cannot fold a signed integer setcc with an unsigned integer setcc.
214 return ISD::SETCC_INVALID;
216 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
218 // If the N and U bits get set then the resultant comparison DOES suddenly
219 // care about orderedness, and is true when ordered.
220 if (Op > ISD::SETTRUE2)
221 Op &= ~16; // Clear the U bit if the N bit is set.
223 // Canonicalize illegal integer setcc's.
224 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
225 Op = ISD::SETNE;
227 return ISD::CondCode(Op);
230 /// getSetCCAndOperation - Return the result of a logical AND between different
231 /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
232 /// function returns zero if it is not possible to represent the resultant
233 /// comparison.
234 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
235 bool isInteger) {
236 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
237 // Cannot fold a signed setcc with an unsigned setcc.
238 return ISD::SETCC_INVALID;
240 // Combine all of the condition bits.
241 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
243 // Canonicalize illegal integer setcc's.
244 if (isInteger) {
245 switch (Result) {
246 default: break;
247 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
248 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
249 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
250 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
254 return Result;
257 const TargetMachine &SelectionDAG::getTarget() const {
258 return TLI.getTargetMachine();
261 //===----------------------------------------------------------------------===//
262 // SelectionDAG Class
263 //===----------------------------------------------------------------------===//
265 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
266 /// SelectionDAG.
267 void SelectionDAG::RemoveDeadNodes() {
268 // Create a dummy node (which is not added to allnodes), that adds a reference
269 // to the root node, preventing it from being deleted.
270 HandleSDNode Dummy(getRoot());
272 SmallVector<SDNode*, 128> DeadNodes;
274 // Add all obviously-dead nodes to the DeadNodes worklist.
275 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
276 if (I->use_empty())
277 DeadNodes.push_back(I);
279 // Process the worklist, deleting the nodes and adding their uses to the
280 // worklist.
281 while (!DeadNodes.empty()) {
282 SDNode *N = DeadNodes.back();
283 DeadNodes.pop_back();
285 // Take the node out of the appropriate CSE map.
286 RemoveNodeFromCSEMaps(N);
288 // Next, brutally remove the operand list. This is safe to do, as there are
289 // no cycles in the graph.
290 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
291 SDNode *Operand = I->Val;
292 Operand->removeUser(N);
294 // Now that we removed this operand, see if there are no uses of it left.
295 if (Operand->use_empty())
296 DeadNodes.push_back(Operand);
298 delete[] N->OperandList;
299 N->OperandList = 0;
300 N->NumOperands = 0;
302 // Finally, remove N itself.
303 AllNodes.erase(N);
306 // If the root changed (e.g. it was a dead load, update the root).
307 setRoot(Dummy.getValue());
310 void SelectionDAG::DeleteNode(SDNode *N) {
311 assert(N->use_empty() && "Cannot delete a node that is not dead!");
313 // First take this out of the appropriate CSE map.
314 RemoveNodeFromCSEMaps(N);
316 // Finally, remove uses due to operands of this node, remove from the
317 // AllNodes list, and delete the node.
318 DeleteNodeNotInCSEMaps(N);
321 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
323 // Remove it from the AllNodes list.
324 AllNodes.remove(N);
326 // Drop all of the operands and decrement used nodes use counts.
327 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
328 I->Val->removeUser(N);
329 delete[] N->OperandList;
330 N->OperandList = 0;
331 N->NumOperands = 0;
333 delete N;
336 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
337 /// correspond to it. This is useful when we're about to delete or repurpose
338 /// the node. We don't want future request for structurally identical nodes
339 /// to return N anymore.
340 void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
341 bool Erased = false;
342 switch (N->getOpcode()) {
343 case ISD::HANDLENODE: return; // noop.
344 case ISD::Constant:
345 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
346 N->getValueType(0)));
347 break;
348 case ISD::TargetConstant:
349 Erased = TargetConstants.erase(std::make_pair(
350 cast<ConstantSDNode>(N)->getValue(),
351 N->getValueType(0)));
352 break;
353 case ISD::ConstantFP: {
354 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
355 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
356 break;
358 case ISD::TargetConstantFP: {
359 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
360 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
361 break;
363 case ISD::STRING:
364 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
365 break;
366 case ISD::CONDCODE:
367 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
368 "Cond code doesn't exist!");
369 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
370 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
371 break;
372 case ISD::GlobalAddress: {
373 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
374 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
375 GN->getOffset()));
376 break;
378 case ISD::TargetGlobalAddress: {
379 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
380 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
381 GN->getOffset()));
382 break;
384 case ISD::FrameIndex:
385 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
386 break;
387 case ISD::TargetFrameIndex:
388 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
389 break;
390 case ISD::JumpTable:
391 Erased = JumpTableIndices.erase(cast<JumpTableSDNode>(N)->getIndex());
392 break;
393 case ISD::TargetJumpTable:
394 Erased =
395 TargetJumpTableIndices.erase(cast<JumpTableSDNode>(N)->getIndex());
396 break;
397 case ISD::ConstantPool:
398 Erased = ConstantPoolIndices.
399 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
400 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
401 cast<ConstantPoolSDNode>(N)->getAlignment())));
402 break;
403 case ISD::TargetConstantPool:
404 Erased = TargetConstantPoolIndices.
405 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
406 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
407 cast<ConstantPoolSDNode>(N)->getAlignment())));
408 break;
409 case ISD::BasicBlock:
410 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
411 break;
412 case ISD::ExternalSymbol:
413 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
414 break;
415 case ISD::TargetExternalSymbol:
416 Erased =
417 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
418 break;
419 case ISD::VALUETYPE:
420 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
421 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
422 break;
423 case ISD::Register:
424 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
425 N->getValueType(0)));
426 break;
427 case ISD::SRCVALUE: {
428 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
429 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
430 break;
432 default:
433 if (N->getNumValues() == 1) {
434 if (N->getNumOperands() == 0) {
435 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
436 N->getValueType(0)));
437 } else {
438 // Remove it from the CSE Map.
439 Erased = CSEMap.RemoveNode(N);
441 } else {
442 // Remove it from the CSE Map.
443 Erased = CSEMap.RemoveNode(N);
445 break;
447 #ifndef NDEBUG
448 // Verify that the node was actually in one of the CSE maps, unless it has a
449 // flag result (which cannot be CSE'd) or is one of the special cases that are
450 // not subject to CSE.
451 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
452 !N->isTargetOpcode()) {
453 N->dump();
454 std::cerr << "\n";
455 assert(0 && "Node is not in map!");
457 #endif
460 /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
461 /// has been taken out and modified in some way. If the specified node already
462 /// exists in the CSE maps, do not modify the maps, but return the existing node
463 /// instead. If it doesn't exist, add it and return null.
465 SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
466 assert(N->getNumOperands() && "This is a leaf node!");
467 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
468 return 0; // Never add these nodes.
470 // Check that remaining values produced are not flags.
471 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
472 if (N->getValueType(i) == MVT::Flag)
473 return 0; // Never CSE anything that produces a flag.
475 SDNode *New = CSEMap.GetOrInsertNode(N);
476 if (New != N) return New; // Node already existed.
477 return 0;
480 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
481 /// were replaced with those specified. If this node is never memoized,
482 /// return null, otherwise return a pointer to the slot it would take. If a
483 /// node already exists with these operands, the slot will be non-null.
484 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
485 void *&InsertPos) {
486 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
487 return 0; // Never add these nodes.
489 // Check that remaining values produced are not flags.
490 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
491 if (N->getValueType(i) == MVT::Flag)
492 return 0; // Never CSE anything that produces a flag.
494 SelectionDAGCSEMap::NodeID ID;
495 ID.SetOpcode(N->getOpcode());
496 ID.SetValueTypes(N->value_begin());
497 ID.SetOperands(Op);
498 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
501 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
502 /// were replaced with those specified. If this node is never memoized,
503 /// return null, otherwise return a pointer to the slot it would take. If a
504 /// node already exists with these operands, the slot will be non-null.
505 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
506 SDOperand Op1, SDOperand Op2,
507 void *&InsertPos) {
508 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
509 return 0; // Never add these nodes.
511 // Check that remaining values produced are not flags.
512 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
513 if (N->getValueType(i) == MVT::Flag)
514 return 0; // Never CSE anything that produces a flag.
516 SelectionDAGCSEMap::NodeID ID;
517 ID.SetOpcode(N->getOpcode());
518 ID.SetValueTypes(N->value_begin());
519 ID.SetOperands(Op1, Op2);
520 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
524 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
525 /// were replaced with those specified. If this node is never memoized,
526 /// return null, otherwise return a pointer to the slot it would take. If a
527 /// node already exists with these operands, the slot will be non-null.
528 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
529 const SDOperand *Ops,unsigned NumOps,
530 void *&InsertPos) {
531 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
532 return 0; // Never add these nodes.
534 // Check that remaining values produced are not flags.
535 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
536 if (N->getValueType(i) == MVT::Flag)
537 return 0; // Never CSE anything that produces a flag.
539 SelectionDAGCSEMap::NodeID ID;
540 ID.SetOpcode(N->getOpcode());
541 ID.SetValueTypes(N->value_begin());
542 ID.SetOperands(Ops, NumOps);
543 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
547 SelectionDAG::~SelectionDAG() {
548 while (!AllNodes.empty()) {
549 SDNode *N = AllNodes.begin();
550 delete [] N->OperandList;
551 N->OperandList = 0;
552 N->NumOperands = 0;
553 AllNodes.pop_front();
557 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
558 if (Op.getValueType() == VT) return Op;
559 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
560 return getNode(ISD::AND, Op.getValueType(), Op,
561 getConstant(Imm, Op.getValueType()));
564 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
565 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
566 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
568 // Mask out any bits that are not valid for this constant.
569 if (VT != MVT::i64)
570 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
572 SDNode *&N = Constants[std::make_pair(Val, VT)];
573 if (N) return SDOperand(N, 0);
574 N = new ConstantSDNode(false, Val, VT);
575 AllNodes.push_back(N);
576 return SDOperand(N, 0);
579 SDOperand SelectionDAG::getString(const std::string &Val) {
580 StringSDNode *&N = StringNodes[Val];
581 if (!N) {
582 N = new StringSDNode(Val);
583 AllNodes.push_back(N);
585 return SDOperand(N, 0);
588 SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
589 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
590 // Mask out any bits that are not valid for this constant.
591 if (VT != MVT::i64)
592 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
594 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
595 if (N) return SDOperand(N, 0);
596 N = new ConstantSDNode(true, Val, VT);
597 AllNodes.push_back(N);
598 return SDOperand(N, 0);
601 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
602 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
603 if (VT == MVT::f32)
604 Val = (float)Val; // Mask out extra precision.
606 // Do the map lookup using the actual bit pattern for the floating point
607 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
608 // we don't have issues with SNANs.
609 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
610 if (N) return SDOperand(N, 0);
611 N = new ConstantFPSDNode(false, Val, VT);
612 AllNodes.push_back(N);
613 return SDOperand(N, 0);
616 SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
617 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
618 if (VT == MVT::f32)
619 Val = (float)Val; // Mask out extra precision.
621 // Do the map lookup using the actual bit pattern for the floating point
622 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
623 // we don't have issues with SNANs.
624 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
625 if (N) return SDOperand(N, 0);
626 N = new ConstantFPSDNode(true, Val, VT);
627 AllNodes.push_back(N);
628 return SDOperand(N, 0);
631 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
632 MVT::ValueType VT, int offset) {
633 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
634 if (N) return SDOperand(N, 0);
635 N = new GlobalAddressSDNode(false, GV, VT, offset);
636 AllNodes.push_back(N);
637 return SDOperand(N, 0);
640 SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
641 MVT::ValueType VT, int offset) {
642 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
643 if (N) return SDOperand(N, 0);
644 N = new GlobalAddressSDNode(true, GV, VT, offset);
645 AllNodes.push_back(N);
646 return SDOperand(N, 0);
649 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
650 SDNode *&N = FrameIndices[FI];
651 if (N) return SDOperand(N, 0);
652 N = new FrameIndexSDNode(FI, VT, false);
653 AllNodes.push_back(N);
654 return SDOperand(N, 0);
657 SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
658 SDNode *&N = TargetFrameIndices[FI];
659 if (N) return SDOperand(N, 0);
660 N = new FrameIndexSDNode(FI, VT, true);
661 AllNodes.push_back(N);
662 return SDOperand(N, 0);
665 SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT) {
666 SDNode *&N = JumpTableIndices[JTI];
667 if (N) return SDOperand(N, 0);
668 N = new JumpTableSDNode(JTI, VT, false);
669 AllNodes.push_back(N);
670 return SDOperand(N, 0);
673 SDOperand SelectionDAG::getTargetJumpTable(int JTI, MVT::ValueType VT) {
674 SDNode *&N = TargetJumpTableIndices[JTI];
675 if (N) return SDOperand(N, 0);
676 N = new JumpTableSDNode(JTI, VT, true);
677 AllNodes.push_back(N);
678 return SDOperand(N, 0);
681 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
682 unsigned Alignment, int Offset) {
683 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
684 std::make_pair(Offset, Alignment))];
685 if (N) return SDOperand(N, 0);
686 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
687 AllNodes.push_back(N);
688 return SDOperand(N, 0);
691 SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
692 unsigned Alignment, int Offset) {
693 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
694 std::make_pair(Offset, Alignment))];
695 if (N) return SDOperand(N, 0);
696 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
697 AllNodes.push_back(N);
698 return SDOperand(N, 0);
701 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
702 SDNode *&N = BBNodes[MBB];
703 if (N) return SDOperand(N, 0);
704 N = new BasicBlockSDNode(MBB);
705 AllNodes.push_back(N);
706 return SDOperand(N, 0);
709 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
710 if ((unsigned)VT >= ValueTypeNodes.size())
711 ValueTypeNodes.resize(VT+1);
712 if (ValueTypeNodes[VT] == 0) {
713 ValueTypeNodes[VT] = new VTSDNode(VT);
714 AllNodes.push_back(ValueTypeNodes[VT]);
717 return SDOperand(ValueTypeNodes[VT], 0);
720 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
721 SDNode *&N = ExternalSymbols[Sym];
722 if (N) return SDOperand(N, 0);
723 N = new ExternalSymbolSDNode(false, Sym, VT);
724 AllNodes.push_back(N);
725 return SDOperand(N, 0);
728 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
729 MVT::ValueType VT) {
730 SDNode *&N = TargetExternalSymbols[Sym];
731 if (N) return SDOperand(N, 0);
732 N = new ExternalSymbolSDNode(true, Sym, VT);
733 AllNodes.push_back(N);
734 return SDOperand(N, 0);
737 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
738 if ((unsigned)Cond >= CondCodeNodes.size())
739 CondCodeNodes.resize(Cond+1);
741 if (CondCodeNodes[Cond] == 0) {
742 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
743 AllNodes.push_back(CondCodeNodes[Cond]);
745 return SDOperand(CondCodeNodes[Cond], 0);
748 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
749 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
750 if (!Reg) {
751 Reg = new RegisterSDNode(RegNo, VT);
752 AllNodes.push_back(Reg);
754 return SDOperand(Reg, 0);
757 SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
758 SDOperand N2, ISD::CondCode Cond) {
759 // These setcc operations always fold.
760 switch (Cond) {
761 default: break;
762 case ISD::SETFALSE:
763 case ISD::SETFALSE2: return getConstant(0, VT);
764 case ISD::SETTRUE:
765 case ISD::SETTRUE2: return getConstant(1, VT);
767 case ISD::SETOEQ:
768 case ISD::SETOGT:
769 case ISD::SETOGE:
770 case ISD::SETOLT:
771 case ISD::SETOLE:
772 case ISD::SETONE:
773 case ISD::SETO:
774 case ISD::SETUO:
775 case ISD::SETUEQ:
776 case ISD::SETUNE:
777 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
778 break;
781 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
782 uint64_t C2 = N2C->getValue();
783 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
784 uint64_t C1 = N1C->getValue();
786 // Sign extend the operands if required
787 if (ISD::isSignedIntSetCC(Cond)) {
788 C1 = N1C->getSignExtended();
789 C2 = N2C->getSignExtended();
792 switch (Cond) {
793 default: assert(0 && "Unknown integer setcc!");
794 case ISD::SETEQ: return getConstant(C1 == C2, VT);
795 case ISD::SETNE: return getConstant(C1 != C2, VT);
796 case ISD::SETULT: return getConstant(C1 < C2, VT);
797 case ISD::SETUGT: return getConstant(C1 > C2, VT);
798 case ISD::SETULE: return getConstant(C1 <= C2, VT);
799 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
800 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
801 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
802 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
803 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
805 } else {
806 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
807 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
808 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
810 // If the comparison constant has bits in the upper part, the
811 // zero-extended value could never match.
812 if (C2 & (~0ULL << InSize)) {
813 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
814 switch (Cond) {
815 case ISD::SETUGT:
816 case ISD::SETUGE:
817 case ISD::SETEQ: return getConstant(0, VT);
818 case ISD::SETULT:
819 case ISD::SETULE:
820 case ISD::SETNE: return getConstant(1, VT);
821 case ISD::SETGT:
822 case ISD::SETGE:
823 // True if the sign bit of C2 is set.
824 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
825 case ISD::SETLT:
826 case ISD::SETLE:
827 // True if the sign bit of C2 isn't set.
828 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
829 default:
830 break;
834 // Otherwise, we can perform the comparison with the low bits.
835 switch (Cond) {
836 case ISD::SETEQ:
837 case ISD::SETNE:
838 case ISD::SETUGT:
839 case ISD::SETUGE:
840 case ISD::SETULT:
841 case ISD::SETULE:
842 return getSetCC(VT, N1.getOperand(0),
843 getConstant(C2, N1.getOperand(0).getValueType()),
844 Cond);
845 default:
846 break; // todo, be more careful with signed comparisons
848 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
849 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
850 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
851 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
852 MVT::ValueType ExtDstTy = N1.getValueType();
853 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
855 // If the extended part has any inconsistent bits, it cannot ever
856 // compare equal. In other words, they have to be all ones or all
857 // zeros.
858 uint64_t ExtBits =
859 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
860 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
861 return getConstant(Cond == ISD::SETNE, VT);
863 // Otherwise, make this a use of a zext.
864 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
865 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
866 Cond);
869 uint64_t MinVal, MaxVal;
870 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
871 if (ISD::isSignedIntSetCC(Cond)) {
872 MinVal = 1ULL << (OperandBitSize-1);
873 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
874 MaxVal = ~0ULL >> (65-OperandBitSize);
875 else
876 MaxVal = 0;
877 } else {
878 MinVal = 0;
879 MaxVal = ~0ULL >> (64-OperandBitSize);
882 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
883 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
884 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
885 --C2; // X >= C1 --> X > (C1-1)
886 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
887 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
890 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
891 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
892 ++C2; // X <= C1 --> X < (C1+1)
893 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
894 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
897 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
898 return getConstant(0, VT); // X < MIN --> false
900 // Canonicalize setgt X, Min --> setne X, Min
901 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
902 return getSetCC(VT, N1, N2, ISD::SETNE);
904 // If we have setult X, 1, turn it into seteq X, 0
905 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
906 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
907 ISD::SETEQ);
908 // If we have setugt X, Max-1, turn it into seteq X, Max
909 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
910 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
911 ISD::SETEQ);
913 // If we have "setcc X, C1", check to see if we can shrink the immediate
914 // by changing cc.
916 // SETUGT X, SINTMAX -> SETLT X, 0
917 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
918 C2 == (~0ULL >> (65-OperandBitSize)))
919 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
921 // FIXME: Implement the rest of these.
924 // Fold bit comparisons when we can.
925 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
926 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
927 if (ConstantSDNode *AndRHS =
928 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
929 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
930 // Perform the xform if the AND RHS is a single bit.
931 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
932 return getNode(ISD::SRL, VT, N1,
933 getConstant(Log2_64(AndRHS->getValue()),
934 TLI.getShiftAmountTy()));
936 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
937 // (X & 8) == 8 --> (X & 8) >> 3
938 // Perform the xform if C2 is a single bit.
939 if ((C2 & (C2-1)) == 0) {
940 return getNode(ISD::SRL, VT, N1,
941 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
946 } else if (isa<ConstantSDNode>(N1.Val)) {
947 // Ensure that the constant occurs on the RHS.
948 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
951 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
952 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
953 double C1 = N1C->getValue(), C2 = N2C->getValue();
955 switch (Cond) {
956 default: break; // FIXME: Implement the rest of these!
957 case ISD::SETEQ: return getConstant(C1 == C2, VT);
958 case ISD::SETNE: return getConstant(C1 != C2, VT);
959 case ISD::SETLT: return getConstant(C1 < C2, VT);
960 case ISD::SETGT: return getConstant(C1 > C2, VT);
961 case ISD::SETLE: return getConstant(C1 <= C2, VT);
962 case ISD::SETGE: return getConstant(C1 >= C2, VT);
964 } else {
965 // Ensure that the constant occurs on the RHS.
966 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
969 // Could not fold it.
970 return SDOperand();
973 /// getNode - Gets or creates the specified node.
975 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
976 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
977 if (!N) {
978 N = new SDNode(Opcode, VT);
979 AllNodes.push_back(N);
981 return SDOperand(N, 0);
984 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
985 SDOperand Operand) {
986 unsigned Tmp1;
987 // Constant fold unary operations with an integer constant operand.
988 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
989 uint64_t Val = C->getValue();
990 switch (Opcode) {
991 default: break;
992 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
993 case ISD::ANY_EXTEND:
994 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
995 case ISD::TRUNCATE: return getConstant(Val, VT);
996 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
997 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
998 case ISD::BIT_CONVERT:
999 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
1000 return getConstantFP(BitsToFloat(Val), VT);
1001 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
1002 return getConstantFP(BitsToDouble(Val), VT);
1003 break;
1004 case ISD::BSWAP:
1005 switch(VT) {
1006 default: assert(0 && "Invalid bswap!"); break;
1007 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1008 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1009 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1011 break;
1012 case ISD::CTPOP:
1013 switch(VT) {
1014 default: assert(0 && "Invalid ctpop!"); break;
1015 case MVT::i1: return getConstant(Val != 0, VT);
1016 case MVT::i8:
1017 Tmp1 = (unsigned)Val & 0xFF;
1018 return getConstant(CountPopulation_32(Tmp1), VT);
1019 case MVT::i16:
1020 Tmp1 = (unsigned)Val & 0xFFFF;
1021 return getConstant(CountPopulation_32(Tmp1), VT);
1022 case MVT::i32:
1023 return getConstant(CountPopulation_32((unsigned)Val), VT);
1024 case MVT::i64:
1025 return getConstant(CountPopulation_64(Val), VT);
1027 case ISD::CTLZ:
1028 switch(VT) {
1029 default: assert(0 && "Invalid ctlz!"); break;
1030 case MVT::i1: return getConstant(Val == 0, VT);
1031 case MVT::i8:
1032 Tmp1 = (unsigned)Val & 0xFF;
1033 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1034 case MVT::i16:
1035 Tmp1 = (unsigned)Val & 0xFFFF;
1036 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1037 case MVT::i32:
1038 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1039 case MVT::i64:
1040 return getConstant(CountLeadingZeros_64(Val), VT);
1042 case ISD::CTTZ:
1043 switch(VT) {
1044 default: assert(0 && "Invalid cttz!"); break;
1045 case MVT::i1: return getConstant(Val == 0, VT);
1046 case MVT::i8:
1047 Tmp1 = (unsigned)Val | 0x100;
1048 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1049 case MVT::i16:
1050 Tmp1 = (unsigned)Val | 0x10000;
1051 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1052 case MVT::i32:
1053 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1054 case MVT::i64:
1055 return getConstant(CountTrailingZeros_64(Val), VT);
1060 // Constant fold unary operations with an floating point constant operand.
1061 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1062 switch (Opcode) {
1063 case ISD::FNEG:
1064 return getConstantFP(-C->getValue(), VT);
1065 case ISD::FABS:
1066 return getConstantFP(fabs(C->getValue()), VT);
1067 case ISD::FP_ROUND:
1068 case ISD::FP_EXTEND:
1069 return getConstantFP(C->getValue(), VT);
1070 case ISD::FP_TO_SINT:
1071 return getConstant((int64_t)C->getValue(), VT);
1072 case ISD::FP_TO_UINT:
1073 return getConstant((uint64_t)C->getValue(), VT);
1074 case ISD::BIT_CONVERT:
1075 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1076 return getConstant(FloatToBits(C->getValue()), VT);
1077 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1078 return getConstant(DoubleToBits(C->getValue()), VT);
1079 break;
1082 unsigned OpOpcode = Operand.Val->getOpcode();
1083 switch (Opcode) {
1084 case ISD::TokenFactor:
1085 return Operand; // Factor of one node? No factor.
1086 case ISD::SIGN_EXTEND:
1087 if (Operand.getValueType() == VT) return Operand; // noop extension
1088 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
1089 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1090 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1091 break;
1092 case ISD::ZERO_EXTEND:
1093 if (Operand.getValueType() == VT) return Operand; // noop extension
1094 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
1095 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1096 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1097 break;
1098 case ISD::ANY_EXTEND:
1099 if (Operand.getValueType() == VT) return Operand; // noop extension
1100 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
1101 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1102 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1103 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1104 break;
1105 case ISD::TRUNCATE:
1106 if (Operand.getValueType() == VT) return Operand; // noop truncate
1107 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
1108 if (OpOpcode == ISD::TRUNCATE)
1109 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1110 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1111 OpOpcode == ISD::ANY_EXTEND) {
1112 // If the source is smaller than the dest, we still need an extend.
1113 if (Operand.Val->getOperand(0).getValueType() < VT)
1114 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1115 else if (Operand.Val->getOperand(0).getValueType() > VT)
1116 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1117 else
1118 return Operand.Val->getOperand(0);
1120 break;
1121 case ISD::BIT_CONVERT:
1122 // Basic sanity checking.
1123 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
1124 && "Cannot BIT_CONVERT between two different types!");
1125 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1126 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1127 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1128 if (OpOpcode == ISD::UNDEF)
1129 return getNode(ISD::UNDEF, VT);
1130 break;
1131 case ISD::SCALAR_TO_VECTOR:
1132 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1133 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1134 "Illegal SCALAR_TO_VECTOR node!");
1135 break;
1136 case ISD::FNEG:
1137 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1138 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1139 Operand.Val->getOperand(0));
1140 if (OpOpcode == ISD::FNEG) // --X -> X
1141 return Operand.Val->getOperand(0);
1142 break;
1143 case ISD::FABS:
1144 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1145 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1146 break;
1149 SDNode *N;
1150 MVT::ValueType *VTs = getNodeValueTypes(VT);
1151 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1152 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Operand);
1153 void *IP = 0;
1154 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1155 return SDOperand(E, 0);
1156 N = new SDNode(Opcode, Operand);
1157 N->setValueTypes(VTs, 1);
1158 CSEMap.InsertNode(N, IP);
1159 } else {
1160 N = new SDNode(Opcode, Operand);
1161 N->setValueTypes(VTs, 1);
1163 AllNodes.push_back(N);
1164 return SDOperand(N, 0);
1169 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1170 SDOperand N1, SDOperand N2) {
1171 #ifndef NDEBUG
1172 switch (Opcode) {
1173 case ISD::TokenFactor:
1174 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1175 N2.getValueType() == MVT::Other && "Invalid token factor!");
1176 break;
1177 case ISD::AND:
1178 case ISD::OR:
1179 case ISD::XOR:
1180 case ISD::UDIV:
1181 case ISD::UREM:
1182 case ISD::MULHU:
1183 case ISD::MULHS:
1184 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1185 // fall through
1186 case ISD::ADD:
1187 case ISD::SUB:
1188 case ISD::MUL:
1189 case ISD::SDIV:
1190 case ISD::SREM:
1191 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1192 // fall through.
1193 case ISD::FADD:
1194 case ISD::FSUB:
1195 case ISD::FMUL:
1196 case ISD::FDIV:
1197 case ISD::FREM:
1198 assert(N1.getValueType() == N2.getValueType() &&
1199 N1.getValueType() == VT && "Binary operator types must match!");
1200 break;
1201 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1202 assert(N1.getValueType() == VT &&
1203 MVT::isFloatingPoint(N1.getValueType()) &&
1204 MVT::isFloatingPoint(N2.getValueType()) &&
1205 "Invalid FCOPYSIGN!");
1206 break;
1207 case ISD::SHL:
1208 case ISD::SRA:
1209 case ISD::SRL:
1210 case ISD::ROTL:
1211 case ISD::ROTR:
1212 assert(VT == N1.getValueType() &&
1213 "Shift operators return type must be the same as their first arg");
1214 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1215 VT != MVT::i1 && "Shifts only work on integers");
1216 break;
1217 case ISD::FP_ROUND_INREG: {
1218 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1219 assert(VT == N1.getValueType() && "Not an inreg round!");
1220 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1221 "Cannot FP_ROUND_INREG integer types");
1222 assert(EVT <= VT && "Not rounding down!");
1223 break;
1225 case ISD::AssertSext:
1226 case ISD::AssertZext:
1227 case ISD::SIGN_EXTEND_INREG: {
1228 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1229 assert(VT == N1.getValueType() && "Not an inreg extend!");
1230 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1231 "Cannot *_EXTEND_INREG FP types");
1232 assert(EVT <= VT && "Not extending!");
1235 default: break;
1237 #endif
1239 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1240 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1241 if (N1C) {
1242 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1243 int64_t Val = N1C->getValue();
1244 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1245 Val <<= 64-FromBits;
1246 Val >>= 64-FromBits;
1247 return getConstant(Val, VT);
1250 if (N2C) {
1251 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1252 switch (Opcode) {
1253 case ISD::ADD: return getConstant(C1 + C2, VT);
1254 case ISD::SUB: return getConstant(C1 - C2, VT);
1255 case ISD::MUL: return getConstant(C1 * C2, VT);
1256 case ISD::UDIV:
1257 if (C2) return getConstant(C1 / C2, VT);
1258 break;
1259 case ISD::UREM :
1260 if (C2) return getConstant(C1 % C2, VT);
1261 break;
1262 case ISD::SDIV :
1263 if (C2) return getConstant(N1C->getSignExtended() /
1264 N2C->getSignExtended(), VT);
1265 break;
1266 case ISD::SREM :
1267 if (C2) return getConstant(N1C->getSignExtended() %
1268 N2C->getSignExtended(), VT);
1269 break;
1270 case ISD::AND : return getConstant(C1 & C2, VT);
1271 case ISD::OR : return getConstant(C1 | C2, VT);
1272 case ISD::XOR : return getConstant(C1 ^ C2, VT);
1273 case ISD::SHL : return getConstant(C1 << C2, VT);
1274 case ISD::SRL : return getConstant(C1 >> C2, VT);
1275 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
1276 case ISD::ROTL :
1277 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1278 VT);
1279 case ISD::ROTR :
1280 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1281 VT);
1282 default: break;
1284 } else { // Cannonicalize constant to RHS if commutative
1285 if (isCommutativeBinOp(Opcode)) {
1286 std::swap(N1C, N2C);
1287 std::swap(N1, N2);
1292 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1293 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1294 if (N1CFP) {
1295 if (N2CFP) {
1296 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1297 switch (Opcode) {
1298 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1299 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1300 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1301 case ISD::FDIV:
1302 if (C2) return getConstantFP(C1 / C2, VT);
1303 break;
1304 case ISD::FREM :
1305 if (C2) return getConstantFP(fmod(C1, C2), VT);
1306 break;
1307 case ISD::FCOPYSIGN: {
1308 union {
1309 double F;
1310 uint64_t I;
1311 } u1;
1312 union {
1313 double F;
1314 int64_t I;
1315 } u2;
1316 u1.F = C1;
1317 u2.F = C2;
1318 if (u2.I < 0) // Sign bit of RHS set?
1319 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1320 else
1321 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1322 return getConstantFP(u1.F, VT);
1324 default: break;
1326 } else { // Cannonicalize constant to RHS if commutative
1327 if (isCommutativeBinOp(Opcode)) {
1328 std::swap(N1CFP, N2CFP);
1329 std::swap(N1, N2);
1334 // Canonicalize an UNDEF to the RHS, even over a constant.
1335 if (N1.getOpcode() == ISD::UNDEF) {
1336 if (isCommutativeBinOp(Opcode)) {
1337 std::swap(N1, N2);
1338 } else {
1339 switch (Opcode) {
1340 case ISD::FP_ROUND_INREG:
1341 case ISD::SIGN_EXTEND_INREG:
1342 case ISD::SUB:
1343 case ISD::FSUB:
1344 case ISD::FDIV:
1345 case ISD::FREM:
1346 case ISD::SRA:
1347 return N1; // fold op(undef, arg2) -> undef
1348 case ISD::UDIV:
1349 case ISD::SDIV:
1350 case ISD::UREM:
1351 case ISD::SREM:
1352 case ISD::SRL:
1353 case ISD::SHL:
1354 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1359 // Fold a bunch of operators when the RHS is undef.
1360 if (N2.getOpcode() == ISD::UNDEF) {
1361 switch (Opcode) {
1362 case ISD::ADD:
1363 case ISD::SUB:
1364 case ISD::FADD:
1365 case ISD::FSUB:
1366 case ISD::FMUL:
1367 case ISD::FDIV:
1368 case ISD::FREM:
1369 case ISD::UDIV:
1370 case ISD::SDIV:
1371 case ISD::UREM:
1372 case ISD::SREM:
1373 case ISD::XOR:
1374 return N2; // fold op(arg1, undef) -> undef
1375 case ISD::MUL:
1376 case ISD::AND:
1377 case ISD::SRL:
1378 case ISD::SHL:
1379 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1380 case ISD::OR:
1381 return getConstant(MVT::getIntVTBitMask(VT), VT);
1382 case ISD::SRA:
1383 return N1;
1387 // Finally, fold operations that do not require constants.
1388 switch (Opcode) {
1389 case ISD::FP_ROUND_INREG:
1390 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1391 break;
1392 case ISD::SIGN_EXTEND_INREG: {
1393 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1394 if (EVT == VT) return N1; // Not actually extending
1395 break;
1398 // FIXME: figure out how to safely handle things like
1399 // int foo(int x) { return 1 << (x & 255); }
1400 // int bar() { return foo(256); }
1401 #if 0
1402 case ISD::SHL:
1403 case ISD::SRL:
1404 case ISD::SRA:
1405 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1406 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
1407 return getNode(Opcode, VT, N1, N2.getOperand(0));
1408 else if (N2.getOpcode() == ISD::AND)
1409 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1410 // If the and is only masking out bits that cannot effect the shift,
1411 // eliminate the and.
1412 unsigned NumBits = MVT::getSizeInBits(VT);
1413 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1414 return getNode(Opcode, VT, N1, N2.getOperand(0));
1416 break;
1417 #endif
1420 // Memoize this node if possible.
1421 SDNode *N;
1422 MVT::ValueType *VTs = getNodeValueTypes(VT);
1423 if (VT != MVT::Flag) {
1424 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2);
1425 void *IP = 0;
1426 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1427 return SDOperand(E, 0);
1428 N = new SDNode(Opcode, N1, N2);
1429 N->setValueTypes(VTs, 1);
1430 CSEMap.InsertNode(N, IP);
1431 } else {
1432 N = new SDNode(Opcode, N1, N2);
1433 N->setValueTypes(VTs, 1);
1436 AllNodes.push_back(N);
1437 return SDOperand(N, 0);
1440 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1441 SDOperand N1, SDOperand N2, SDOperand N3) {
1442 // Perform various simplifications.
1443 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1444 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1445 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1446 switch (Opcode) {
1447 case ISD::SETCC: {
1448 // Use SimplifySetCC to simplify SETCC's.
1449 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
1450 if (Simp.Val) return Simp;
1451 break;
1453 case ISD::SELECT:
1454 if (N1C)
1455 if (N1C->getValue())
1456 return N2; // select true, X, Y -> X
1457 else
1458 return N3; // select false, X, Y -> Y
1460 if (N2 == N3) return N2; // select C, X, X -> X
1461 break;
1462 case ISD::BRCOND:
1463 if (N2C)
1464 if (N2C->getValue()) // Unconditional branch
1465 return getNode(ISD::BR, MVT::Other, N1, N3);
1466 else
1467 return N1; // Never-taken branch
1468 break;
1469 case ISD::VECTOR_SHUFFLE:
1470 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1471 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1472 N3.getOpcode() == ISD::BUILD_VECTOR &&
1473 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1474 "Illegal VECTOR_SHUFFLE node!");
1475 break;
1478 // Memoize node if it doesn't produce a flag.
1479 SDNode *N;
1480 MVT::ValueType *VTs = getNodeValueTypes(VT);
1482 if (VT != MVT::Flag) {
1483 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2, N3);
1484 void *IP = 0;
1485 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1486 return SDOperand(E, 0);
1487 N = new SDNode(Opcode, N1, N2, N3);
1488 N->setValueTypes(VTs, 1);
1489 CSEMap.InsertNode(N, IP);
1490 } else {
1491 N = new SDNode(Opcode, N1, N2, N3);
1492 N->setValueTypes(VTs, 1);
1494 AllNodes.push_back(N);
1495 return SDOperand(N, 0);
1498 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1499 SDOperand N1, SDOperand N2, SDOperand N3,
1500 SDOperand N4) {
1501 SDOperand Ops[] = { N1, N2, N3, N4 };
1502 return getNode(Opcode, VT, Ops, 4);
1505 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1506 SDOperand N1, SDOperand N2, SDOperand N3,
1507 SDOperand N4, SDOperand N5) {
1508 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1509 return getNode(Opcode, VT, Ops, 5);
1512 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1513 SDOperand Chain, SDOperand Ptr,
1514 SDOperand SV) {
1515 MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other);
1517 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, SV);
1518 void *IP = 0;
1519 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1520 return SDOperand(E, 0);
1521 SDNode *N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1522 N->setValueTypes(VTs, 2);
1523 CSEMap.InsertNode(N, IP);
1524 AllNodes.push_back(N);
1525 return SDOperand(N, 0);
1528 SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1529 SDOperand Chain, SDOperand Ptr,
1530 SDOperand SV) {
1531 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1532 getValueType(EVT) };
1533 std::vector<MVT::ValueType> VTs;
1534 VTs.reserve(2);
1535 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1536 return getNode(ISD::VLOAD, VTs, Ops, 5);
1539 SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1540 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1541 MVT::ValueType EVT) {
1542 SDOperand Ops[] = { Chain, Ptr, SV, getValueType(EVT) };
1543 std::vector<MVT::ValueType> VTs;
1544 VTs.reserve(2);
1545 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1546 return getNode(Opcode, VTs, Ops, 4);
1549 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
1550 assert((!V || isa<PointerType>(V->getType())) &&
1551 "SrcValue is not a pointer?");
1552 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1553 if (N) return SDOperand(N, 0);
1555 N = new SrcValueSDNode(V, Offset);
1556 AllNodes.push_back(N);
1557 return SDOperand(N, 0);
1560 SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1561 SDOperand Chain, SDOperand Ptr,
1562 SDOperand SV) {
1563 SDOperand Ops[] = { Chain, Ptr, SV };
1564 std::vector<MVT::ValueType> VTs;
1565 VTs.reserve(2);
1566 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1567 return getNode(ISD::VAARG, VTs, Ops, 3);
1570 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1571 const SDOperand *Ops, unsigned NumOps) {
1572 switch (NumOps) {
1573 case 0: return getNode(Opcode, VT);
1574 case 1: return getNode(Opcode, VT, Ops[0]);
1575 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1576 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
1577 default: break;
1580 switch (Opcode) {
1581 default: break;
1582 case ISD::TRUNCSTORE: {
1583 assert(NumOps == 5 && "TRUNCSTORE takes 5 operands!");
1584 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1585 #if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1586 // If this is a truncating store of a constant, convert to the desired type
1587 // and store it instead.
1588 if (isa<Constant>(Ops[0])) {
1589 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1590 if (isa<Constant>(Op))
1591 N1 = Op;
1593 // Also for ConstantFP?
1594 #endif
1595 if (Ops[0].getValueType() == EVT) // Normal store?
1596 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1597 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1598 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1599 "Can't do FP-INT conversion!");
1600 break;
1602 case ISD::SELECT_CC: {
1603 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
1604 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1605 "LHS and RHS of condition must have same type!");
1606 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1607 "True and False arms of SelectCC must have same type!");
1608 assert(Ops[2].getValueType() == VT &&
1609 "select_cc node must be of same type as true and false value!");
1610 break;
1612 case ISD::BR_CC: {
1613 assert(NumOps == 5 && "BR_CC takes 5 operands!");
1614 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1615 "LHS/RHS of comparison should match types!");
1616 break;
1620 // Memoize nodes.
1621 SDNode *N;
1622 MVT::ValueType *VTs = getNodeValueTypes(VT);
1623 if (VT != MVT::Flag) {
1624 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Ops, NumOps);
1625 void *IP = 0;
1626 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1627 return SDOperand(E, 0);
1628 N = new SDNode(Opcode, Ops, NumOps);
1629 N->setValueTypes(VTs, 1);
1630 CSEMap.InsertNode(N, IP);
1631 } else {
1632 N = new SDNode(Opcode, Ops, NumOps);
1633 N->setValueTypes(VTs, 1);
1635 AllNodes.push_back(N);
1636 return SDOperand(N, 0);
1639 SDOperand SelectionDAG::getNode(unsigned Opcode,
1640 std::vector<MVT::ValueType> &ResultTys,
1641 const SDOperand *Ops, unsigned NumOps) {
1642 if (ResultTys.size() == 1)
1643 return getNode(Opcode, ResultTys[0], Ops, NumOps);
1645 switch (Opcode) {
1646 case ISD::EXTLOAD:
1647 case ISD::SEXTLOAD:
1648 case ISD::ZEXTLOAD: {
1649 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1650 assert(NumOps == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1651 // If they are asking for an extending load from/to the same thing, return a
1652 // normal load.
1653 if (ResultTys[0] == EVT)
1654 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1655 if (MVT::isVector(ResultTys[0])) {
1656 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1657 "Invalid vector extload!");
1658 } else {
1659 assert(EVT < ResultTys[0] &&
1660 "Should only be an extending load, not truncating!");
1662 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1663 "Cannot sign/zero extend a FP/Vector load!");
1664 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1665 "Cannot convert from FP to Int or Int -> FP!");
1666 break;
1669 // FIXME: figure out how to safely handle things like
1670 // int foo(int x) { return 1 << (x & 255); }
1671 // int bar() { return foo(256); }
1672 #if 0
1673 case ISD::SRA_PARTS:
1674 case ISD::SRL_PARTS:
1675 case ISD::SHL_PARTS:
1676 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1677 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
1678 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1679 else if (N3.getOpcode() == ISD::AND)
1680 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1681 // If the and is only masking out bits that cannot effect the shift,
1682 // eliminate the and.
1683 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1684 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1685 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1687 break;
1688 #endif
1691 // Memoize the node unless it returns a flag.
1692 SDNode *N;
1693 MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
1694 if (ResultTys.back() != MVT::Flag) {
1695 SelectionDAGCSEMap::NodeID ID;
1696 ID.SetOpcode(Opcode);
1697 ID.SetValueTypes(VTs);
1698 ID.SetOperands(&Ops[0], NumOps);
1699 void *IP = 0;
1700 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1701 return SDOperand(E, 0);
1702 N = new SDNode(Opcode, Ops, NumOps);
1703 N->setValueTypes(VTs, ResultTys.size());
1704 CSEMap.InsertNode(N, IP);
1705 } else {
1706 N = new SDNode(Opcode, Ops, NumOps);
1707 N->setValueTypes(VTs, ResultTys.size());
1709 AllNodes.push_back(N);
1710 return SDOperand(N, 0);
1714 MVT::ValueType *SelectionDAG::getNodeValueTypes(MVT::ValueType VT) {
1715 return SDNode::getValueTypeList(VT);
1718 MVT::ValueType *SelectionDAG::getNodeValueTypes(
1719 std::vector<MVT::ValueType> &RetVals) {
1720 switch (RetVals.size()) {
1721 case 0: assert(0 && "Cannot have nodes without results!");
1722 case 1: return SDNode::getValueTypeList(RetVals[0]);
1723 case 2: return getNodeValueTypes(RetVals[0], RetVals[1]);
1724 default: break;
1727 std::list<std::vector<MVT::ValueType> >::iterator I =
1728 std::find(VTList.begin(), VTList.end(), RetVals);
1729 if (I == VTList.end()) {
1730 VTList.push_front(RetVals);
1731 I = VTList.begin();
1734 return &(*I)[0];
1737 MVT::ValueType *SelectionDAG::getNodeValueTypes(MVT::ValueType VT1,
1738 MVT::ValueType VT2) {
1739 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1740 E = VTList.end(); I != E; ++I) {
1741 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
1742 return &(*I)[0];
1744 std::vector<MVT::ValueType> V;
1745 V.push_back(VT1);
1746 V.push_back(VT2);
1747 VTList.push_front(V);
1748 return &(*VTList.begin())[0];
1751 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1752 /// specified operands. If the resultant node already exists in the DAG,
1753 /// this does not modify the specified node, instead it returns the node that
1754 /// already exists. If the resultant node does not exist in the DAG, the
1755 /// input node is returned. As a degenerate case, if you specify the same
1756 /// input operands as the node already has, the input node is returned.
1757 SDOperand SelectionDAG::
1758 UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1759 SDNode *N = InN.Val;
1760 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1762 // Check to see if there is no change.
1763 if (Op == N->getOperand(0)) return InN;
1765 // See if the modified node already exists.
1766 void *InsertPos = 0;
1767 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1768 return SDOperand(Existing, InN.ResNo);
1770 // Nope it doesn't. Remove the node from it's current place in the maps.
1771 if (InsertPos)
1772 RemoveNodeFromCSEMaps(N);
1774 // Now we update the operands.
1775 N->OperandList[0].Val->removeUser(N);
1776 Op.Val->addUser(N);
1777 N->OperandList[0] = Op;
1779 // If this gets put into a CSE map, add it.
1780 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1781 return InN;
1784 SDOperand SelectionDAG::
1785 UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1786 SDNode *N = InN.Val;
1787 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1789 // Check to see if there is no change.
1790 bool AnyChange = false;
1791 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1792 return InN; // No operands changed, just return the input node.
1794 // See if the modified node already exists.
1795 void *InsertPos = 0;
1796 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1797 return SDOperand(Existing, InN.ResNo);
1799 // Nope it doesn't. Remove the node from it's current place in the maps.
1800 if (InsertPos)
1801 RemoveNodeFromCSEMaps(N);
1803 // Now we update the operands.
1804 if (N->OperandList[0] != Op1) {
1805 N->OperandList[0].Val->removeUser(N);
1806 Op1.Val->addUser(N);
1807 N->OperandList[0] = Op1;
1809 if (N->OperandList[1] != Op2) {
1810 N->OperandList[1].Val->removeUser(N);
1811 Op2.Val->addUser(N);
1812 N->OperandList[1] = Op2;
1815 // If this gets put into a CSE map, add it.
1816 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1817 return InN;
1820 SDOperand SelectionDAG::
1821 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1822 SDOperand Ops[] = { Op1, Op2, Op3 };
1823 return UpdateNodeOperands(N, Ops, 3);
1826 SDOperand SelectionDAG::
1827 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1828 SDOperand Op3, SDOperand Op4) {
1829 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
1830 return UpdateNodeOperands(N, Ops, 4);
1833 SDOperand SelectionDAG::
1834 UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1835 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1836 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
1837 return UpdateNodeOperands(N, Ops, 5);
1841 SDOperand SelectionDAG::
1842 UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
1843 SDNode *N = InN.Val;
1844 assert(N->getNumOperands() == NumOps &&
1845 "Update with wrong number of operands");
1847 // Check to see if there is no change.
1848 bool AnyChange = false;
1849 for (unsigned i = 0; i != NumOps; ++i) {
1850 if (Ops[i] != N->getOperand(i)) {
1851 AnyChange = true;
1852 break;
1856 // No operands changed, just return the input node.
1857 if (!AnyChange) return InN;
1859 // See if the modified node already exists.
1860 void *InsertPos = 0;
1861 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
1862 return SDOperand(Existing, InN.ResNo);
1864 // Nope it doesn't. Remove the node from it's current place in the maps.
1865 if (InsertPos)
1866 RemoveNodeFromCSEMaps(N);
1868 // Now we update the operands.
1869 for (unsigned i = 0; i != NumOps; ++i) {
1870 if (N->OperandList[i] != Ops[i]) {
1871 N->OperandList[i].Val->removeUser(N);
1872 Ops[i].Val->addUser(N);
1873 N->OperandList[i] = Ops[i];
1877 // If this gets put into a CSE map, add it.
1878 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
1879 return InN;
1885 /// SelectNodeTo - These are used for target selectors to *mutate* the
1886 /// specified node to have the specified return type, Target opcode, and
1887 /// operands. Note that target opcodes are stored as
1888 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1890 /// Note that SelectNodeTo returns the resultant node. If there is already a
1891 /// node of the specified opcode and operands, it returns that node instead of
1892 /// the current one.
1893 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1894 MVT::ValueType VT) {
1895 // If an identical node already exists, use it.
1896 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1897 if (ON) return SDOperand(ON, 0);
1899 RemoveNodeFromCSEMaps(N);
1901 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1902 N->setValueTypes(getNodeValueTypes(VT), 1);
1904 ON = N; // Memoize the new node.
1905 return SDOperand(N, 0);
1908 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1909 MVT::ValueType VT, SDOperand Op1) {
1910 // If an identical node already exists, use it.
1911 MVT::ValueType *VTs = getNodeValueTypes(VT);
1912 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
1913 void *IP = 0;
1914 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1915 return SDOperand(ON, 0);
1917 RemoveNodeFromCSEMaps(N);
1918 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1919 N->setValueTypes(getNodeValueTypes(VT), 1);
1920 N->setOperands(Op1);
1921 CSEMap.InsertNode(N, IP);
1922 return SDOperand(N, 0);
1925 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1926 MVT::ValueType VT, SDOperand Op1,
1927 SDOperand Op2) {
1928 // If an identical node already exists, use it.
1929 MVT::ValueType *VTs = getNodeValueTypes(VT);
1930 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1931 void *IP = 0;
1932 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1933 return SDOperand(ON, 0);
1935 RemoveNodeFromCSEMaps(N);
1936 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1937 N->setValueTypes(VTs, 1);
1938 N->setOperands(Op1, Op2);
1940 CSEMap.InsertNode(N, IP); // Memoize the new node.
1941 return SDOperand(N, 0);
1944 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1945 MVT::ValueType VT, SDOperand Op1,
1946 SDOperand Op2, SDOperand Op3) {
1947 // If an identical node already exists, use it.
1948 MVT::ValueType *VTs = getNodeValueTypes(VT);
1949 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2, Op3);
1950 void *IP = 0;
1951 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1952 return SDOperand(ON, 0);
1954 RemoveNodeFromCSEMaps(N);
1955 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1956 N->setValueTypes(VTs, 1);
1957 N->setOperands(Op1, Op2, Op3);
1959 CSEMap.InsertNode(N, IP); // Memoize the new node.
1960 return SDOperand(N, 0);
1963 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1964 MVT::ValueType VT, SDOperand Op1,
1965 SDOperand Op2, SDOperand Op3,
1966 SDOperand Op4) {
1967 // If an identical node already exists, use it.
1968 MVT::ValueType *VTs = getNodeValueTypes(VT);
1969 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1970 ID.AddOperand(Op1);
1971 ID.AddOperand(Op2);
1972 ID.AddOperand(Op3);
1973 ID.AddOperand(Op4);
1974 void *IP = 0;
1975 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1976 return SDOperand(ON, 0);
1978 RemoveNodeFromCSEMaps(N);
1979 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1980 N->setValueTypes(VTs, 1);
1981 N->setOperands(Op1, Op2, Op3, Op4);
1983 CSEMap.InsertNode(N, IP); // Memoize the new node.
1984 return SDOperand(N, 0);
1987 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1988 MVT::ValueType VT, SDOperand Op1,
1989 SDOperand Op2, SDOperand Op3,
1990 SDOperand Op4, SDOperand Op5) {
1991 MVT::ValueType *VTs = getNodeValueTypes(VT);
1992 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1993 ID.AddOperand(Op1);
1994 ID.AddOperand(Op2);
1995 ID.AddOperand(Op3);
1996 ID.AddOperand(Op4);
1997 ID.AddOperand(Op5);
1998 void *IP = 0;
1999 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2000 return SDOperand(ON, 0);
2002 RemoveNodeFromCSEMaps(N);
2003 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2004 N->setValueTypes(VTs, 1);
2005 N->setOperands(Op1, Op2, Op3, Op4, Op5);
2007 CSEMap.InsertNode(N, IP); // Memoize the new node.
2008 return SDOperand(N, 0);
2011 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2012 MVT::ValueType VT, SDOperand Op1,
2013 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2014 SDOperand Op5, SDOperand Op6) {
2015 MVT::ValueType *VTs = getNodeValueTypes(VT);
2016 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2017 ID.AddOperand(Op1);
2018 ID.AddOperand(Op2);
2019 ID.AddOperand(Op3);
2020 ID.AddOperand(Op4);
2021 ID.AddOperand(Op5);
2022 ID.AddOperand(Op6);
2023 void *IP = 0;
2024 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2025 return SDOperand(ON, 0);
2027 RemoveNodeFromCSEMaps(N);
2028 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2029 N->setValueTypes(VTs, 1);
2030 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
2032 CSEMap.InsertNode(N, IP); // Memoize the new node.
2033 return SDOperand(N, 0);
2036 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2037 MVT::ValueType VT, SDOperand Op1,
2038 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2039 SDOperand Op5, SDOperand Op6,
2040 SDOperand Op7) {
2041 MVT::ValueType *VTs = getNodeValueTypes(VT);
2042 // If an identical node already exists, use it.
2043 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2044 ID.AddOperand(Op1);
2045 ID.AddOperand(Op2);
2046 ID.AddOperand(Op3);
2047 ID.AddOperand(Op4);
2048 ID.AddOperand(Op5);
2049 ID.AddOperand(Op6);
2050 ID.AddOperand(Op7);
2051 void *IP = 0;
2052 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2053 return SDOperand(ON, 0);
2055 RemoveNodeFromCSEMaps(N);
2056 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2057 N->setValueTypes(VTs, 1);
2058 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
2060 CSEMap.InsertNode(N, IP); // Memoize the new node.
2061 return SDOperand(N, 0);
2063 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2064 MVT::ValueType VT, SDOperand Op1,
2065 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2066 SDOperand Op5, SDOperand Op6,
2067 SDOperand Op7, SDOperand Op8) {
2068 // If an identical node already exists, use it.
2069 MVT::ValueType *VTs = getNodeValueTypes(VT);
2070 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2071 ID.AddOperand(Op1);
2072 ID.AddOperand(Op2);
2073 ID.AddOperand(Op3);
2074 ID.AddOperand(Op4);
2075 ID.AddOperand(Op5);
2076 ID.AddOperand(Op6);
2077 ID.AddOperand(Op7);
2078 ID.AddOperand(Op8);
2079 void *IP = 0;
2080 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2081 return SDOperand(ON, 0);
2083 RemoveNodeFromCSEMaps(N);
2084 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2085 N->setValueTypes(VTs, 1);
2086 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2088 CSEMap.InsertNode(N, IP); // Memoize the new node.
2089 return SDOperand(N, 0);
2092 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2093 MVT::ValueType VT1, MVT::ValueType VT2,
2094 SDOperand Op1, SDOperand Op2) {
2095 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2096 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
2097 void *IP = 0;
2098 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2099 return SDOperand(ON, 0);
2101 RemoveNodeFromCSEMaps(N);
2102 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2103 N->setValueTypes(VTs, 2);
2104 N->setOperands(Op1, Op2);
2106 CSEMap.InsertNode(N, IP); // Memoize the new node.
2107 return SDOperand(N, 0);
2110 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2111 MVT::ValueType VT1, MVT::ValueType VT2,
2112 SDOperand Op1, SDOperand Op2,
2113 SDOperand Op3) {
2114 // If an identical node already exists, use it.
2115 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2116 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
2117 Op1, Op2, Op3);
2118 void *IP = 0;
2119 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2120 return SDOperand(ON, 0);
2122 RemoveNodeFromCSEMaps(N);
2123 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2124 N->setValueTypes(VTs, 2);
2125 N->setOperands(Op1, Op2, Op3);
2127 CSEMap.InsertNode(N, IP); // Memoize the new node.
2128 return SDOperand(N, 0);
2131 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2132 MVT::ValueType VT1, MVT::ValueType VT2,
2133 SDOperand Op1, SDOperand Op2,
2134 SDOperand Op3, SDOperand Op4) {
2135 // If an identical node already exists, use it.
2136 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2137 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2138 ID.AddOperand(Op1);
2139 ID.AddOperand(Op2);
2140 ID.AddOperand(Op3);
2141 ID.AddOperand(Op4);
2142 void *IP = 0;
2143 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2144 return SDOperand(ON, 0);
2146 RemoveNodeFromCSEMaps(N);
2147 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2148 N->setValueTypes(VTs, 2);
2149 N->setOperands(Op1, Op2, Op3, Op4);
2151 CSEMap.InsertNode(N, IP); // Memoize the new node.
2152 return SDOperand(N, 0);
2155 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2156 MVT::ValueType VT1, MVT::ValueType VT2,
2157 SDOperand Op1, SDOperand Op2,
2158 SDOperand Op3, SDOperand Op4,
2159 SDOperand Op5) {
2160 // If an identical node already exists, use it.
2161 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2162 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2163 ID.AddOperand(Op1);
2164 ID.AddOperand(Op2);
2165 ID.AddOperand(Op3);
2166 ID.AddOperand(Op4);
2167 ID.AddOperand(Op5);
2168 void *IP = 0;
2169 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2170 return SDOperand(ON, 0);
2172 RemoveNodeFromCSEMaps(N);
2173 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2174 N->setValueTypes(VTs, 2);
2175 N->setOperands(Op1, Op2, Op3, Op4, Op5);
2177 CSEMap.InsertNode(N, IP); // Memoize the new node.
2178 return SDOperand(N, 0);
2181 /// getTargetNode - These are used for target selectors to create a new node
2182 /// with specified return type(s), target opcode, and operands.
2184 /// Note that getTargetNode returns the resultant node. If there is already a
2185 /// node of the specified opcode and operands, it returns that node instead of
2186 /// the current one.
2187 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2188 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2190 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2191 SDOperand Op1) {
2192 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2194 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2195 SDOperand Op1, SDOperand Op2) {
2196 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2198 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2199 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2200 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2202 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2203 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2204 SDOperand Op4) {
2205 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2207 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2208 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2209 SDOperand Op4, SDOperand Op5) {
2210 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2212 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2213 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2214 SDOperand Op4, SDOperand Op5,
2215 SDOperand Op6) {
2216 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
2217 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 6).Val;
2219 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2220 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2221 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2222 SDOperand Op7) {
2223 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
2224 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 7).Val;
2226 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2227 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2228 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2229 SDOperand Op7, SDOperand Op8) {
2230 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8 };
2231 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 8).Val;
2233 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2234 const SDOperand *Ops, unsigned NumOps) {
2235 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
2237 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2238 MVT::ValueType VT2, SDOperand Op1) {
2239 std::vector<MVT::ValueType> ResultTys;
2240 ResultTys.push_back(VT1);
2241 ResultTys.push_back(VT2);
2242 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, &Op1, 1).Val;
2244 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2245 MVT::ValueType VT2, SDOperand Op1,
2246 SDOperand Op2) {
2247 std::vector<MVT::ValueType> ResultTys;
2248 ResultTys.push_back(VT1);
2249 ResultTys.push_back(VT2);
2250 SDOperand Ops[] = { Op1, Op2 };
2251 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 2).Val;
2253 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2254 MVT::ValueType VT2, SDOperand Op1,
2255 SDOperand Op2, SDOperand Op3) {
2256 std::vector<MVT::ValueType> ResultTys;
2257 ResultTys.push_back(VT1);
2258 ResultTys.push_back(VT2);
2259 SDOperand Ops[] = { Op1, Op2, Op3 };
2260 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 3).Val;
2262 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2263 MVT::ValueType VT2, SDOperand Op1,
2264 SDOperand Op2, SDOperand Op3,
2265 SDOperand Op4) {
2266 std::vector<MVT::ValueType> ResultTys;
2267 ResultTys.push_back(VT1);
2268 ResultTys.push_back(VT2);
2269 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2270 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 4).Val;
2272 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2273 MVT::ValueType VT2, SDOperand Op1,
2274 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2275 SDOperand Op5) {
2276 std::vector<MVT::ValueType> ResultTys;
2277 ResultTys.push_back(VT1);
2278 ResultTys.push_back(VT2);
2279 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2280 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 5).Val;
2282 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2283 MVT::ValueType VT2, SDOperand Op1,
2284 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2285 SDOperand Op5, SDOperand Op6) {
2286 std::vector<MVT::ValueType> ResultTys;
2287 ResultTys.push_back(VT1);
2288 ResultTys.push_back(VT2);
2289 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
2290 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 6).Val;
2292 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2293 MVT::ValueType VT2, SDOperand Op1,
2294 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2295 SDOperand Op5, SDOperand Op6,
2296 SDOperand Op7) {
2297 std::vector<MVT::ValueType> ResultTys;
2298 ResultTys.push_back(VT1);
2299 ResultTys.push_back(VT2);
2300 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
2301 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 7).Val;
2303 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2304 MVT::ValueType VT2, MVT::ValueType VT3,
2305 SDOperand Op1, SDOperand Op2) {
2306 std::vector<MVT::ValueType> ResultTys;
2307 ResultTys.push_back(VT1);
2308 ResultTys.push_back(VT2);
2309 ResultTys.push_back(VT3);
2310 SDOperand Ops[] = { Op1, Op2 };
2311 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 2).Val;
2313 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2314 MVT::ValueType VT2, MVT::ValueType VT3,
2315 SDOperand Op1, SDOperand Op2,
2316 SDOperand Op3, SDOperand Op4,
2317 SDOperand Op5) {
2318 std::vector<MVT::ValueType> ResultTys;
2319 ResultTys.push_back(VT1);
2320 ResultTys.push_back(VT2);
2321 ResultTys.push_back(VT3);
2322 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2323 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 5).Val;
2325 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2326 MVT::ValueType VT2, MVT::ValueType VT3,
2327 SDOperand Op1, SDOperand Op2,
2328 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2329 SDOperand Op6) {
2330 std::vector<MVT::ValueType> ResultTys;
2331 ResultTys.push_back(VT1);
2332 ResultTys.push_back(VT2);
2333 ResultTys.push_back(VT3);
2334 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
2335 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 6).Val;
2337 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2338 MVT::ValueType VT2, MVT::ValueType VT3,
2339 SDOperand Op1, SDOperand Op2,
2340 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2341 SDOperand Op6, SDOperand Op7) {
2342 std::vector<MVT::ValueType> ResultTys;
2343 ResultTys.push_back(VT1);
2344 ResultTys.push_back(VT2);
2345 ResultTys.push_back(VT3);
2346 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
2347 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 7).Val;
2349 SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2350 MVT::ValueType VT2,
2351 const SDOperand *Ops, unsigned NumOps) {
2352 std::vector<MVT::ValueType> ResultTys;
2353 ResultTys.push_back(VT1);
2354 ResultTys.push_back(VT2);
2355 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, NumOps).Val;
2358 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2359 /// This can cause recursive merging of nodes in the DAG.
2361 /// This version assumes From/To have a single result value.
2363 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2364 std::vector<SDNode*> *Deleted) {
2365 SDNode *From = FromN.Val, *To = ToN.Val;
2366 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2367 "Cannot replace with this method!");
2368 assert(From != To && "Cannot replace uses of with self");
2370 while (!From->use_empty()) {
2371 // Process users until they are all gone.
2372 SDNode *U = *From->use_begin();
2374 // This node is about to morph, remove its old self from the CSE maps.
2375 RemoveNodeFromCSEMaps(U);
2377 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2378 I != E; ++I)
2379 if (I->Val == From) {
2380 From->removeUser(U);
2381 I->Val = To;
2382 To->addUser(U);
2385 // Now that we have modified U, add it back to the CSE maps. If it already
2386 // exists there, recursively merge the results together.
2387 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2388 ReplaceAllUsesWith(U, Existing, Deleted);
2389 // U is now dead.
2390 if (Deleted) Deleted->push_back(U);
2391 DeleteNodeNotInCSEMaps(U);
2396 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2397 /// This can cause recursive merging of nodes in the DAG.
2399 /// This version assumes From/To have matching types and numbers of result
2400 /// values.
2402 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2403 std::vector<SDNode*> *Deleted) {
2404 assert(From != To && "Cannot replace uses of with self");
2405 assert(From->getNumValues() == To->getNumValues() &&
2406 "Cannot use this version of ReplaceAllUsesWith!");
2407 if (From->getNumValues() == 1) { // If possible, use the faster version.
2408 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
2409 return;
2412 while (!From->use_empty()) {
2413 // Process users until they are all gone.
2414 SDNode *U = *From->use_begin();
2416 // This node is about to morph, remove its old self from the CSE maps.
2417 RemoveNodeFromCSEMaps(U);
2419 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2420 I != E; ++I)
2421 if (I->Val == From) {
2422 From->removeUser(U);
2423 I->Val = To;
2424 To->addUser(U);
2427 // Now that we have modified U, add it back to the CSE maps. If it already
2428 // exists there, recursively merge the results together.
2429 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2430 ReplaceAllUsesWith(U, Existing, Deleted);
2431 // U is now dead.
2432 if (Deleted) Deleted->push_back(U);
2433 DeleteNodeNotInCSEMaps(U);
2438 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2439 /// This can cause recursive merging of nodes in the DAG.
2441 /// This version can replace From with any result values. To must match the
2442 /// number and types of values returned by From.
2443 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
2444 const std::vector<SDOperand> &To,
2445 std::vector<SDNode*> *Deleted) {
2446 assert(From->getNumValues() == To.size() &&
2447 "Incorrect number of values to replace with!");
2448 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
2449 // Degenerate case handled above.
2450 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
2451 return;
2454 while (!From->use_empty()) {
2455 // Process users until they are all gone.
2456 SDNode *U = *From->use_begin();
2458 // This node is about to morph, remove its old self from the CSE maps.
2459 RemoveNodeFromCSEMaps(U);
2461 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2462 I != E; ++I)
2463 if (I->Val == From) {
2464 const SDOperand &ToOp = To[I->ResNo];
2465 From->removeUser(U);
2466 *I = ToOp;
2467 ToOp.Val->addUser(U);
2470 // Now that we have modified U, add it back to the CSE maps. If it already
2471 // exists there, recursively merge the results together.
2472 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2473 ReplaceAllUsesWith(U, Existing, Deleted);
2474 // U is now dead.
2475 if (Deleted) Deleted->push_back(U);
2476 DeleteNodeNotInCSEMaps(U);
2481 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2482 /// uses of other values produced by From.Val alone. The Deleted vector is
2483 /// handled the same was as for ReplaceAllUsesWith.
2484 void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2485 std::vector<SDNode*> &Deleted) {
2486 assert(From != To && "Cannot replace a value with itself");
2487 // Handle the simple, trivial, case efficiently.
2488 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2489 ReplaceAllUsesWith(From, To, &Deleted);
2490 return;
2493 // Get all of the users in a nice, deterministically ordered, uniqued set.
2494 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2496 while (!Users.empty()) {
2497 // We know that this user uses some value of From. If it is the right
2498 // value, update it.
2499 SDNode *User = Users.back();
2500 Users.pop_back();
2502 for (SDOperand *Op = User->OperandList,
2503 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2504 if (*Op == From) {
2505 // Okay, we know this user needs to be updated. Remove its old self
2506 // from the CSE maps.
2507 RemoveNodeFromCSEMaps(User);
2509 // Update all operands that match "From".
2510 for (; Op != E; ++Op) {
2511 if (*Op == From) {
2512 From.Val->removeUser(User);
2513 *Op = To;
2514 To.Val->addUser(User);
2518 // Now that we have modified User, add it back to the CSE maps. If it
2519 // already exists there, recursively merge the results together.
2520 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2521 unsigned NumDeleted = Deleted.size();
2522 ReplaceAllUsesWith(User, Existing, &Deleted);
2524 // User is now dead.
2525 Deleted.push_back(User);
2526 DeleteNodeNotInCSEMaps(User);
2528 // We have to be careful here, because ReplaceAllUsesWith could have
2529 // deleted a user of From, which means there may be dangling pointers
2530 // in the "Users" setvector. Scan over the deleted node pointers and
2531 // remove them from the setvector.
2532 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2533 Users.remove(Deleted[i]);
2535 break; // Exit the operand scanning loop.
2542 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2543 /// their allnodes order. It returns the maximum id.
2544 unsigned SelectionDAG::AssignNodeIds() {
2545 unsigned Id = 0;
2546 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2547 SDNode *N = I;
2548 N->setNodeId(Id++);
2550 return Id;
2553 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
2554 /// based on their topological order. It returns the maximum id and a vector
2555 /// of the SDNodes* in assigned order by reference.
2556 unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
2557 unsigned DAGSize = AllNodes.size();
2558 std::vector<unsigned> InDegree(DAGSize);
2559 std::vector<SDNode*> Sources;
2561 // Use a two pass approach to avoid using a std::map which is slow.
2562 unsigned Id = 0;
2563 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2564 SDNode *N = I;
2565 N->setNodeId(Id++);
2566 unsigned Degree = N->use_size();
2567 InDegree[N->getNodeId()] = Degree;
2568 if (Degree == 0)
2569 Sources.push_back(N);
2572 TopOrder.clear();
2573 while (!Sources.empty()) {
2574 SDNode *N = Sources.back();
2575 Sources.pop_back();
2576 TopOrder.push_back(N);
2577 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2578 SDNode *P = I->Val;
2579 unsigned Degree = --InDegree[P->getNodeId()];
2580 if (Degree == 0)
2581 Sources.push_back(P);
2585 // Second pass, assign the actual topological order as node ids.
2586 Id = 0;
2587 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2588 TI != TE; ++TI)
2589 (*TI)->setNodeId(Id++);
2591 return Id;
2596 //===----------------------------------------------------------------------===//
2597 // SDNode Class
2598 //===----------------------------------------------------------------------===//
2600 // Out-of-line virtual method to give class a home.
2601 void SDNode::ANCHOR() {
2604 /// getValueTypeList - Return a pointer to the specified value type.
2606 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2607 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2608 VTs[VT] = VT;
2609 return &VTs[VT];
2612 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2613 /// indicated value. This method ignores uses of other values defined by this
2614 /// operation.
2615 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
2616 assert(Value < getNumValues() && "Bad value!");
2618 // If there is only one value, this is easy.
2619 if (getNumValues() == 1)
2620 return use_size() == NUses;
2621 if (Uses.size() < NUses) return false;
2623 SDOperand TheValue(const_cast<SDNode *>(this), Value);
2625 std::set<SDNode*> UsersHandled;
2627 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
2628 UI != E; ++UI) {
2629 SDNode *User = *UI;
2630 if (User->getNumOperands() == 1 ||
2631 UsersHandled.insert(User).second) // First time we've seen this?
2632 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2633 if (User->getOperand(i) == TheValue) {
2634 if (NUses == 0)
2635 return false; // too many uses
2636 --NUses;
2640 // Found exactly the right number of uses?
2641 return NUses == 0;
2645 // isOnlyUse - Return true if this node is the only use of N.
2646 bool SDNode::isOnlyUse(SDNode *N) const {
2647 bool Seen = false;
2648 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2649 SDNode *User = *I;
2650 if (User == this)
2651 Seen = true;
2652 else
2653 return false;
2656 return Seen;
2659 // isOperand - Return true if this node is an operand of N.
2660 bool SDOperand::isOperand(SDNode *N) const {
2661 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2662 if (*this == N->getOperand(i))
2663 return true;
2664 return false;
2667 bool SDNode::isOperand(SDNode *N) const {
2668 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2669 if (this == N->OperandList[i].Val)
2670 return true;
2671 return false;
2674 const char *SDNode::getOperationName(const SelectionDAG *G) const {
2675 switch (getOpcode()) {
2676 default:
2677 if (getOpcode() < ISD::BUILTIN_OP_END)
2678 return "<<Unknown DAG Node>>";
2679 else {
2680 if (G) {
2681 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
2682 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2683 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
2685 TargetLowering &TLI = G->getTargetLoweringInfo();
2686 const char *Name =
2687 TLI.getTargetNodeName(getOpcode());
2688 if (Name) return Name;
2691 return "<<Unknown Target Node>>";
2694 case ISD::PCMARKER: return "PCMarker";
2695 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
2696 case ISD::SRCVALUE: return "SrcValue";
2697 case ISD::EntryToken: return "EntryToken";
2698 case ISD::TokenFactor: return "TokenFactor";
2699 case ISD::AssertSext: return "AssertSext";
2700 case ISD::AssertZext: return "AssertZext";
2702 case ISD::STRING: return "String";
2703 case ISD::BasicBlock: return "BasicBlock";
2704 case ISD::VALUETYPE: return "ValueType";
2705 case ISD::Register: return "Register";
2707 case ISD::Constant: return "Constant";
2708 case ISD::ConstantFP: return "ConstantFP";
2709 case ISD::GlobalAddress: return "GlobalAddress";
2710 case ISD::FrameIndex: return "FrameIndex";
2711 case ISD::JumpTable: return "JumpTable";
2712 case ISD::ConstantPool: return "ConstantPool";
2713 case ISD::ExternalSymbol: return "ExternalSymbol";
2714 case ISD::INTRINSIC_WO_CHAIN: {
2715 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2716 return Intrinsic::getName((Intrinsic::ID)IID);
2718 case ISD::INTRINSIC_VOID:
2719 case ISD::INTRINSIC_W_CHAIN: {
2720 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
2721 return Intrinsic::getName((Intrinsic::ID)IID);
2724 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
2725 case ISD::TargetConstant: return "TargetConstant";
2726 case ISD::TargetConstantFP:return "TargetConstantFP";
2727 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2728 case ISD::TargetFrameIndex: return "TargetFrameIndex";
2729 case ISD::TargetJumpTable: return "TargetJumpTable";
2730 case ISD::TargetConstantPool: return "TargetConstantPool";
2731 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
2733 case ISD::CopyToReg: return "CopyToReg";
2734 case ISD::CopyFromReg: return "CopyFromReg";
2735 case ISD::UNDEF: return "undef";
2736 case ISD::MERGE_VALUES: return "mergevalues";
2737 case ISD::INLINEASM: return "inlineasm";
2738 case ISD::HANDLENODE: return "handlenode";
2739 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
2740 case ISD::CALL: return "call";
2742 // Unary operators
2743 case ISD::FABS: return "fabs";
2744 case ISD::FNEG: return "fneg";
2745 case ISD::FSQRT: return "fsqrt";
2746 case ISD::FSIN: return "fsin";
2747 case ISD::FCOS: return "fcos";
2749 // Binary operators
2750 case ISD::ADD: return "add";
2751 case ISD::SUB: return "sub";
2752 case ISD::MUL: return "mul";
2753 case ISD::MULHU: return "mulhu";
2754 case ISD::MULHS: return "mulhs";
2755 case ISD::SDIV: return "sdiv";
2756 case ISD::UDIV: return "udiv";
2757 case ISD::SREM: return "srem";
2758 case ISD::UREM: return "urem";
2759 case ISD::AND: return "and";
2760 case ISD::OR: return "or";
2761 case ISD::XOR: return "xor";
2762 case ISD::SHL: return "shl";
2763 case ISD::SRA: return "sra";
2764 case ISD::SRL: return "srl";
2765 case ISD::ROTL: return "rotl";
2766 case ISD::ROTR: return "rotr";
2767 case ISD::FADD: return "fadd";
2768 case ISD::FSUB: return "fsub";
2769 case ISD::FMUL: return "fmul";
2770 case ISD::FDIV: return "fdiv";
2771 case ISD::FREM: return "frem";
2772 case ISD::FCOPYSIGN: return "fcopysign";
2773 case ISD::VADD: return "vadd";
2774 case ISD::VSUB: return "vsub";
2775 case ISD::VMUL: return "vmul";
2776 case ISD::VSDIV: return "vsdiv";
2777 case ISD::VUDIV: return "vudiv";
2778 case ISD::VAND: return "vand";
2779 case ISD::VOR: return "vor";
2780 case ISD::VXOR: return "vxor";
2782 case ISD::SETCC: return "setcc";
2783 case ISD::SELECT: return "select";
2784 case ISD::SELECT_CC: return "select_cc";
2785 case ISD::VSELECT: return "vselect";
2786 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2787 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2788 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
2789 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
2790 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2791 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2792 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2793 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2794 case ISD::VBIT_CONVERT: return "vbit_convert";
2795 case ISD::ADDC: return "addc";
2796 case ISD::ADDE: return "adde";
2797 case ISD::SUBC: return "subc";
2798 case ISD::SUBE: return "sube";
2799 case ISD::SHL_PARTS: return "shl_parts";
2800 case ISD::SRA_PARTS: return "sra_parts";
2801 case ISD::SRL_PARTS: return "srl_parts";
2803 // Conversion operators.
2804 case ISD::SIGN_EXTEND: return "sign_extend";
2805 case ISD::ZERO_EXTEND: return "zero_extend";
2806 case ISD::ANY_EXTEND: return "any_extend";
2807 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
2808 case ISD::TRUNCATE: return "truncate";
2809 case ISD::FP_ROUND: return "fp_round";
2810 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
2811 case ISD::FP_EXTEND: return "fp_extend";
2813 case ISD::SINT_TO_FP: return "sint_to_fp";
2814 case ISD::UINT_TO_FP: return "uint_to_fp";
2815 case ISD::FP_TO_SINT: return "fp_to_sint";
2816 case ISD::FP_TO_UINT: return "fp_to_uint";
2817 case ISD::BIT_CONVERT: return "bit_convert";
2819 // Control flow instructions
2820 case ISD::BR: return "br";
2821 case ISD::BRIND: return "brind";
2822 case ISD::BRCOND: return "brcond";
2823 case ISD::BR_CC: return "br_cc";
2824 case ISD::RET: return "ret";
2825 case ISD::CALLSEQ_START: return "callseq_start";
2826 case ISD::CALLSEQ_END: return "callseq_end";
2828 // Other operators
2829 case ISD::LOAD: return "load";
2830 case ISD::STORE: return "store";
2831 case ISD::VLOAD: return "vload";
2832 case ISD::EXTLOAD: return "extload";
2833 case ISD::SEXTLOAD: return "sextload";
2834 case ISD::ZEXTLOAD: return "zextload";
2835 case ISD::TRUNCSTORE: return "truncstore";
2836 case ISD::VAARG: return "vaarg";
2837 case ISD::VACOPY: return "vacopy";
2838 case ISD::VAEND: return "vaend";
2839 case ISD::VASTART: return "vastart";
2840 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2841 case ISD::EXTRACT_ELEMENT: return "extract_element";
2842 case ISD::BUILD_PAIR: return "build_pair";
2843 case ISD::STACKSAVE: return "stacksave";
2844 case ISD::STACKRESTORE: return "stackrestore";
2846 // Block memory operations.
2847 case ISD::MEMSET: return "memset";
2848 case ISD::MEMCPY: return "memcpy";
2849 case ISD::MEMMOVE: return "memmove";
2851 // Bit manipulation
2852 case ISD::BSWAP: return "bswap";
2853 case ISD::CTPOP: return "ctpop";
2854 case ISD::CTTZ: return "cttz";
2855 case ISD::CTLZ: return "ctlz";
2857 // Debug info
2858 case ISD::LOCATION: return "location";
2859 case ISD::DEBUG_LOC: return "debug_loc";
2860 case ISD::DEBUG_LABEL: return "debug_label";
2862 case ISD::CONDCODE:
2863 switch (cast<CondCodeSDNode>(this)->get()) {
2864 default: assert(0 && "Unknown setcc condition!");
2865 case ISD::SETOEQ: return "setoeq";
2866 case ISD::SETOGT: return "setogt";
2867 case ISD::SETOGE: return "setoge";
2868 case ISD::SETOLT: return "setolt";
2869 case ISD::SETOLE: return "setole";
2870 case ISD::SETONE: return "setone";
2872 case ISD::SETO: return "seto";
2873 case ISD::SETUO: return "setuo";
2874 case ISD::SETUEQ: return "setue";
2875 case ISD::SETUGT: return "setugt";
2876 case ISD::SETUGE: return "setuge";
2877 case ISD::SETULT: return "setult";
2878 case ISD::SETULE: return "setule";
2879 case ISD::SETUNE: return "setune";
2881 case ISD::SETEQ: return "seteq";
2882 case ISD::SETGT: return "setgt";
2883 case ISD::SETGE: return "setge";
2884 case ISD::SETLT: return "setlt";
2885 case ISD::SETLE: return "setle";
2886 case ISD::SETNE: return "setne";
2891 void SDNode::dump() const { dump(0); }
2892 void SDNode::dump(const SelectionDAG *G) const {
2893 std::cerr << (void*)this << ": ";
2895 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2896 if (i) std::cerr << ",";
2897 if (getValueType(i) == MVT::Other)
2898 std::cerr << "ch";
2899 else
2900 std::cerr << MVT::getValueTypeString(getValueType(i));
2902 std::cerr << " = " << getOperationName(G);
2904 std::cerr << " ";
2905 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2906 if (i) std::cerr << ", ";
2907 std::cerr << (void*)getOperand(i).Val;
2908 if (unsigned RN = getOperand(i).ResNo)
2909 std::cerr << ":" << RN;
2912 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2913 std::cerr << "<" << CSDN->getValue() << ">";
2914 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2915 std::cerr << "<" << CSDN->getValue() << ">";
2916 } else if (const GlobalAddressSDNode *GADN =
2917 dyn_cast<GlobalAddressSDNode>(this)) {
2918 int offset = GADN->getOffset();
2919 std::cerr << "<";
2920 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
2921 if (offset > 0)
2922 std::cerr << " + " << offset;
2923 else
2924 std::cerr << " " << offset;
2925 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
2926 std::cerr << "<" << FIDN->getIndex() << ">";
2927 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
2928 int offset = CP->getOffset();
2929 std::cerr << "<" << *CP->get() << ">";
2930 if (offset > 0)
2931 std::cerr << " + " << offset;
2932 else
2933 std::cerr << " " << offset;
2934 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
2935 std::cerr << "<";
2936 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2937 if (LBB)
2938 std::cerr << LBB->getName() << " ";
2939 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
2940 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
2941 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2942 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2943 } else {
2944 std::cerr << " #" << R->getReg();
2946 } else if (const ExternalSymbolSDNode *ES =
2947 dyn_cast<ExternalSymbolSDNode>(this)) {
2948 std::cerr << "'" << ES->getSymbol() << "'";
2949 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2950 if (M->getValue())
2951 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2952 else
2953 std::cerr << "<null:" << M->getOffset() << ">";
2954 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2955 std::cerr << ":" << getValueTypeString(N->getVT());
2959 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
2960 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2961 if (N->getOperand(i).Val->hasOneUse())
2962 DumpNodes(N->getOperand(i).Val, indent+2, G);
2963 else
2964 std::cerr << "\n" << std::string(indent+2, ' ')
2965 << (void*)N->getOperand(i).Val << ": <multiple use>";
2968 std::cerr << "\n" << std::string(indent, ' ');
2969 N->dump(G);
2972 void SelectionDAG::dump() const {
2973 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
2974 std::vector<const SDNode*> Nodes;
2975 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2976 I != E; ++I)
2977 Nodes.push_back(I);
2979 std::sort(Nodes.begin(), Nodes.end());
2981 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2982 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
2983 DumpNodes(Nodes[i], 2, this);
2986 DumpNodes(getRoot().Val, 2, this);
2988 std::cerr << "\n\n";
2991 /// InsertISelMapEntry - A helper function to insert a key / element pair
2992 /// into a SDOperand to SDOperand map. This is added to avoid the map
2993 /// insertion operator from being inlined.
2994 void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
2995 SDNode *Key, unsigned KeyResNo,
2996 SDNode *Element, unsigned ElementResNo) {
2997 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
2998 SDOperand(Element, ElementResNo)));