ed96bd61ae2b7c25efade7047660e0a371a7ef37
[oota-llvm.git] / lib / CodeGen / SelectionDAG / SelectionDAG.cpp
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.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/CodeGen/SelectionDAG.h"
15 #include "llvm/Constants.h"
16 #include "llvm/GlobalValue.h"
17 #include "llvm/Assembly/Writer.h"
18 #include "llvm/CodeGen/MachineBasicBlock.h"
19 #include "llvm/Support/MathExtras.h"
20 #include "llvm/Target/MRegisterInfo.h"
21 #include "llvm/Target/TargetLowering.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include <iostream>
26 #include <set>
27 #include <cmath>
28 #include <algorithm>
29 using namespace llvm;
30
31 static bool isCommutativeBinOp(unsigned Opcode) {
32   switch (Opcode) {
33   case ISD::ADD:
34   case ISD::MUL:
35   case ISD::MULHU:
36   case ISD::MULHS:
37   case ISD::FADD:
38   case ISD::FMUL:
39   case ISD::AND:
40   case ISD::OR:
41   case ISD::XOR: return true;
42   default: return false; // FIXME: Need commutative info for user ops!
43   }
44 }
45
46 static bool isAssociativeBinOp(unsigned Opcode) {
47   switch (Opcode) {
48   case ISD::ADD:
49   case ISD::MUL:
50   case ISD::AND:
51   case ISD::OR:
52   case ISD::XOR: return true;
53   default: return false; // FIXME: Need associative info for user ops!
54   }
55 }
56
57 // isInvertibleForFree - Return true if there is no cost to emitting the logical
58 // inverse of this node.
59 static bool isInvertibleForFree(SDOperand N) {
60   if (isa<ConstantSDNode>(N.Val)) return true;
61   if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
62     return true;
63   return false;
64 }
65
66 //===----------------------------------------------------------------------===//
67 //                              ConstantFPSDNode Class
68 //===----------------------------------------------------------------------===//
69
70 /// isExactlyValue - We don't rely on operator== working on double values, as
71 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
72 /// As such, this method can be used to do an exact bit-for-bit comparison of
73 /// two floating point values.
74 bool ConstantFPSDNode::isExactlyValue(double V) const {
75   return DoubleToBits(V) == DoubleToBits(Value);
76 }
77
78 //===----------------------------------------------------------------------===//
79 //                              ISD Class
80 //===----------------------------------------------------------------------===//
81
82 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
83 /// when given the operation for (X op Y).
84 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
85   // To perform this operation, we just need to swap the L and G bits of the
86   // operation.
87   unsigned OldL = (Operation >> 2) & 1;
88   unsigned OldG = (Operation >> 1) & 1;
89   return ISD::CondCode((Operation & ~6) |  // Keep the N, U, E bits
90                        (OldL << 1) |       // New G bit
91                        (OldG << 2));        // New L bit.
92 }
93
94 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
95 /// 'op' is a valid SetCC operation.
96 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
97   unsigned Operation = Op;
98   if (isInteger)
99     Operation ^= 7;   // Flip L, G, E bits, but not U.
100   else
101     Operation ^= 15;  // Flip all of the condition bits.
102   if (Operation > ISD::SETTRUE2)
103     Operation &= ~8;     // Don't let N and U bits get set.
104   return ISD::CondCode(Operation);
105 }
106
107
108 /// isSignedOp - For an integer comparison, return 1 if the comparison is a
109 /// signed operation and 2 if the result is an unsigned comparison.  Return zero
110 /// if the operation does not depend on the sign of the input (setne and seteq).
111 static int isSignedOp(ISD::CondCode Opcode) {
112   switch (Opcode) {
113   default: assert(0 && "Illegal integer setcc operation!");
114   case ISD::SETEQ:
115   case ISD::SETNE: return 0;
116   case ISD::SETLT:
117   case ISD::SETLE:
118   case ISD::SETGT:
119   case ISD::SETGE: return 1;
120   case ISD::SETULT:
121   case ISD::SETULE:
122   case ISD::SETUGT:
123   case ISD::SETUGE: return 2;
124   }
125 }
126
127 /// getSetCCOrOperation - Return the result of a logical OR between different
128 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)).  This function
129 /// returns SETCC_INVALID if it is not possible to represent the resultant
130 /// comparison.
131 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
132                                        bool isInteger) {
133   if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
134     // Cannot fold a signed integer setcc with an unsigned integer setcc.
135     return ISD::SETCC_INVALID;
136
137   unsigned Op = Op1 | Op2;  // Combine all of the condition bits.
138
139   // If the N and U bits get set then the resultant comparison DOES suddenly
140   // care about orderedness, and is true when ordered.
141   if (Op > ISD::SETTRUE2)
142     Op &= ~16;     // Clear the N bit.
143   return ISD::CondCode(Op);
144 }
145
146 /// getSetCCAndOperation - Return the result of a logical AND between different
147 /// comparisons of identical values: ((X op1 Y) & (X op2 Y)).  This
148 /// function returns zero if it is not possible to represent the resultant
149 /// comparison.
150 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
151                                         bool isInteger) {
152   if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
153     // Cannot fold a signed setcc with an unsigned setcc.
154     return ISD::SETCC_INVALID;
155
156   // Combine all of the condition bits.
157   return ISD::CondCode(Op1 & Op2);
158 }
159
160 const TargetMachine &SelectionDAG::getTarget() const {
161   return TLI.getTargetMachine();
162 }
163
164 //===----------------------------------------------------------------------===//
165 //                              SelectionDAG Class
166 //===----------------------------------------------------------------------===//
167
168 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
169 /// SelectionDAG, including nodes (like loads) that have uses of their token
170 /// chain but no other uses and no side effect.  If a node is passed in as an
171 /// argument, it is used as the seed for node deletion.
172 void SelectionDAG::RemoveDeadNodes(SDNode *N) {
173   // Create a dummy node (which is not added to allnodes), that adds a reference
174   // to the root node, preventing it from being deleted.
175   HandleSDNode Dummy(getRoot());
176
177   bool MadeChange = false;
178   
179   // If we have a hint to start from, use it.
180   if (N && N->use_empty()) {
181     DestroyDeadNode(N);
182     MadeChange = true;
183   }
184
185   for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
186     if (I->use_empty() && I->getOpcode() != 65535) {
187       // Node is dead, recursively delete newly dead uses.
188       DestroyDeadNode(I);
189       MadeChange = true;
190     }
191   
192   // Walk the nodes list, removing the nodes we've marked as dead.
193   if (MadeChange) {
194     for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
195       SDNode *N = I++;
196       if (N->use_empty())
197         AllNodes.erase(N);
198     }
199   }
200   
201   // If the root changed (e.g. it was a dead load, update the root).
202   setRoot(Dummy.getValue());
203 }
204
205 /// DestroyDeadNode - We know that N is dead.  Nuke it from the CSE maps for the
206 /// graph.  If it is the last user of any of its operands, recursively process
207 /// them the same way.
208 /// 
209 void SelectionDAG::DestroyDeadNode(SDNode *N) {
210   // Okay, we really are going to delete this node.  First take this out of the
211   // appropriate CSE map.
212   RemoveNodeFromCSEMaps(N);
213   
214   // Next, brutally remove the operand list.  This is safe to do, as there are
215   // no cycles in the graph.
216   for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
217     SDNode *O = I->Val;
218     O->removeUser(N);
219     
220     // Now that we removed this operand, see if there are no uses of it left.
221     if (O->use_empty())
222       DestroyDeadNode(O);
223   }
224   delete[] N->OperandList;
225   N->OperandList = 0;
226   N->NumOperands = 0;
227
228   // Mark the node as dead.
229   N->MorphNodeTo(65535);
230 }
231
232 void SelectionDAG::DeleteNode(SDNode *N) {
233   assert(N->use_empty() && "Cannot delete a node that is not dead!");
234
235   // First take this out of the appropriate CSE map.
236   RemoveNodeFromCSEMaps(N);
237
238   // Finally, remove uses due to operands of this node, remove from the 
239   // AllNodes list, and delete the node.
240   DeleteNodeNotInCSEMaps(N);
241 }
242
243 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
244
245   // Remove it from the AllNodes list.
246   AllNodes.remove(N);
247     
248   // Drop all of the operands and decrement used nodes use counts.
249   for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
250     I->Val->removeUser(N);
251   delete[] N->OperandList;
252   N->OperandList = 0;
253   N->NumOperands = 0;
254   
255   delete N;
256 }
257
258 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
259 /// correspond to it.  This is useful when we're about to delete or repurpose
260 /// the node.  We don't want future request for structurally identical nodes
261 /// to return N anymore.
262 void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
263   bool Erased = false;
264   switch (N->getOpcode()) {
265   case ISD::HANDLENODE: return;  // noop.
266   case ISD::Constant:
267     Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
268                                             N->getValueType(0)));
269     break;
270   case ISD::TargetConstant:
271     Erased = TargetConstants.erase(std::make_pair(
272                                     cast<ConstantSDNode>(N)->getValue(),
273                                                   N->getValueType(0)));
274     break;
275   case ISD::ConstantFP: {
276     uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
277     Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
278     break;
279   }
280   case ISD::STRING:
281     Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
282     break;
283   case ISD::CONDCODE:
284     assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
285            "Cond code doesn't exist!");
286     Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
287     CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
288     break;
289   case ISD::GlobalAddress: {
290     GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
291     Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
292                                                GN->getOffset()));
293     break;
294   }
295   case ISD::TargetGlobalAddress: {
296     GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
297     Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
298                                                     GN->getOffset()));
299     break;
300   }
301   case ISD::FrameIndex:
302     Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
303     break;
304   case ISD::TargetFrameIndex:
305     Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
306     break;
307   case ISD::ConstantPool:
308     Erased = ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
309     break;
310   case ISD::TargetConstantPool:
311     Erased =TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
312     break;
313   case ISD::BasicBlock:
314     Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
315     break;
316   case ISD::ExternalSymbol:
317     Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
318     break;
319   case ISD::TargetExternalSymbol:
320     Erased = TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
321     break;
322   case ISD::VALUETYPE:
323     Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
324     ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
325     break;
326   case ISD::Register:
327     Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
328                                            N->getValueType(0)));
329     break;
330   case ISD::SRCVALUE: {
331     SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
332     Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
333     break;
334   }    
335   case ISD::LOAD:
336     Erased = Loads.erase(std::make_pair(N->getOperand(1),
337                                         std::make_pair(N->getOperand(0),
338                                                        N->getValueType(0))));
339     break;
340   default:
341     if (N->getNumValues() == 1) {
342       if (N->getNumOperands() == 0) {
343         Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
344                                                  N->getValueType(0)));
345       } else if (N->getNumOperands() == 1) {
346         Erased = 
347           UnaryOps.erase(std::make_pair(N->getOpcode(),
348                                         std::make_pair(N->getOperand(0),
349                                                        N->getValueType(0))));
350       } else if (N->getNumOperands() == 2) {
351         Erased = 
352           BinaryOps.erase(std::make_pair(N->getOpcode(),
353                                          std::make_pair(N->getOperand(0),
354                                                         N->getOperand(1))));
355       } else { 
356         std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
357         Erased = 
358           OneResultNodes.erase(std::make_pair(N->getOpcode(),
359                                               std::make_pair(N->getValueType(0),
360                                                              Ops)));
361       }
362     } else {
363       // Remove the node from the ArbitraryNodes map.
364       std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
365       std::vector<SDOperand>     Ops(N->op_begin(), N->op_end());
366       Erased =
367         ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
368                                             std::make_pair(RV, Ops)));
369     }
370     break;
371   }
372 #ifndef NDEBUG
373   // Verify that the node was actually in one of the CSE maps, unless it has a 
374   // flag result (which cannot be CSE'd) or is one of the special cases that are
375   // not subject to CSE.
376   if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
377       N->getOpcode() != ISD::CALL && N->getOpcode() != ISD::CALLSEQ_START &&
378       N->getOpcode() != ISD::CALLSEQ_END && !N->isTargetOpcode()) {
379     
380     N->dump();
381     assert(0 && "Node is not in map!");
382   }
383 #endif
384 }
385
386 /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps.  It
387 /// has been taken out and modified in some way.  If the specified node already
388 /// exists in the CSE maps, do not modify the maps, but return the existing node
389 /// instead.  If it doesn't exist, add it and return null.
390 ///
391 SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
392   assert(N->getNumOperands() && "This is a leaf node!");
393   if (N->getOpcode() == ISD::CALLSEQ_START || 
394       N->getOpcode() == ISD::CALLSEQ_END ||
395       N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
396     return 0;    // Never add these nodes.
397   
398   // Check that remaining values produced are not flags.
399   for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
400     if (N->getValueType(i) == MVT::Flag)
401       return 0;   // Never CSE anything that produces a flag.
402   
403   if (N->getNumValues() == 1) {
404     if (N->getNumOperands() == 1) {
405       SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
406                                            std::make_pair(N->getOperand(0),
407                                                           N->getValueType(0)))];
408       if (U) return U;
409       U = N;
410     } else if (N->getNumOperands() == 2) {
411       SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
412                                             std::make_pair(N->getOperand(0),
413                                                            N->getOperand(1)))];
414       if (B) return B;
415       B = N;
416     } else {
417       std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
418       SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
419                                                    std::make_pair(N->getValueType(0), Ops))];
420       if (ORN) return ORN;
421       ORN = N;
422     }
423   } else {  
424     if (N->getOpcode() == ISD::LOAD) {
425       SDNode *&L = Loads[std::make_pair(N->getOperand(1),
426                                         std::make_pair(N->getOperand(0),
427                                                        N->getValueType(0)))];
428       if (L) return L;
429       L = N;
430     } else {
431       // Remove the node from the ArbitraryNodes map.
432       std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
433       std::vector<SDOperand>     Ops(N->op_begin(), N->op_end());
434       SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
435                                                   std::make_pair(RV, Ops))];
436       if (AN) return AN;
437       AN = N;
438     }
439   }
440   return 0;
441 }
442
443
444
445 SelectionDAG::~SelectionDAG() {
446   while (!AllNodes.empty()) {
447     SDNode *N = AllNodes.begin();
448     delete [] N->OperandList;
449     N->OperandList = 0;
450     N->NumOperands = 0;
451     AllNodes.pop_front();
452   }
453 }
454
455 SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
456   if (Op.getValueType() == VT) return Op;
457   int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
458   return getNode(ISD::AND, Op.getValueType(), Op,
459                  getConstant(Imm, Op.getValueType()));
460 }
461
462 SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
463   assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
464   // Mask out any bits that are not valid for this constant.
465   if (VT != MVT::i64)
466     Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
467
468   SDNode *&N = Constants[std::make_pair(Val, VT)];
469   if (N) return SDOperand(N, 0);
470   N = new ConstantSDNode(false, Val, VT);
471   AllNodes.push_back(N);
472   return SDOperand(N, 0);
473 }
474
475 SDOperand SelectionDAG::getString(const std::string &Val) {
476   StringSDNode *&N = StringNodes[Val];
477   if (!N) {
478     N = new StringSDNode(Val);
479     AllNodes.push_back(N);
480   }
481   return SDOperand(N, 0);
482 }
483
484 SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
485   assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
486   // Mask out any bits that are not valid for this constant.
487   if (VT != MVT::i64)
488     Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
489   
490   SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
491   if (N) return SDOperand(N, 0);
492   N = new ConstantSDNode(true, Val, VT);
493   AllNodes.push_back(N);
494   return SDOperand(N, 0);
495 }
496
497 SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
498   assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
499   if (VT == MVT::f32)
500     Val = (float)Val;  // Mask out extra precision.
501
502   // Do the map lookup using the actual bit pattern for the floating point
503   // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
504   // we don't have issues with SNANs.
505   SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
506   if (N) return SDOperand(N, 0);
507   N = new ConstantFPSDNode(Val, VT);
508   AllNodes.push_back(N);
509   return SDOperand(N, 0);
510 }
511
512 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
513                                          MVT::ValueType VT, int offset) {
514   SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
515   if (N) return SDOperand(N, 0);
516   N = new GlobalAddressSDNode(false, GV, VT, offset);
517   AllNodes.push_back(N);
518   return SDOperand(N, 0);
519 }
520
521 SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
522                                                MVT::ValueType VT, int offset) {
523   SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
524   if (N) return SDOperand(N, 0);
525   N = new GlobalAddressSDNode(true, GV, VT, offset);
526   AllNodes.push_back(N);
527   return SDOperand(N, 0);
528 }
529
530 SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
531   SDNode *&N = FrameIndices[FI];
532   if (N) return SDOperand(N, 0);
533   N = new FrameIndexSDNode(FI, VT, false);
534   AllNodes.push_back(N);
535   return SDOperand(N, 0);
536 }
537
538 SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
539   SDNode *&N = TargetFrameIndices[FI];
540   if (N) return SDOperand(N, 0);
541   N = new FrameIndexSDNode(FI, VT, true);
542   AllNodes.push_back(N);
543   return SDOperand(N, 0);
544 }
545
546 SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
547   SDNode *&N = ConstantPoolIndices[C];
548   if (N) return SDOperand(N, 0);
549   N = new ConstantPoolSDNode(C, VT, false);
550   AllNodes.push_back(N);
551   return SDOperand(N, 0);
552 }
553
554 SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
555   SDNode *&N = TargetConstantPoolIndices[C];
556   if (N) return SDOperand(N, 0);
557   N = new ConstantPoolSDNode(C, VT, true);
558   AllNodes.push_back(N);
559   return SDOperand(N, 0);
560 }
561
562 SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
563   SDNode *&N = BBNodes[MBB];
564   if (N) return SDOperand(N, 0);
565   N = new BasicBlockSDNode(MBB);
566   AllNodes.push_back(N);
567   return SDOperand(N, 0);
568 }
569
570 SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
571   if ((unsigned)VT >= ValueTypeNodes.size())
572     ValueTypeNodes.resize(VT+1);
573   if (ValueTypeNodes[VT] == 0) {
574     ValueTypeNodes[VT] = new VTSDNode(VT);
575     AllNodes.push_back(ValueTypeNodes[VT]);
576   }
577
578   return SDOperand(ValueTypeNodes[VT], 0);
579 }
580
581 SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
582   SDNode *&N = ExternalSymbols[Sym];
583   if (N) return SDOperand(N, 0);
584   N = new ExternalSymbolSDNode(false, Sym, VT);
585   AllNodes.push_back(N);
586   return SDOperand(N, 0);
587 }
588
589 SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT::ValueType VT) {
590   SDNode *&N = TargetExternalSymbols[Sym];
591   if (N) return SDOperand(N, 0);
592   N = new ExternalSymbolSDNode(true, Sym, VT);
593   AllNodes.push_back(N);
594   return SDOperand(N, 0);
595 }
596
597 SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
598   if ((unsigned)Cond >= CondCodeNodes.size())
599     CondCodeNodes.resize(Cond+1);
600   
601   if (CondCodeNodes[Cond] == 0) {
602     CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
603     AllNodes.push_back(CondCodeNodes[Cond]);
604   }
605   return SDOperand(CondCodeNodes[Cond], 0);
606 }
607
608 SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
609   RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
610   if (!Reg) {
611     Reg = new RegisterSDNode(RegNo, VT);
612     AllNodes.push_back(Reg);
613   }
614   return SDOperand(Reg, 0);
615 }
616
617 SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
618                                       SDOperand N2, ISD::CondCode Cond) {
619   // These setcc operations always fold.
620   switch (Cond) {
621   default: break;
622   case ISD::SETFALSE:
623   case ISD::SETFALSE2: return getConstant(0, VT);
624   case ISD::SETTRUE:
625   case ISD::SETTRUE2:  return getConstant(1, VT);
626   }
627
628   if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
629     uint64_t C2 = N2C->getValue();
630     if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
631       uint64_t C1 = N1C->getValue();
632
633       // Sign extend the operands if required
634       if (ISD::isSignedIntSetCC(Cond)) {
635         C1 = N1C->getSignExtended();
636         C2 = N2C->getSignExtended();
637       }
638
639       switch (Cond) {
640       default: assert(0 && "Unknown integer setcc!");
641       case ISD::SETEQ:  return getConstant(C1 == C2, VT);
642       case ISD::SETNE:  return getConstant(C1 != C2, VT);
643       case ISD::SETULT: return getConstant(C1 <  C2, VT);
644       case ISD::SETUGT: return getConstant(C1 >  C2, VT);
645       case ISD::SETULE: return getConstant(C1 <= C2, VT);
646       case ISD::SETUGE: return getConstant(C1 >= C2, VT);
647       case ISD::SETLT:  return getConstant((int64_t)C1 <  (int64_t)C2, VT);
648       case ISD::SETGT:  return getConstant((int64_t)C1 >  (int64_t)C2, VT);
649       case ISD::SETLE:  return getConstant((int64_t)C1 <= (int64_t)C2, VT);
650       case ISD::SETGE:  return getConstant((int64_t)C1 >= (int64_t)C2, VT);
651       }
652     } else {
653       // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
654       if (N1.getOpcode() == ISD::ZERO_EXTEND) {
655         unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
656
657         // If the comparison constant has bits in the upper part, the
658         // zero-extended value could never match.
659         if (C2 & (~0ULL << InSize)) {
660           unsigned VSize = MVT::getSizeInBits(N1.getValueType());
661           switch (Cond) {
662           case ISD::SETUGT:
663           case ISD::SETUGE:
664           case ISD::SETEQ: return getConstant(0, VT);
665           case ISD::SETULT:
666           case ISD::SETULE:
667           case ISD::SETNE: return getConstant(1, VT);
668           case ISD::SETGT:
669           case ISD::SETGE:
670             // True if the sign bit of C2 is set.
671             return getConstant((C2 & (1ULL << VSize)) != 0, VT);
672           case ISD::SETLT:
673           case ISD::SETLE:
674             // True if the sign bit of C2 isn't set.
675             return getConstant((C2 & (1ULL << VSize)) == 0, VT);
676           default:
677             break;
678           }
679         }
680
681         // Otherwise, we can perform the comparison with the low bits.
682         switch (Cond) {
683         case ISD::SETEQ:
684         case ISD::SETNE:
685         case ISD::SETUGT:
686         case ISD::SETUGE:
687         case ISD::SETULT:
688         case ISD::SETULE:
689           return getSetCC(VT, N1.getOperand(0),
690                           getConstant(C2, N1.getOperand(0).getValueType()),
691                           Cond);
692         default:
693           break;   // todo, be more careful with signed comparisons
694         }
695       } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
696                  (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
697         MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
698         unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
699         MVT::ValueType ExtDstTy = N1.getValueType();
700         unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
701
702         // If the extended part has any inconsistent bits, it cannot ever
703         // compare equal.  In other words, they have to be all ones or all
704         // zeros.
705         uint64_t ExtBits =
706           (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
707         if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
708           return getConstant(Cond == ISD::SETNE, VT);
709         
710         // Otherwise, make this a use of a zext.
711         return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
712                         getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
713                         Cond);
714       }
715
716       uint64_t MinVal, MaxVal;
717       unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
718       if (ISD::isSignedIntSetCC(Cond)) {
719         MinVal = 1ULL << (OperandBitSize-1);
720         if (OperandBitSize != 1)   // Avoid X >> 64, which is undefined.
721           MaxVal = ~0ULL >> (65-OperandBitSize);
722         else
723           MaxVal = 0;
724       } else {
725         MinVal = 0;
726         MaxVal = ~0ULL >> (64-OperandBitSize);
727       }
728
729       // Canonicalize GE/LE comparisons to use GT/LT comparisons.
730       if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
731         if (C2 == MinVal) return getConstant(1, VT);   // X >= MIN --> true
732         --C2;                                          // X >= C1 --> X > (C1-1)
733         return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
734                         (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
735       }
736
737       if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
738         if (C2 == MaxVal) return getConstant(1, VT);   // X <= MAX --> true
739         ++C2;                                          // X <= C1 --> X < (C1+1)
740         return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
741                         (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
742       }
743
744       if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
745         return getConstant(0, VT);      // X < MIN --> false
746
747       // Canonicalize setgt X, Min --> setne X, Min
748       if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
749         return getSetCC(VT, N1, N2, ISD::SETNE);
750
751       // If we have setult X, 1, turn it into seteq X, 0
752       if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
753         return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
754                         ISD::SETEQ);
755       // If we have setugt X, Max-1, turn it into seteq X, Max
756       else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
757         return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
758                         ISD::SETEQ);
759
760       // If we have "setcc X, C1", check to see if we can shrink the immediate
761       // by changing cc.
762
763       // SETUGT X, SINTMAX  -> SETLT X, 0
764       if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
765           C2 == (~0ULL >> (65-OperandBitSize)))
766         return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
767
768       // FIXME: Implement the rest of these.
769
770
771       // Fold bit comparisons when we can.
772       if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
773           VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
774         if (ConstantSDNode *AndRHS =
775                     dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
776           if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0  -->  (X & 8) >> 3
777             // Perform the xform if the AND RHS is a single bit.
778             if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
779               return getNode(ISD::SRL, VT, N1,
780                              getConstant(Log2_64(AndRHS->getValue()),
781                                                    TLI.getShiftAmountTy()));
782             }
783           } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
784             // (X & 8) == 8  -->  (X & 8) >> 3
785             // Perform the xform if C2 is a single bit.
786             if ((C2 & (C2-1)) == 0) {
787               return getNode(ISD::SRL, VT, N1,
788                              getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
789             }
790           }
791         }
792     }
793   } else if (isa<ConstantSDNode>(N1.Val)) {
794       // Ensure that the constant occurs on the RHS.
795     return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
796   }
797
798   if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
799     if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
800       double C1 = N1C->getValue(), C2 = N2C->getValue();
801
802       switch (Cond) {
803       default: break; // FIXME: Implement the rest of these!
804       case ISD::SETEQ:  return getConstant(C1 == C2, VT);
805       case ISD::SETNE:  return getConstant(C1 != C2, VT);
806       case ISD::SETLT:  return getConstant(C1 < C2, VT);
807       case ISD::SETGT:  return getConstant(C1 > C2, VT);
808       case ISD::SETLE:  return getConstant(C1 <= C2, VT);
809       case ISD::SETGE:  return getConstant(C1 >= C2, VT);
810       }
811     } else {
812       // Ensure that the constant occurs on the RHS.
813       return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
814     }
815
816   // Could not fold it.
817   return SDOperand();
818 }
819
820 /// getNode - Gets or creates the specified node.
821 ///
822 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
823   SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
824   if (!N) {
825     N = new SDNode(Opcode, VT);
826     AllNodes.push_back(N);
827   }
828   return SDOperand(N, 0);
829 }
830
831 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
832                                 SDOperand Operand) {
833   unsigned Tmp1;
834   // Constant fold unary operations with an integer constant operand.
835   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
836     uint64_t Val = C->getValue();
837     switch (Opcode) {
838     default: break;
839     case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
840     case ISD::ANY_EXTEND:
841     case ISD::ZERO_EXTEND: return getConstant(Val, VT);
842     case ISD::TRUNCATE:    return getConstant(Val, VT);
843     case ISD::SINT_TO_FP:  return getConstantFP(C->getSignExtended(), VT);
844     case ISD::UINT_TO_FP:  return getConstantFP(C->getValue(), VT);
845     case ISD::BIT_CONVERT:
846       if (VT == MVT::f32) {
847         assert(C->getValueType(0) == MVT::i32 && "Invalid bit_convert!");
848         return getConstantFP(BitsToFloat(Val), VT);
849       } else if (VT == MVT::f64) {
850         assert(C->getValueType(0) == MVT::i64 && "Invalid bit_convert!");
851         return getConstantFP(BitsToDouble(Val), VT);
852       }
853       break;
854     case ISD::BSWAP:
855       switch(VT) {
856       default: assert(0 && "Invalid bswap!"); break;
857       case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
858       case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
859       case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
860       }
861       break;
862     case ISD::CTPOP:
863       switch(VT) {
864       default: assert(0 && "Invalid ctpop!"); break;
865       case MVT::i1: return getConstant(Val != 0, VT);
866       case MVT::i8: 
867         Tmp1 = (unsigned)Val & 0xFF;
868         return getConstant(CountPopulation_32(Tmp1), VT);
869       case MVT::i16:
870         Tmp1 = (unsigned)Val & 0xFFFF;
871         return getConstant(CountPopulation_32(Tmp1), VT);
872       case MVT::i32:
873         return getConstant(CountPopulation_32((unsigned)Val), VT);
874       case MVT::i64:
875         return getConstant(CountPopulation_64(Val), VT);
876       }
877     case ISD::CTLZ:
878       switch(VT) {
879       default: assert(0 && "Invalid ctlz!"); break;
880       case MVT::i1: return getConstant(Val == 0, VT);
881       case MVT::i8: 
882         Tmp1 = (unsigned)Val & 0xFF;
883         return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
884       case MVT::i16:
885         Tmp1 = (unsigned)Val & 0xFFFF;
886         return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
887       case MVT::i32:
888         return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
889       case MVT::i64:
890         return getConstant(CountLeadingZeros_64(Val), VT);
891       }
892     case ISD::CTTZ:
893       switch(VT) {
894       default: assert(0 && "Invalid cttz!"); break;
895       case MVT::i1: return getConstant(Val == 0, VT);
896       case MVT::i8: 
897         Tmp1 = (unsigned)Val | 0x100;
898         return getConstant(CountTrailingZeros_32(Tmp1), VT);
899       case MVT::i16:
900         Tmp1 = (unsigned)Val | 0x10000;
901         return getConstant(CountTrailingZeros_32(Tmp1), VT);
902       case MVT::i32:
903         return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
904       case MVT::i64:
905         return getConstant(CountTrailingZeros_64(Val), VT);
906       }
907     }
908   }
909
910   // Constant fold unary operations with an floating point constant operand.
911   if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
912     switch (Opcode) {
913     case ISD::FNEG:
914       return getConstantFP(-C->getValue(), VT);
915     case ISD::FABS:
916       return getConstantFP(fabs(C->getValue()), VT);
917     case ISD::FP_ROUND:
918     case ISD::FP_EXTEND:
919       return getConstantFP(C->getValue(), VT);
920     case ISD::FP_TO_SINT:
921       return getConstant((int64_t)C->getValue(), VT);
922     case ISD::FP_TO_UINT:
923       return getConstant((uint64_t)C->getValue(), VT);
924     case ISD::BIT_CONVERT:
925       if (VT == MVT::i32) {
926         assert(C->getValueType(0) == MVT::f32 && "Invalid bit_convert!");
927         return getConstant(FloatToBits(C->getValue()), VT);
928       } else if (VT == MVT::i64) {
929         assert(C->getValueType(0) == MVT::f64 && "Invalid bit_convert!");
930         return getConstant(DoubleToBits(C->getValue()), VT);
931       }
932       break;
933     }
934
935   unsigned OpOpcode = Operand.Val->getOpcode();
936   switch (Opcode) {
937   case ISD::TokenFactor:
938     return Operand;         // Factor of one node?  No factor.
939   case ISD::SIGN_EXTEND:
940     if (Operand.getValueType() == VT) return Operand;   // noop extension
941     if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
942       return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
943     break;
944   case ISD::ZERO_EXTEND:
945     if (Operand.getValueType() == VT) return Operand;   // noop extension
946     if (OpOpcode == ISD::ZERO_EXTEND)   // (zext (zext x)) -> (zext x)
947       return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
948     break;
949   case ISD::ANY_EXTEND:
950     if (Operand.getValueType() == VT) return Operand;   // noop extension
951     if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
952       // (ext (zext x)) -> (zext x)  and  (ext (sext x)) -> (sext x)
953       return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
954     break;
955   case ISD::TRUNCATE:
956     if (Operand.getValueType() == VT) return Operand;   // noop truncate
957     if (OpOpcode == ISD::TRUNCATE)
958       return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
959     else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
960              OpOpcode == ISD::ANY_EXTEND) {
961       // If the source is smaller than the dest, we still need an extend.
962       if (Operand.Val->getOperand(0).getValueType() < VT)
963         return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
964       else if (Operand.Val->getOperand(0).getValueType() > VT)
965         return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
966       else
967         return Operand.Val->getOperand(0);
968     }
969     break;
970   case ISD::BIT_CONVERT:
971     // Basic sanity checking.
972     assert(MVT::getSizeInBits(VT)==MVT::getSizeInBits(Operand.getValueType()) &&
973            "Cannot BIT_CONVERT between two different types!");
974     if (VT == Operand.getValueType()) return Operand;  // noop conversion.
975     if (OpOpcode == ISD::BIT_CONVERT)  // bitconv(bitconv(x)) -> bitconv(x)
976       return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
977     break;
978   case ISD::FNEG:
979     if (OpOpcode == ISD::FSUB)   // -(X-Y) -> (Y-X)
980       return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
981                      Operand.Val->getOperand(0));
982     if (OpOpcode == ISD::FNEG)  // --X -> X
983       return Operand.Val->getOperand(0);
984     break;
985   case ISD::FABS:
986     if (OpOpcode == ISD::FNEG)  // abs(-X) -> abs(X)
987       return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
988     break;
989   }
990
991   SDNode *N;
992   if (VT != MVT::Flag) { // Don't CSE flag producing nodes
993     SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
994     if (E) return SDOperand(E, 0);
995     E = N = new SDNode(Opcode, Operand);
996   } else {
997     N = new SDNode(Opcode, Operand);
998   }
999   N->setValueTypes(VT);
1000   AllNodes.push_back(N);
1001   return SDOperand(N, 0);
1002 }
1003
1004
1005
1006 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1007                                 SDOperand N1, SDOperand N2) {
1008 #ifndef NDEBUG
1009   switch (Opcode) {
1010   case ISD::TokenFactor:
1011     assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1012            N2.getValueType() == MVT::Other && "Invalid token factor!");
1013     break;
1014   case ISD::AND:
1015   case ISD::OR:
1016   case ISD::XOR:
1017   case ISD::UDIV:
1018   case ISD::UREM:
1019   case ISD::MULHU:
1020   case ISD::MULHS:
1021     assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1022     // fall through
1023   case ISD::ADD:
1024   case ISD::SUB:
1025   case ISD::MUL:
1026   case ISD::SDIV:
1027   case ISD::SREM:
1028     assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1029     // fall through.
1030   case ISD::FADD:
1031   case ISD::FSUB:
1032   case ISD::FMUL:
1033   case ISD::FDIV:
1034   case ISD::FREM:
1035     assert(N1.getValueType() == N2.getValueType() &&
1036            N1.getValueType() == VT && "Binary operator types must match!");
1037     break;
1038
1039   case ISD::SHL:
1040   case ISD::SRA:
1041   case ISD::SRL:
1042   case ISD::ROTL:
1043   case ISD::ROTR:
1044     assert(VT == N1.getValueType() &&
1045            "Shift operators return type must be the same as their first arg");
1046     assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1047            VT != MVT::i1 && "Shifts only work on integers");
1048     break;
1049   case ISD::FP_ROUND_INREG: {
1050     MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1051     assert(VT == N1.getValueType() && "Not an inreg round!");
1052     assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1053            "Cannot FP_ROUND_INREG integer types");
1054     assert(EVT <= VT && "Not rounding down!");
1055     break;
1056   }
1057   case ISD::AssertSext:
1058   case ISD::AssertZext:
1059   case ISD::SIGN_EXTEND_INREG: {
1060     MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1061     assert(VT == N1.getValueType() && "Not an inreg extend!");
1062     assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1063            "Cannot *_EXTEND_INREG FP types");
1064     assert(EVT <= VT && "Not extending!");
1065   }
1066
1067   default: break;
1068   }
1069 #endif
1070
1071   ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1072   ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1073   if (N1C) {
1074     if (N2C) {
1075       uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1076       switch (Opcode) {
1077       case ISD::ADD: return getConstant(C1 + C2, VT);
1078       case ISD::SUB: return getConstant(C1 - C2, VT);
1079       case ISD::MUL: return getConstant(C1 * C2, VT);
1080       case ISD::UDIV:
1081         if (C2) return getConstant(C1 / C2, VT);
1082         break;
1083       case ISD::UREM :
1084         if (C2) return getConstant(C1 % C2, VT);
1085         break;
1086       case ISD::SDIV :
1087         if (C2) return getConstant(N1C->getSignExtended() /
1088                                    N2C->getSignExtended(), VT);
1089         break;
1090       case ISD::SREM :
1091         if (C2) return getConstant(N1C->getSignExtended() %
1092                                    N2C->getSignExtended(), VT);
1093         break;
1094       case ISD::AND  : return getConstant(C1 & C2, VT);
1095       case ISD::OR   : return getConstant(C1 | C2, VT);
1096       case ISD::XOR  : return getConstant(C1 ^ C2, VT);
1097       case ISD::SHL  : return getConstant(C1 << C2, VT);
1098       case ISD::SRL  : return getConstant(C1 >> C2, VT);
1099       case ISD::SRA  : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
1100       case ISD::ROTL : 
1101         return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1102                            VT);
1103       case ISD::ROTR : 
1104         return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)), 
1105                            VT);
1106       default: break;
1107       }
1108     } else {      // Cannonicalize constant to RHS if commutative
1109       if (isCommutativeBinOp(Opcode)) {
1110         std::swap(N1C, N2C);
1111         std::swap(N1, N2);
1112       }
1113     }
1114   }
1115
1116   ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1117   ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1118   if (N1CFP) {
1119     if (N2CFP) {
1120       double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1121       switch (Opcode) {
1122       case ISD::FADD: return getConstantFP(C1 + C2, VT);
1123       case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1124       case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1125       case ISD::FDIV:
1126         if (C2) return getConstantFP(C1 / C2, VT);
1127         break;
1128       case ISD::FREM :
1129         if (C2) return getConstantFP(fmod(C1, C2), VT);
1130         break;
1131       default: break;
1132       }
1133     } else {      // Cannonicalize constant to RHS if commutative
1134       if (isCommutativeBinOp(Opcode)) {
1135         std::swap(N1CFP, N2CFP);
1136         std::swap(N1, N2);
1137       }
1138     }
1139   }
1140
1141   // Finally, fold operations that do not require constants.
1142   switch (Opcode) {
1143   case ISD::FP_ROUND_INREG:
1144     if (cast<VTSDNode>(N2)->getVT() == VT) return N1;  // Not actually rounding.
1145     break;
1146   case ISD::SIGN_EXTEND_INREG: {
1147     MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1148     if (EVT == VT) return N1;  // Not actually extending
1149     break;
1150   }
1151
1152   // FIXME: figure out how to safely handle things like
1153   // int foo(int x) { return 1 << (x & 255); }
1154   // int bar() { return foo(256); }
1155 #if 0
1156   case ISD::SHL:
1157   case ISD::SRL:
1158   case ISD::SRA:
1159     if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1160         cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
1161       return getNode(Opcode, VT, N1, N2.getOperand(0));
1162     else if (N2.getOpcode() == ISD::AND)
1163       if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1164         // If the and is only masking out bits that cannot effect the shift,
1165         // eliminate the and.
1166         unsigned NumBits = MVT::getSizeInBits(VT);
1167         if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1168           return getNode(Opcode, VT, N1, N2.getOperand(0));
1169       }
1170     break;
1171 #endif
1172   }
1173
1174   // Memoize this node if possible.
1175   SDNode *N;
1176   if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END &&
1177       VT != MVT::Flag) {
1178     SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1179     if (BON) return SDOperand(BON, 0);
1180
1181     BON = N = new SDNode(Opcode, N1, N2);
1182   } else {
1183     N = new SDNode(Opcode, N1, N2);
1184   }
1185
1186   N->setValueTypes(VT);
1187   AllNodes.push_back(N);
1188   return SDOperand(N, 0);
1189 }
1190
1191 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1192                                 SDOperand N1, SDOperand N2, SDOperand N3) {
1193   // Perform various simplifications.
1194   ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1195   ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1196   ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1197   switch (Opcode) {
1198   case ISD::SETCC: {
1199     // Use SimplifySetCC  to simplify SETCC's.
1200     SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
1201     if (Simp.Val) return Simp;
1202     break;
1203   }
1204   case ISD::SELECT:
1205     if (N1C)
1206       if (N1C->getValue())
1207         return N2;             // select true, X, Y -> X
1208       else
1209         return N3;             // select false, X, Y -> Y
1210
1211     if (N2 == N3) return N2;   // select C, X, X -> X
1212     break;
1213   case ISD::BRCOND:
1214     if (N2C)
1215       if (N2C->getValue()) // Unconditional branch
1216         return getNode(ISD::BR, MVT::Other, N1, N3);
1217       else
1218         return N1;         // Never-taken branch
1219     break;
1220   }
1221
1222   std::vector<SDOperand> Ops;
1223   Ops.reserve(3);
1224   Ops.push_back(N1);
1225   Ops.push_back(N2);
1226   Ops.push_back(N3);
1227
1228   // Memoize node if it doesn't produce a flag.
1229   SDNode *N;
1230   if (VT != MVT::Flag) {
1231     SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1232     if (E) return SDOperand(E, 0);
1233     E = N = new SDNode(Opcode, N1, N2, N3);
1234   } else {
1235     N = new SDNode(Opcode, N1, N2, N3);
1236   }
1237   N->setValueTypes(VT);
1238   AllNodes.push_back(N);
1239   return SDOperand(N, 0);
1240 }
1241
1242 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1243                                 SDOperand N1, SDOperand N2, SDOperand N3,
1244                                 SDOperand N4) {
1245   std::vector<SDOperand> Ops;
1246   Ops.reserve(4);
1247   Ops.push_back(N1);
1248   Ops.push_back(N2);
1249   Ops.push_back(N3);
1250   Ops.push_back(N4);
1251   return getNode(Opcode, VT, Ops);
1252 }
1253
1254 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1255                                 SDOperand N1, SDOperand N2, SDOperand N3,
1256                                 SDOperand N4, SDOperand N5) {
1257   std::vector<SDOperand> Ops;
1258   Ops.reserve(5);
1259   Ops.push_back(N1);
1260   Ops.push_back(N2);
1261   Ops.push_back(N3);
1262   Ops.push_back(N4);
1263   Ops.push_back(N5);
1264   return getNode(Opcode, VT, Ops);
1265 }
1266
1267 // setAdjCallChain - This method changes the token chain of an
1268 // CALLSEQ_START/END node to be the specified operand.
1269 void SDNode::setAdjCallChain(SDOperand N) {
1270   assert(N.getValueType() == MVT::Other);
1271   assert((getOpcode() == ISD::CALLSEQ_START ||
1272           getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
1273
1274   OperandList[0].Val->removeUser(this);
1275   OperandList[0] = N;
1276   OperandList[0].Val->Uses.push_back(this);
1277 }
1278
1279 // setAdjCallFlag - This method changes the flag input of an
1280 // CALLSEQ_START/END node to be the specified operand.
1281 void SDNode::setAdjCallFlag(SDOperand N) {
1282   assert(N.getValueType() == MVT::Flag);
1283   assert((getOpcode() == ISD::CALLSEQ_START ||
1284           getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
1285   
1286   SDOperand &FlagOp = OperandList[getNumOperands()-1];
1287   assert(FlagOp.getValueType() == MVT::Flag);
1288   FlagOp.Val->removeUser(this);
1289   FlagOp = N;
1290   FlagOp.Val->Uses.push_back(this);
1291 }
1292
1293
1294 SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1295                                 SDOperand Chain, SDOperand Ptr,
1296                                 SDOperand SV) {
1297   SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1298   if (N) return SDOperand(N, 0);
1299   N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1300
1301   // Loads have a token chain.
1302   setNodeValueTypes(N, VT, MVT::Other);
1303   AllNodes.push_back(N);
1304   return SDOperand(N, 0);
1305 }
1306
1307 SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1308                                    SDOperand Chain, SDOperand Ptr,
1309                                    SDOperand SV) {
1310   SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1311   if (N) return SDOperand(N, 0);
1312   std::vector<SDOperand> Ops;
1313   Ops.reserve(5);
1314   Ops.push_back(Chain);
1315   Ops.push_back(Ptr);
1316   Ops.push_back(getConstant(Count, MVT::i32));
1317   Ops.push_back(getValueType(EVT));
1318   Ops.push_back(SV);
1319   std::vector<MVT::ValueType> VTs;
1320   VTs.reserve(2);
1321   VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other);  // Add token chain.
1322   return getNode(ISD::VLOAD, VTs, Ops);
1323 }
1324
1325 SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1326                                    SDOperand Chain, SDOperand Ptr, SDOperand SV,
1327                                    MVT::ValueType EVT) {
1328   std::vector<SDOperand> Ops;
1329   Ops.reserve(4);
1330   Ops.push_back(Chain);
1331   Ops.push_back(Ptr);
1332   Ops.push_back(SV);
1333   Ops.push_back(getValueType(EVT));
1334   std::vector<MVT::ValueType> VTs;
1335   VTs.reserve(2);
1336   VTs.push_back(VT); VTs.push_back(MVT::Other);  // Add token chain.
1337   return getNode(Opcode, VTs, Ops);
1338 }
1339
1340 SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
1341   assert((!V || isa<PointerType>(V->getType())) &&
1342          "SrcValue is not a pointer?");
1343   SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1344   if (N) return SDOperand(N, 0);
1345
1346   N = new SrcValueSDNode(V, Offset);
1347   AllNodes.push_back(N);
1348   return SDOperand(N, 0);
1349 }
1350
1351 SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1352                                 std::vector<SDOperand> &Ops) {
1353   switch (Ops.size()) {
1354   case 0: return getNode(Opcode, VT);
1355   case 1: return getNode(Opcode, VT, Ops[0]);
1356   case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1357   case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
1358   default: break;
1359   }
1360   
1361   ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
1362   switch (Opcode) {
1363   default: break;
1364   case ISD::BRCONDTWOWAY:
1365     if (N1C)
1366       if (N1C->getValue()) // Unconditional branch to true dest.
1367         return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
1368       else                 // Unconditional branch to false dest.
1369         return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
1370     break;
1371   case ISD::BRTWOWAY_CC:
1372     assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1373     assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1374            "LHS and RHS of comparison must have same type!");
1375     break;
1376   case ISD::TRUNCSTORE: {
1377     assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1378     MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1379 #if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1380     // If this is a truncating store of a constant, convert to the desired type
1381     // and store it instead.
1382     if (isa<Constant>(Ops[0])) {
1383       SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1384       if (isa<Constant>(Op))
1385         N1 = Op;
1386     }
1387     // Also for ConstantFP?
1388 #endif
1389     if (Ops[0].getValueType() == EVT)       // Normal store?
1390       return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1391     assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1392     assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1393            "Can't do FP-INT conversion!");
1394     break;
1395   }
1396   case ISD::SELECT_CC: {
1397     assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
1398     assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1399            "LHS and RHS of condition must have same type!");
1400     assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1401            "True and False arms of SelectCC must have same type!");
1402     assert(Ops[2].getValueType() == VT &&
1403            "select_cc node must be of same type as true and false value!");
1404     break;
1405   }
1406   case ISD::BR_CC: {
1407     assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
1408     assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1409            "LHS/RHS of comparison should match types!");
1410     break;
1411   }
1412   }
1413
1414   // Memoize nodes.
1415   SDNode *N;
1416   if (VT != MVT::Flag) {
1417     SDNode *&E =
1418       OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1419     if (E) return SDOperand(E, 0);
1420     E = N = new SDNode(Opcode, Ops);
1421   } else {
1422     N = new SDNode(Opcode, Ops);
1423   }
1424   N->setValueTypes(VT);
1425   AllNodes.push_back(N);
1426   return SDOperand(N, 0);
1427 }
1428
1429 SDOperand SelectionDAG::getNode(unsigned Opcode,
1430                                 std::vector<MVT::ValueType> &ResultTys,
1431                                 std::vector<SDOperand> &Ops) {
1432   if (ResultTys.size() == 1)
1433     return getNode(Opcode, ResultTys[0], Ops);
1434
1435   switch (Opcode) {
1436   case ISD::EXTLOAD:
1437   case ISD::SEXTLOAD:
1438   case ISD::ZEXTLOAD: {
1439     MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1440     assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1441     // If they are asking for an extending load from/to the same thing, return a
1442     // normal load.
1443     if (ResultTys[0] == EVT)
1444       return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1445     assert(EVT < ResultTys[0] &&
1446            "Should only be an extending load, not truncating!");
1447     assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1448            "Cannot sign/zero extend a FP load!");
1449     assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1450            "Cannot convert from FP to Int or Int -> FP!");
1451     break;
1452   }
1453
1454   // FIXME: figure out how to safely handle things like
1455   // int foo(int x) { return 1 << (x & 255); }
1456   // int bar() { return foo(256); }
1457 #if 0
1458   case ISD::SRA_PARTS:
1459   case ISD::SRL_PARTS:
1460   case ISD::SHL_PARTS:
1461     if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1462         cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
1463       return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1464     else if (N3.getOpcode() == ISD::AND)
1465       if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1466         // If the and is only masking out bits that cannot effect the shift,
1467         // eliminate the and.
1468         unsigned NumBits = MVT::getSizeInBits(VT)*2;
1469         if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1470           return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1471       }
1472     break;
1473 #endif
1474   }
1475
1476   // Memoize the node unless it returns a flag.
1477   SDNode *N;
1478   if (ResultTys.back() != MVT::Flag) {
1479     SDNode *&E =
1480       ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1481     if (E) return SDOperand(E, 0);
1482     E = N = new SDNode(Opcode, Ops);
1483   } else {
1484     N = new SDNode(Opcode, Ops);
1485   }
1486   setNodeValueTypes(N, ResultTys);
1487   AllNodes.push_back(N);
1488   return SDOperand(N, 0);
1489 }
1490
1491 void SelectionDAG::setNodeValueTypes(SDNode *N, 
1492                                      std::vector<MVT::ValueType> &RetVals) {
1493   switch (RetVals.size()) {
1494   case 0: return;
1495   case 1: N->setValueTypes(RetVals[0]); return;
1496   case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1497   default: break;
1498   }
1499   
1500   std::list<std::vector<MVT::ValueType> >::iterator I =
1501     std::find(VTList.begin(), VTList.end(), RetVals);
1502   if (I == VTList.end()) {
1503     VTList.push_front(RetVals);
1504     I = VTList.begin();
1505   }
1506
1507   N->setValueTypes(&(*I)[0], I->size());
1508 }
1509
1510 void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1, 
1511                                      MVT::ValueType VT2) {
1512   for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1513        E = VTList.end(); I != E; ++I) {
1514     if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1515       N->setValueTypes(&(*I)[0], 2);
1516       return;
1517     }
1518   }
1519   std::vector<MVT::ValueType> V;
1520   V.push_back(VT1);
1521   V.push_back(VT2);
1522   VTList.push_front(V);
1523   N->setValueTypes(&(*VTList.begin())[0], 2);
1524 }
1525
1526
1527 /// SelectNodeTo - These are used for target selectors to *mutate* the
1528 /// specified node to have the specified return type, Target opcode, and
1529 /// operands.  Note that target opcodes are stored as
1530 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1531 ///
1532 /// Note that SelectNodeTo returns the resultant node.  If there is already a
1533 /// node of the specified opcode and operands, it returns that node instead of
1534 /// the current one.
1535 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1536                                      MVT::ValueType VT) {
1537   // If an identical node already exists, use it.
1538   SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1539   if (ON) return SDOperand(ON, 0);
1540   
1541   RemoveNodeFromCSEMaps(N);
1542   
1543   N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1544   N->setValueTypes(VT);
1545
1546   ON = N;   // Memoize the new node.
1547   return SDOperand(N, 0);
1548 }
1549
1550 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1551                                      MVT::ValueType VT, SDOperand Op1) {
1552   // If an identical node already exists, use it.
1553   SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1554                                         std::make_pair(Op1, VT))];
1555   if (ON) return SDOperand(ON, 0);
1556   
1557   RemoveNodeFromCSEMaps(N);
1558   N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1559   N->setValueTypes(VT);
1560   N->setOperands(Op1);
1561   
1562   ON = N;   // Memoize the new node.
1563   return SDOperand(N, 0);
1564 }
1565
1566 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1567                                      MVT::ValueType VT, SDOperand Op1,
1568                                      SDOperand Op2) {
1569   // If an identical node already exists, use it.
1570   SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1571                                          std::make_pair(Op1, Op2))];
1572   if (ON) return SDOperand(ON, 0);
1573   
1574   RemoveNodeFromCSEMaps(N);
1575   N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1576   N->setValueTypes(VT);
1577   N->setOperands(Op1, Op2);
1578   
1579   ON = N;   // Memoize the new node.
1580   return SDOperand(N, 0);
1581 }
1582
1583 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1584                                      MVT::ValueType VT, SDOperand Op1,
1585                                      SDOperand Op2, SDOperand Op3) {
1586   // If an identical node already exists, use it.
1587   std::vector<SDOperand> OpList;
1588   OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1589   SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1590                                               std::make_pair(VT, OpList))];
1591   if (ON) return SDOperand(ON, 0);
1592   
1593   RemoveNodeFromCSEMaps(N);
1594   N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1595   N->setValueTypes(VT);
1596   N->setOperands(Op1, Op2, Op3);
1597
1598   ON = N;   // Memoize the new node.
1599   return SDOperand(N, 0);
1600 }
1601
1602 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1603                                      MVT::ValueType VT, SDOperand Op1,
1604                                      SDOperand Op2, SDOperand Op3,
1605                                      SDOperand Op4) {
1606   // If an identical node already exists, use it.
1607   std::vector<SDOperand> OpList;
1608   OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1609   OpList.push_back(Op4);
1610   SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1611                                               std::make_pair(VT, OpList))];
1612   if (ON) return SDOperand(ON, 0);
1613   
1614   RemoveNodeFromCSEMaps(N);
1615   N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1616   N->setValueTypes(VT);
1617   N->setOperands(Op1, Op2, Op3, Op4);
1618
1619   ON = N;   // Memoize the new node.
1620   return SDOperand(N, 0);
1621 }
1622
1623 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1624                                      MVT::ValueType VT, SDOperand Op1,
1625                                      SDOperand Op2, SDOperand Op3,SDOperand Op4,
1626                                      SDOperand Op5) {
1627   // If an identical node already exists, use it.
1628   std::vector<SDOperand> OpList;
1629   OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1630   OpList.push_back(Op4); OpList.push_back(Op5);
1631   SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1632                                               std::make_pair(VT, OpList))];
1633   if (ON) return SDOperand(ON, 0);
1634   
1635   RemoveNodeFromCSEMaps(N);
1636   N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1637   N->setValueTypes(VT);
1638   N->setOperands(Op1, Op2, Op3, Op4, Op5);
1639   
1640   ON = N;   // Memoize the new node.
1641   return SDOperand(N, 0);
1642 }
1643
1644 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1645                                      MVT::ValueType VT, SDOperand Op1,
1646                                      SDOperand Op2, SDOperand Op3,SDOperand Op4,
1647                                      SDOperand Op5, SDOperand Op6) {
1648   // If an identical node already exists, use it.
1649   std::vector<SDOperand> OpList;
1650   OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1651   OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1652   SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1653                                               std::make_pair(VT, OpList))];
1654   if (ON) return SDOperand(ON, 0);
1655
1656   RemoveNodeFromCSEMaps(N);
1657   N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1658   N->setValueTypes(VT);
1659   N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
1660   
1661   ON = N;   // Memoize the new node.
1662   return SDOperand(N, 0);
1663 }
1664
1665 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1666                                      MVT::ValueType VT, SDOperand Op1,
1667                                      SDOperand Op2, SDOperand Op3,SDOperand Op4,
1668                                      SDOperand Op5, SDOperand Op6,
1669                                      SDOperand Op7) {
1670   // If an identical node already exists, use it.
1671   std::vector<SDOperand> OpList;
1672   OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1673   OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1674   OpList.push_back(Op7);
1675   SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1676                                               std::make_pair(VT, OpList))];
1677   if (ON) return SDOperand(ON, 0);
1678
1679   RemoveNodeFromCSEMaps(N);
1680   N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1681   N->setValueTypes(VT);
1682   N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
1683   
1684   ON = N;   // Memoize the new node.
1685   return SDOperand(N, 0);
1686 }
1687 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1688                                      MVT::ValueType VT, SDOperand Op1,
1689                                      SDOperand Op2, SDOperand Op3,SDOperand Op4,
1690                                      SDOperand Op5, SDOperand Op6,
1691                                      SDOperand Op7, SDOperand Op8) {
1692   // If an identical node already exists, use it.
1693   std::vector<SDOperand> OpList;
1694   OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1695   OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1696   OpList.push_back(Op7); OpList.push_back(Op8);
1697   SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1698                                               std::make_pair(VT, OpList))];
1699   if (ON) return SDOperand(ON, 0);
1700
1701   RemoveNodeFromCSEMaps(N);
1702   N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1703   N->setValueTypes(VT);
1704   N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
1705   
1706   ON = N;   // Memoize the new node.
1707   return SDOperand(N, 0);
1708 }
1709
1710 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, 
1711                                      MVT::ValueType VT1, MVT::ValueType VT2,
1712                                      SDOperand Op1, SDOperand Op2) {
1713   // If an identical node already exists, use it.
1714   std::vector<SDOperand> OpList;
1715   OpList.push_back(Op1); OpList.push_back(Op2); 
1716   std::vector<MVT::ValueType> VTList;
1717   VTList.push_back(VT1); VTList.push_back(VT2);
1718   SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1719                                               std::make_pair(VTList, OpList))];
1720   if (ON) return SDOperand(ON, 0);
1721
1722   RemoveNodeFromCSEMaps(N);
1723   N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1724   setNodeValueTypes(N, VT1, VT2);
1725   N->setOperands(Op1, Op2);
1726   
1727   ON = N;   // Memoize the new node.
1728   return SDOperand(N, 0);
1729 }
1730
1731 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1732                                      MVT::ValueType VT1, MVT::ValueType VT2,
1733                                      SDOperand Op1, SDOperand Op2, 
1734                                      SDOperand Op3) {
1735   // If an identical node already exists, use it.
1736   std::vector<SDOperand> OpList;
1737   OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1738   std::vector<MVT::ValueType> VTList;
1739   VTList.push_back(VT1); VTList.push_back(VT2);
1740   SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1741                                               std::make_pair(VTList, OpList))];
1742   if (ON) return SDOperand(ON, 0);
1743
1744   RemoveNodeFromCSEMaps(N);
1745   N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1746   setNodeValueTypes(N, VT1, VT2);
1747   N->setOperands(Op1, Op2, Op3);
1748   
1749   ON = N;   // Memoize the new node.
1750   return SDOperand(N, 0);
1751 }
1752
1753 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1754                                      MVT::ValueType VT1, MVT::ValueType VT2,
1755                                      SDOperand Op1, SDOperand Op2,
1756                                      SDOperand Op3, SDOperand Op4) {
1757   // If an identical node already exists, use it.
1758   std::vector<SDOperand> OpList;
1759   OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1760   OpList.push_back(Op4);
1761   std::vector<MVT::ValueType> VTList;
1762   VTList.push_back(VT1); VTList.push_back(VT2);
1763   SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1764                                               std::make_pair(VTList, OpList))];
1765   if (ON) return SDOperand(ON, 0);
1766
1767   RemoveNodeFromCSEMaps(N);
1768   N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1769   setNodeValueTypes(N, VT1, VT2);
1770   N->setOperands(Op1, Op2, Op3, Op4);
1771
1772   ON = N;   // Memoize the new node.
1773   return SDOperand(N, 0);
1774 }
1775
1776 SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1777                                      MVT::ValueType VT1, MVT::ValueType VT2,
1778                                      SDOperand Op1, SDOperand Op2,
1779                                      SDOperand Op3, SDOperand Op4, 
1780                                      SDOperand Op5) {
1781   // If an identical node already exists, use it.
1782   std::vector<SDOperand> OpList;
1783   OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1784   OpList.push_back(Op4); OpList.push_back(Op5);
1785   std::vector<MVT::ValueType> VTList;
1786   VTList.push_back(VT1); VTList.push_back(VT2);
1787   SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1788                                               std::make_pair(VTList, OpList))];
1789   if (ON) return SDOperand(ON, 0);
1790
1791   RemoveNodeFromCSEMaps(N);
1792   N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1793   setNodeValueTypes(N, VT1, VT2);
1794   N->setOperands(Op1, Op2, Op3, Op4, Op5);
1795   
1796   ON = N;   // Memoize the new node.
1797   return SDOperand(N, 0);
1798 }
1799
1800 // ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1801 /// This can cause recursive merging of nodes in the DAG.
1802 ///
1803 /// This version assumes From/To have a single result value.
1804 ///
1805 void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
1806                                       std::vector<SDNode*> *Deleted) {
1807   SDNode *From = FromN.Val, *To = ToN.Val;
1808   assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
1809          "Cannot replace with this method!");
1810   assert(From != To && "Cannot replace uses of with self");
1811   
1812   while (!From->use_empty()) {
1813     // Process users until they are all gone.
1814     SDNode *U = *From->use_begin();
1815     
1816     // This node is about to morph, remove its old self from the CSE maps.
1817     RemoveNodeFromCSEMaps(U);
1818     
1819     for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1820          I != E; ++I)
1821       if (I->Val == From) {
1822         From->removeUser(U);
1823         I->Val = To;
1824         To->addUser(U);
1825       }
1826
1827     // Now that we have modified U, add it back to the CSE maps.  If it already
1828     // exists there, recursively merge the results together.
1829     if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1830       ReplaceAllUsesWith(U, Existing, Deleted);
1831       // U is now dead.
1832       if (Deleted) Deleted->push_back(U);
1833       DeleteNodeNotInCSEMaps(U);
1834     }
1835   }
1836 }
1837
1838 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1839 /// This can cause recursive merging of nodes in the DAG.
1840 ///
1841 /// This version assumes From/To have matching types and numbers of result
1842 /// values.
1843 ///
1844 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
1845                                       std::vector<SDNode*> *Deleted) {
1846   assert(From != To && "Cannot replace uses of with self");
1847   assert(From->getNumValues() == To->getNumValues() &&
1848          "Cannot use this version of ReplaceAllUsesWith!");
1849   if (From->getNumValues() == 1) {  // If possible, use the faster version.
1850     ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
1851     return;
1852   }
1853   
1854   while (!From->use_empty()) {
1855     // Process users until they are all gone.
1856     SDNode *U = *From->use_begin();
1857     
1858     // This node is about to morph, remove its old self from the CSE maps.
1859     RemoveNodeFromCSEMaps(U);
1860     
1861     for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1862          I != E; ++I)
1863       if (I->Val == From) {
1864         From->removeUser(U);
1865         I->Val = To;
1866         To->addUser(U);
1867       }
1868         
1869     // Now that we have modified U, add it back to the CSE maps.  If it already
1870     // exists there, recursively merge the results together.
1871     if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1872       ReplaceAllUsesWith(U, Existing, Deleted);
1873       // U is now dead.
1874       if (Deleted) Deleted->push_back(U);
1875       DeleteNodeNotInCSEMaps(U);
1876     }
1877   }
1878 }
1879
1880 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1881 /// This can cause recursive merging of nodes in the DAG.
1882 ///
1883 /// This version can replace From with any result values.  To must match the
1884 /// number and types of values returned by From.
1885 void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
1886                                       const std::vector<SDOperand> &To,
1887                                       std::vector<SDNode*> *Deleted) {
1888   assert(From->getNumValues() == To.size() &&
1889          "Incorrect number of values to replace with!");
1890   if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
1891     // Degenerate case handled above.
1892     ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
1893     return;
1894   }
1895
1896   while (!From->use_empty()) {
1897     // Process users until they are all gone.
1898     SDNode *U = *From->use_begin();
1899     
1900     // This node is about to morph, remove its old self from the CSE maps.
1901     RemoveNodeFromCSEMaps(U);
1902     
1903     for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1904          I != E; ++I)
1905       if (I->Val == From) {
1906         const SDOperand &ToOp = To[I->ResNo];
1907         From->removeUser(U);
1908         *I = ToOp;
1909         ToOp.Val->addUser(U);
1910       }
1911         
1912     // Now that we have modified U, add it back to the CSE maps.  If it already
1913     // exists there, recursively merge the results together.
1914     if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1915       ReplaceAllUsesWith(U, Existing, Deleted);
1916       // U is now dead.
1917       if (Deleted) Deleted->push_back(U);
1918       DeleteNodeNotInCSEMaps(U);
1919     }
1920   }
1921 }
1922
1923
1924 //===----------------------------------------------------------------------===//
1925 //                              SDNode Class
1926 //===----------------------------------------------------------------------===//
1927
1928
1929 /// getValueTypeList - Return a pointer to the specified value type.
1930 ///
1931 MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
1932   static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
1933   VTs[VT] = VT;
1934   return &VTs[VT];
1935 }
1936
1937 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1938 /// indicated value.  This method ignores uses of other values defined by this
1939 /// operation.
1940 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1941   assert(Value < getNumValues() && "Bad value!");
1942
1943   // If there is only one value, this is easy.
1944   if (getNumValues() == 1)
1945     return use_size() == NUses;
1946   if (Uses.size() < NUses) return false;
1947
1948   SDOperand TheValue(this, Value);
1949
1950   std::set<SDNode*> UsersHandled;
1951
1952   for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1953        UI != E; ++UI) {
1954     SDNode *User = *UI;
1955     if (User->getNumOperands() == 1 ||
1956         UsersHandled.insert(User).second)     // First time we've seen this?
1957       for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1958         if (User->getOperand(i) == TheValue) {
1959           if (NUses == 0)
1960             return false;   // too many uses
1961           --NUses;
1962         }
1963   }
1964
1965   // Found exactly the right number of uses?
1966   return NUses == 0;
1967 }
1968
1969
1970 const char *SDNode::getOperationName(const SelectionDAG *G) const {
1971   switch (getOpcode()) {
1972   default:
1973     if (getOpcode() < ISD::BUILTIN_OP_END)
1974       return "<<Unknown DAG Node>>";
1975     else {
1976       if (G) {
1977         if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
1978           if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
1979             return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
1980
1981         TargetLowering &TLI = G->getTargetLoweringInfo();
1982         const char *Name =
1983           TLI.getTargetNodeName(getOpcode());
1984         if (Name) return Name;
1985       }
1986
1987       return "<<Unknown Target Node>>";
1988     }
1989    
1990   case ISD::PCMARKER:      return "PCMarker";
1991   case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
1992   case ISD::SRCVALUE:      return "SrcValue";
1993   case ISD::VALUETYPE:     return "ValueType";
1994   case ISD::STRING:        return "String";
1995   case ISD::EntryToken:    return "EntryToken";
1996   case ISD::TokenFactor:   return "TokenFactor";
1997   case ISD::AssertSext:    return "AssertSext";
1998   case ISD::AssertZext:    return "AssertZext";
1999   case ISD::Constant:      return "Constant";
2000   case ISD::TargetConstant: return "TargetConstant";
2001   case ISD::ConstantFP:    return "ConstantFP";
2002   case ISD::ConstantVec:   return "ConstantVec";
2003   case ISD::GlobalAddress: return "GlobalAddress";
2004   case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2005   case ISD::FrameIndex:    return "FrameIndex";
2006   case ISD::TargetFrameIndex: return "TargetFrameIndex";
2007   case ISD::BasicBlock:    return "BasicBlock";
2008   case ISD::Register:      return "Register";
2009   case ISD::ExternalSymbol: return "ExternalSymbol";
2010   case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
2011   case ISD::ConstantPool:  return "ConstantPool";
2012   case ISD::TargetConstantPool:  return "TargetConstantPool";
2013   case ISD::CopyToReg:     return "CopyToReg";
2014   case ISD::CopyFromReg:   return "CopyFromReg";
2015   case ISD::UNDEF:         return "undef";
2016   case ISD::MERGE_VALUES:  return "mergevalues";
2017
2018   // Unary operators
2019   case ISD::FABS:   return "fabs";
2020   case ISD::FNEG:   return "fneg";
2021   case ISD::FSQRT:  return "fsqrt";
2022   case ISD::FSIN:   return "fsin";
2023   case ISD::FCOS:   return "fcos";
2024
2025   // Binary operators
2026   case ISD::ADD:    return "add";
2027   case ISD::SUB:    return "sub";
2028   case ISD::MUL:    return "mul";
2029   case ISD::MULHU:  return "mulhu";
2030   case ISD::MULHS:  return "mulhs";
2031   case ISD::SDIV:   return "sdiv";
2032   case ISD::UDIV:   return "udiv";
2033   case ISD::SREM:   return "srem";
2034   case ISD::UREM:   return "urem";
2035   case ISD::AND:    return "and";
2036   case ISD::OR:     return "or";
2037   case ISD::XOR:    return "xor";
2038   case ISD::SHL:    return "shl";
2039   case ISD::SRA:    return "sra";
2040   case ISD::SRL:    return "srl";
2041   case ISD::ROTL:   return "rotl";
2042   case ISD::ROTR:   return "rotr";
2043   case ISD::FADD:   return "fadd";
2044   case ISD::FSUB:   return "fsub";
2045   case ISD::FMUL:   return "fmul";
2046   case ISD::FDIV:   return "fdiv";
2047   case ISD::FREM:   return "frem";
2048   case ISD::VADD:   return "vadd";
2049   case ISD::VSUB:   return "vsub";
2050   case ISD::VMUL:   return "vmul";
2051     
2052   case ISD::SETCC:       return "setcc";
2053   case ISD::SELECT:      return "select";
2054   case ISD::SELECT_CC:   return "select_cc";
2055   case ISD::ADD_PARTS:   return "add_parts";
2056   case ISD::SUB_PARTS:   return "sub_parts";
2057   case ISD::SHL_PARTS:   return "shl_parts";
2058   case ISD::SRA_PARTS:   return "sra_parts";
2059   case ISD::SRL_PARTS:   return "srl_parts";
2060
2061   // Conversion operators.
2062   case ISD::SIGN_EXTEND: return "sign_extend";
2063   case ISD::ZERO_EXTEND: return "zero_extend";
2064   case ISD::ANY_EXTEND:  return "any_extend";
2065   case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
2066   case ISD::TRUNCATE:    return "truncate";
2067   case ISD::FP_ROUND:    return "fp_round";
2068   case ISD::FP_ROUND_INREG: return "fp_round_inreg";
2069   case ISD::FP_EXTEND:   return "fp_extend";
2070
2071   case ISD::SINT_TO_FP:  return "sint_to_fp";
2072   case ISD::UINT_TO_FP:  return "uint_to_fp";
2073   case ISD::FP_TO_SINT:  return "fp_to_sint";
2074   case ISD::FP_TO_UINT:  return "fp_to_uint";
2075   case ISD::BIT_CONVERT: return "bit_convert";
2076
2077     // Control flow instructions
2078   case ISD::BR:      return "br";
2079   case ISD::BRCOND:  return "brcond";
2080   case ISD::BRCONDTWOWAY:  return "brcondtwoway";
2081   case ISD::BR_CC:  return "br_cc";
2082   case ISD::BRTWOWAY_CC:  return "brtwoway_cc";
2083   case ISD::RET:     return "ret";
2084   case ISD::CALL:    return "call";
2085   case ISD::TAILCALL:return "tailcall";
2086   case ISD::CALLSEQ_START:  return "callseq_start";
2087   case ISD::CALLSEQ_END:    return "callseq_end";
2088
2089     // Other operators
2090   case ISD::LOAD:    return "load";
2091   case ISD::STORE:   return "store";
2092   case ISD::VLOAD:   return "vload";
2093   case ISD::EXTLOAD:    return "extload";
2094   case ISD::SEXTLOAD:   return "sextload";
2095   case ISD::ZEXTLOAD:   return "zextload";
2096   case ISD::TRUNCSTORE: return "truncstore";
2097
2098   case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2099   case ISD::EXTRACT_ELEMENT:    return "extract_element";
2100   case ISD::BUILD_PAIR:         return "build_pair";
2101   case ISD::STACKSAVE:          return "stacksave";
2102   case ISD::STACKRESTORE:       return "stackrestore";
2103     
2104   // Block memory operations.
2105   case ISD::MEMSET:  return "memset";
2106   case ISD::MEMCPY:  return "memcpy";
2107   case ISD::MEMMOVE: return "memmove";
2108
2109   // Bit manipulation
2110   case ISD::BSWAP:   return "bswap";
2111   case ISD::CTPOP:   return "ctpop";
2112   case ISD::CTTZ:    return "cttz";
2113   case ISD::CTLZ:    return "ctlz";
2114
2115   // IO Intrinsics
2116   case ISD::READPORT: return "readport";
2117   case ISD::WRITEPORT: return "writeport";
2118   case ISD::READIO: return "readio";
2119   case ISD::WRITEIO: return "writeio";
2120
2121   // Debug info
2122   case ISD::LOCATION: return "location";
2123   case ISD::DEBUG_LOC: return "debug_loc";
2124   case ISD::DEBUG_LABEL: return "debug_label";
2125
2126   case ISD::CONDCODE:
2127     switch (cast<CondCodeSDNode>(this)->get()) {
2128     default: assert(0 && "Unknown setcc condition!");
2129     case ISD::SETOEQ:  return "setoeq";
2130     case ISD::SETOGT:  return "setogt";
2131     case ISD::SETOGE:  return "setoge";
2132     case ISD::SETOLT:  return "setolt";
2133     case ISD::SETOLE:  return "setole";
2134     case ISD::SETONE:  return "setone";
2135
2136     case ISD::SETO:    return "seto";
2137     case ISD::SETUO:   return "setuo";
2138     case ISD::SETUEQ:  return "setue";
2139     case ISD::SETUGT:  return "setugt";
2140     case ISD::SETUGE:  return "setuge";
2141     case ISD::SETULT:  return "setult";
2142     case ISD::SETULE:  return "setule";
2143     case ISD::SETUNE:  return "setune";
2144
2145     case ISD::SETEQ:   return "seteq";
2146     case ISD::SETGT:   return "setgt";
2147     case ISD::SETGE:   return "setge";
2148     case ISD::SETLT:   return "setlt";
2149     case ISD::SETLE:   return "setle";
2150     case ISD::SETNE:   return "setne";
2151     }
2152   }
2153 }
2154
2155 void SDNode::dump() const { dump(0); }
2156 void SDNode::dump(const SelectionDAG *G) const {
2157   std::cerr << (void*)this << ": ";
2158
2159   for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2160     if (i) std::cerr << ",";
2161     if (getValueType(i) == MVT::Other)
2162       std::cerr << "ch";
2163     else
2164       std::cerr << MVT::getValueTypeString(getValueType(i));
2165   }
2166   std::cerr << " = " << getOperationName(G);
2167
2168   std::cerr << " ";
2169   for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2170     if (i) std::cerr << ", ";
2171     std::cerr << (void*)getOperand(i).Val;
2172     if (unsigned RN = getOperand(i).ResNo)
2173       std::cerr << ":" << RN;
2174   }
2175
2176   if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2177     std::cerr << "<" << CSDN->getValue() << ">";
2178   } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2179     std::cerr << "<" << CSDN->getValue() << ">";
2180   } else if (const GlobalAddressSDNode *GADN =
2181              dyn_cast<GlobalAddressSDNode>(this)) {
2182     int offset = GADN->getOffset();
2183     std::cerr << "<";
2184     WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
2185     if (offset > 0)
2186       std::cerr << " + " << offset;
2187     else
2188       std::cerr << " " << offset;
2189   } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
2190     std::cerr << "<" << FIDN->getIndex() << ">";
2191   } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
2192     std::cerr << "<" << *CP->get() << ">";
2193   } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
2194     std::cerr << "<";
2195     const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2196     if (LBB)
2197       std::cerr << LBB->getName() << " ";
2198     std::cerr << (const void*)BBDN->getBasicBlock() << ">";
2199   } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
2200     if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2201       std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2202     } else {
2203       std::cerr << " #" << R->getReg();
2204     }
2205   } else if (const ExternalSymbolSDNode *ES =
2206              dyn_cast<ExternalSymbolSDNode>(this)) {
2207     std::cerr << "'" << ES->getSymbol() << "'";
2208   } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2209     if (M->getValue())
2210       std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2211     else
2212       std::cerr << "<null:" << M->getOffset() << ">";
2213   } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2214     std::cerr << ":" << getValueTypeString(N->getVT());
2215   }
2216 }
2217
2218 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
2219   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2220     if (N->getOperand(i).Val->hasOneUse())
2221       DumpNodes(N->getOperand(i).Val, indent+2, G);
2222     else
2223       std::cerr << "\n" << std::string(indent+2, ' ')
2224                 << (void*)N->getOperand(i).Val << ": <multiple use>";
2225
2226
2227   std::cerr << "\n" << std::string(indent, ' ');
2228   N->dump(G);
2229 }
2230
2231 void SelectionDAG::dump() const {
2232   std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
2233   std::vector<const SDNode*> Nodes;
2234   for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2235        I != E; ++I)
2236     Nodes.push_back(I);
2237   
2238   std::sort(Nodes.begin(), Nodes.end());
2239
2240   for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2241     if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
2242       DumpNodes(Nodes[i], 2, this);
2243   }
2244
2245   DumpNodes(getRoot().Val, 2, this);
2246
2247   std::cerr << "\n\n";
2248 }
2249