SelectionDAG.cpp revision 17eee18f40c79cd6ed00985201d395f8da50c417
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 <iostream>
20#include <set>
21#include <cmath>
22#include <algorithm>
23using namespace llvm;
24
25static bool isCommutativeBinOp(unsigned Opcode) {
26  switch (Opcode) {
27  case ISD::ADD:
28  case ISD::MUL:
29  case ISD::AND:
30  case ISD::OR:
31  case ISD::XOR: return true;
32  default: return false; // FIXME: Need commutative info for user ops!
33  }
34}
35
36static bool isAssociativeBinOp(unsigned Opcode) {
37  switch (Opcode) {
38  case ISD::ADD:
39  case ISD::MUL:
40  case ISD::AND:
41  case ISD::OR:
42  case ISD::XOR: return true;
43  default: return false; // FIXME: Need associative info for user ops!
44  }
45}
46
47static unsigned ExactLog2(uint64_t Val) {
48  unsigned Count = 0;
49  while (Val != 1) {
50    Val >>= 1;
51    ++Count;
52  }
53  return Count;
54}
55
56// isInvertibleForFree - Return true if there is no cost to emitting the logical
57// inverse of this node.
58static bool isInvertibleForFree(SDOperand N) {
59  if (isa<ConstantSDNode>(N.Val)) return true;
60  if (isa<SetCCSDNode>(N.Val) && N.Val->hasOneUse())
61    return true;
62  return false;
63}
64
65
66/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
67/// when given the operation for (X op Y).
68ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
69  // To perform this operation, we just need to swap the L and G bits of the
70  // operation.
71  unsigned OldL = (Operation >> 2) & 1;
72  unsigned OldG = (Operation >> 1) & 1;
73  return ISD::CondCode((Operation & ~6) |  // Keep the N, U, E bits
74                       (OldL << 1) |       // New G bit
75                       (OldG << 2));        // New L bit.
76}
77
78/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
79/// 'op' is a valid SetCC operation.
80ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
81  unsigned Operation = Op;
82  if (isInteger)
83    Operation ^= 7;   // Flip L, G, E bits, but not U.
84  else
85    Operation ^= 15;  // Flip all of the condition bits.
86  if (Operation > ISD::SETTRUE2)
87    Operation &= ~8;     // Don't let N and U bits get set.
88  return ISD::CondCode(Operation);
89}
90
91
92/// isSignedOp - For an integer comparison, return 1 if the comparison is a
93/// signed operation and 2 if the result is an unsigned comparison.  Return zero
94/// if the operation does not depend on the sign of the input (setne and seteq).
95static int isSignedOp(ISD::CondCode Opcode) {
96  switch (Opcode) {
97  default: assert(0 && "Illegal integer setcc operation!");
98  case ISD::SETEQ:
99  case ISD::SETNE: return 0;
100  case ISD::SETLT:
101  case ISD::SETLE:
102  case ISD::SETGT:
103  case ISD::SETGE: return 1;
104  case ISD::SETULT:
105  case ISD::SETULE:
106  case ISD::SETUGT:
107  case ISD::SETUGE: return 2;
108  }
109}
110
111/// getSetCCOrOperation - Return the result of a logical OR between different
112/// comparisons of identical values: ((X op1 Y) | (X op2 Y)).  This function
113/// returns SETCC_INVALID if it is not possible to represent the resultant
114/// comparison.
115ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
116                                       bool isInteger) {
117  if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
118    // Cannot fold a signed integer setcc with an unsigned integer setcc.
119    return ISD::SETCC_INVALID;
120
121  unsigned Op = Op1 | Op2;  // Combine all of the condition bits.
122
123  // If the N and U bits get set then the resultant comparison DOES suddenly
124  // care about orderedness, and is true when ordered.
125  if (Op > ISD::SETTRUE2)
126    Op &= ~16;     // Clear the N bit.
127  return ISD::CondCode(Op);
128}
129
130/// getSetCCAndOperation - Return the result of a logical AND between different
131/// comparisons of identical values: ((X op1 Y) & (X op2 Y)).  This
132/// function returns zero if it is not possible to represent the resultant
133/// comparison.
134ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
135                                        bool isInteger) {
136  if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
137    // Cannot fold a signed setcc with an unsigned setcc.
138    return ISD::SETCC_INVALID;
139
140  // Combine all of the condition bits.
141  return ISD::CondCode(Op1 & Op2);
142}
143
144/// RemoveDeadNodes - This method deletes all unreachable nodes in the
145/// SelectionDAG, including nodes (like loads) that have uses of their token
146/// chain but no other uses and no side effect.  If a node is passed in as an
147/// argument, it is used as the seed for node deletion.
148void SelectionDAG::RemoveDeadNodes(SDNode *N) {
149  std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end());
150
151  // Create a dummy node (which is not added to allnodes), that adds a reference
152  // to the root node, preventing it from being deleted.
153  SDNode *DummyNode = new SDNode(ISD::EntryToken, getRoot());
154
155  DeleteNodeIfDead(N, &AllNodeSet);
156
157 Restart:
158  unsigned NumNodes = AllNodeSet.size();
159  for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end();
160       I != E; ++I) {
161    // Try to delete this node.
162    DeleteNodeIfDead(*I, &AllNodeSet);
163
164    // If we actually deleted any nodes, do not use invalid iterators in
165    // AllNodeSet.
166    if (AllNodeSet.size() != NumNodes)
167      goto Restart;
168  }
169
170  // Restore AllNodes.
171  if (AllNodes.size() != NumNodes)
172    AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end());
173
174  // If the root changed (e.g. it was a dead load, update the root).
175  setRoot(DummyNode->getOperand(0));
176
177  // Now that we are done with the dummy node, delete it.
178  DummyNode->getOperand(0).Val->removeUser(DummyNode);
179  delete DummyNode;
180}
181
182void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) {
183  if (!N->use_empty())
184    return;
185
186  // Okay, we really are going to delete this node.  First take this out of the
187  // appropriate CSE map.
188  switch (N->getOpcode()) {
189  case ISD::Constant:
190    Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
191                                   N->getValueType(0)));
192    break;
193  case ISD::ConstantFP:
194    ConstantFPs.erase(std::make_pair(cast<ConstantFPSDNode>(N)->getValue(),
195                                     N->getValueType(0)));
196    break;
197  case ISD::GlobalAddress:
198    GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
199    break;
200  case ISD::FrameIndex:
201    FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
202    break;
203  case ISD::ConstantPool:
204    ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex());
205    break;
206  case ISD::BasicBlock:
207    BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
208    break;
209  case ISD::ExternalSymbol:
210    ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
211    break;
212
213  case ISD::LOAD:
214    Loads.erase(std::make_pair(N->getOperand(1),
215                               std::make_pair(N->getOperand(0),
216                                              N->getValueType(0))));
217    break;
218  case ISD::SETCC:
219    SetCCs.erase(std::make_pair(std::make_pair(N->getOperand(0),
220                                               N->getOperand(1)),
221                                std::make_pair(
222                                     cast<SetCCSDNode>(N)->getCondition(),
223                                     N->getValueType(0))));
224    break;
225  case ISD::TRUNCSTORE:
226  case ISD::SIGN_EXTEND_INREG:
227  case ISD::ZERO_EXTEND_INREG:
228  case ISD::FP_ROUND_INREG:
229  case ISD::EXTLOAD:
230  case ISD::SEXTLOAD:
231  case ISD::ZEXTLOAD: {
232    EVTStruct NN;
233    NN.Opcode = ISD::TRUNCSTORE;
234    NN.VT = N->getValueType(0);
235    NN.EVT = cast<MVTSDNode>(N)->getExtraValueType();
236    for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
237      NN.Ops.push_back(N->getOperand(i));
238    MVTSDNodes.erase(NN);
239    break;
240  }
241  default:
242    if (N->getNumOperands() == 1)
243      UnaryOps.erase(std::make_pair(N->getOpcode(),
244                                    std::make_pair(N->getOperand(0),
245                                                   N->getValueType(0))));
246    else if (N->getNumOperands() == 2)
247      BinaryOps.erase(std::make_pair(N->getOpcode(),
248                                     std::make_pair(N->getOperand(0),
249                                                    N->getOperand(1))));
250    break;
251  }
252
253  // Next, brutally remove the operand list.
254  while (!N->Operands.empty()) {
255    SDNode *O = N->Operands.back().Val;
256    N->Operands.pop_back();
257    O->removeUser(N);
258
259    // Now that we removed this operand, see if there are no uses of it left.
260    DeleteNodeIfDead(O, NodeSet);
261  }
262
263  // Remove the node from the nodes set and delete it.
264  std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
265  AllNodeSet.erase(N);
266
267  // Now that the node is gone, check to see if any of the operands of this node
268  // are dead now.
269  delete N;
270}
271
272
273SelectionDAG::~SelectionDAG() {
274  for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
275    delete AllNodes[i];
276}
277
278SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
279  assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
280  // Mask out any bits that are not valid for this constant.
281  if (VT != MVT::i64)
282    Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
283
284  SDNode *&N = Constants[std::make_pair(Val, VT)];
285  if (N) return SDOperand(N, 0);
286  N = new ConstantSDNode(Val, VT);
287  AllNodes.push_back(N);
288  return SDOperand(N, 0);
289}
290
291SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
292  assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
293  if (VT == MVT::f32)
294    Val = (float)Val;  // Mask out extra precision.
295
296  SDNode *&N = ConstantFPs[std::make_pair(Val, VT)];
297  if (N) return SDOperand(N, 0);
298  N = new ConstantFPSDNode(Val, VT);
299  AllNodes.push_back(N);
300  return SDOperand(N, 0);
301}
302
303
304
305SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
306                                         MVT::ValueType VT) {
307  SDNode *&N = GlobalValues[GV];
308  if (N) return SDOperand(N, 0);
309  N = new GlobalAddressSDNode(GV,VT);
310  AllNodes.push_back(N);
311  return SDOperand(N, 0);
312}
313
314SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
315  SDNode *&N = FrameIndices[FI];
316  if (N) return SDOperand(N, 0);
317  N = new FrameIndexSDNode(FI, VT);
318  AllNodes.push_back(N);
319  return SDOperand(N, 0);
320}
321
322SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
323  SDNode *N = ConstantPoolIndices[CPIdx];
324  if (N) return SDOperand(N, 0);
325  N = new ConstantPoolSDNode(CPIdx, VT);
326  AllNodes.push_back(N);
327  return SDOperand(N, 0);
328}
329
330SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
331  SDNode *&N = BBNodes[MBB];
332  if (N) return SDOperand(N, 0);
333  N = new BasicBlockSDNode(MBB);
334  AllNodes.push_back(N);
335  return SDOperand(N, 0);
336}
337
338SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
339  SDNode *&N = ExternalSymbols[Sym];
340  if (N) return SDOperand(N, 0);
341  N = new ExternalSymbolSDNode(Sym, VT);
342  AllNodes.push_back(N);
343  return SDOperand(N, 0);
344}
345
346SDOperand SelectionDAG::getSetCC(ISD::CondCode Cond, MVT::ValueType VT,
347                                 SDOperand N1, SDOperand N2) {
348  // These setcc operations always fold.
349  switch (Cond) {
350  default: break;
351  case ISD::SETFALSE:
352  case ISD::SETFALSE2: return getConstant(0, VT);
353  case ISD::SETTRUE:
354  case ISD::SETTRUE2:  return getConstant(1, VT);
355  }
356
357  if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val))
358    if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
359      uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
360
361      // Sign extend the operands if required
362      if (ISD::isSignedIntSetCC(Cond)) {
363        C1 = N1C->getSignExtended();
364        C2 = N2C->getSignExtended();
365      }
366
367      switch (Cond) {
368      default: assert(0 && "Unknown integer setcc!");
369      case ISD::SETEQ:  return getConstant(C1 == C2, VT);
370      case ISD::SETNE:  return getConstant(C1 != C2, VT);
371      case ISD::SETULT: return getConstant(C1 <  C2, VT);
372      case ISD::SETUGT: return getConstant(C1 >  C2, VT);
373      case ISD::SETULE: return getConstant(C1 <= C2, VT);
374      case ISD::SETUGE: return getConstant(C1 >= C2, VT);
375      case ISD::SETLT:  return getConstant((int64_t)C1 <  (int64_t)C2, VT);
376      case ISD::SETGT:  return getConstant((int64_t)C1 >  (int64_t)C2, VT);
377      case ISD::SETLE:  return getConstant((int64_t)C1 <= (int64_t)C2, VT);
378      case ISD::SETGE:  return getConstant((int64_t)C1 >= (int64_t)C2, VT);
379      }
380    } else {
381      // Ensure that the constant occurs on the RHS.
382      Cond = ISD::getSetCCSwappedOperands(Cond);
383      std::swap(N1, N2);
384    }
385
386  if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
387    if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
388      double C1 = N1C->getValue(), C2 = N2C->getValue();
389
390      switch (Cond) {
391      default: break; // FIXME: Implement the rest of these!
392      case ISD::SETEQ:  return getConstant(C1 == C2, VT);
393      case ISD::SETNE:  return getConstant(C1 != C2, VT);
394      case ISD::SETLT:  return getConstant(C1 < C2, VT);
395      case ISD::SETGT:  return getConstant(C1 > C2, VT);
396      case ISD::SETLE:  return getConstant(C1 <= C2, VT);
397      case ISD::SETGE:  return getConstant(C1 >= C2, VT);
398      }
399    } else {
400      // Ensure that the constant occurs on the RHS.
401      Cond = ISD::getSetCCSwappedOperands(Cond);
402      std::swap(N1, N2);
403    }
404
405  if (N1 == N2) {
406    // We can always fold X == Y for integer setcc's.
407    if (MVT::isInteger(N1.getValueType()))
408      return getConstant(ISD::isTrueWhenEqual(Cond), VT);
409    unsigned UOF = ISD::getUnorderedFlavor(Cond);
410    if (UOF == 2)   // FP operators that are undefined on NaNs.
411      return getConstant(ISD::isTrueWhenEqual(Cond), VT);
412    if (UOF == ISD::isTrueWhenEqual(Cond))
413      return getConstant(UOF, VT);
414    // Otherwise, we can't fold it.  However, we can simplify it to SETUO/SETO
415    // if it is not already.
416    Cond = UOF == 0 ? ISD::SETUO : ISD::SETO;
417  }
418
419  if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
420      MVT::isInteger(N1.getValueType())) {
421    if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
422        N1.getOpcode() == ISD::XOR) {
423      // Simplify (X+Y) == (X+Z) -->  Y == Z
424      if (N1.getOpcode() == N2.getOpcode()) {
425        if (N1.getOperand(0) == N2.getOperand(0))
426          return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
427        if (N1.getOperand(1) == N2.getOperand(1))
428          return getSetCC(Cond, VT, N1.getOperand(0), N2.getOperand(0));
429        if (isCommutativeBinOp(N1.getOpcode())) {
430          // If X op Y == Y op X, try other combinations.
431          if (N1.getOperand(0) == N2.getOperand(1))
432            return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(0));
433          if (N1.getOperand(1) == N2.getOperand(0))
434            return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
435        }
436      }
437
438      // Simplify (X+Z) == X -->  Z == 0
439      if (N1.getOperand(0) == N2)
440        return getSetCC(Cond, VT, N1.getOperand(1),
441                        getConstant(0, N1.getValueType()));
442      if (N1.getOperand(1) == N2) {
443        if (isCommutativeBinOp(N1.getOpcode()))
444          return getSetCC(Cond, VT, N1.getOperand(0),
445                          getConstant(0, N1.getValueType()));
446        else {
447          assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
448          // (Z-X) == X  --> Z == X<<1
449          return getSetCC(Cond, VT, N1.getOperand(0),
450                          getNode(ISD::SHL, N2.getValueType(),
451                                  N2, getConstant(1, MVT::i8)));
452        }
453      }
454    }
455
456    if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
457        N2.getOpcode() == ISD::XOR) {
458      // Simplify  X == (X+Z) -->  Z == 0
459      if (N2.getOperand(0) == N1)
460        return getSetCC(Cond, VT, N2.getOperand(1),
461                        getConstant(0, N2.getValueType()));
462      else if (N2.getOperand(1) == N1)
463        return getSetCC(Cond, VT, N2.getOperand(0),
464                        getConstant(0, N2.getValueType()));
465    }
466  }
467
468  SetCCSDNode *&N = SetCCs[std::make_pair(std::make_pair(N1, N2),
469                                          std::make_pair(Cond, VT))];
470  if (N) return SDOperand(N, 0);
471  N = new SetCCSDNode(Cond, N1, N2);
472  N->setValueTypes(VT);
473  AllNodes.push_back(N);
474  return SDOperand(N, 0);
475}
476
477
478
479/// getNode - Gets or creates the specified node.
480///
481SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
482  SDNode *N = new SDNode(Opcode, VT);
483  AllNodes.push_back(N);
484  return SDOperand(N, 0);
485}
486
487SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
488                                SDOperand Operand) {
489  if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
490    uint64_t Val = C->getValue();
491    switch (Opcode) {
492    default: break;
493    case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
494    case ISD::ZERO_EXTEND: return getConstant(Val, VT);
495    case ISD::TRUNCATE:    return getConstant(Val, VT);
496    case ISD::SINT_TO_FP:  return getConstantFP(C->getSignExtended(), VT);
497    case ISD::UINT_TO_FP:  return getConstantFP(C->getValue(), VT);
498    }
499  }
500
501  if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
502    switch (Opcode) {
503    case ISD::FP_ROUND:
504    case ISD::FP_EXTEND:
505      return getConstantFP(C->getValue(), VT);
506    case ISD::FP_TO_SINT:
507      return getConstant((int64_t)C->getValue(), VT);
508    case ISD::FP_TO_UINT:
509      return getConstant((uint64_t)C->getValue(), VT);
510    }
511
512  unsigned OpOpcode = Operand.Val->getOpcode();
513  switch (Opcode) {
514  case ISD::SIGN_EXTEND:
515    if (Operand.getValueType() == VT) return Operand;   // noop extension
516    if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
517      return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
518    break;
519  case ISD::ZERO_EXTEND:
520    if (Operand.getValueType() == VT) return Operand;   // noop extension
521    if (OpOpcode == ISD::ZERO_EXTEND)
522      return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
523    break;
524  case ISD::TRUNCATE:
525    if (Operand.getValueType() == VT) return Operand;   // noop truncate
526    if (OpOpcode == ISD::TRUNCATE)
527      return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
528    else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
529      // If the source is smaller than the dest, we still need an extend.
530      if (Operand.Val->getOperand(0).getValueType() < VT)
531        return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
532      else if (Operand.Val->getOperand(0).getValueType() > VT)
533        return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
534      else
535        return Operand.Val->getOperand(0);
536    }
537    break;
538  }
539
540  SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
541  if (N) return SDOperand(N, 0);
542  N = new SDNode(Opcode, Operand);
543  N->setValueTypes(VT);
544  AllNodes.push_back(N);
545  return SDOperand(N, 0);
546}
547
548SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
549                                SDOperand N1, SDOperand N2) {
550#ifndef NDEBUG
551  switch (Opcode) {
552  case ISD::TokenFactor:
553    assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
554           N2.getValueType() == MVT::Other && "Invalid token factor!");
555    break;
556  case ISD::AND:
557  case ISD::OR:
558  case ISD::XOR:
559  case ISD::UDIV:
560  case ISD::UREM:
561    assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
562    // fall through
563  case ISD::ADD:
564  case ISD::SUB:
565  case ISD::MUL:
566  case ISD::SDIV:
567  case ISD::SREM:
568    assert(N1.getValueType() == N2.getValueType() &&
569           N1.getValueType() == VT && "Binary operator types must match!");
570    break;
571
572  case ISD::SHL:
573  case ISD::SRA:
574  case ISD::SRL:
575    assert(VT == N1.getValueType() &&
576           "Shift operators return type must be the same as their first arg");
577    assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
578           VT != MVT::i1 && "Shifts only work on integers");
579    break;
580  default: break;
581  }
582#endif
583
584  ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
585  ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
586  if (N1C) {
587    if (N2C) {
588      uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
589      switch (Opcode) {
590      case ISD::ADD: return getConstant(C1 + C2, VT);
591      case ISD::SUB: return getConstant(C1 - C2, VT);
592      case ISD::MUL: return getConstant(C1 * C2, VT);
593      case ISD::UDIV:
594        if (C2) return getConstant(C1 / C2, VT);
595        break;
596      case ISD::UREM :
597        if (C2) return getConstant(C1 % C2, VT);
598        break;
599      case ISD::SDIV :
600        if (C2) return getConstant(N1C->getSignExtended() /
601                                   N2C->getSignExtended(), VT);
602        break;
603      case ISD::SREM :
604        if (C2) return getConstant(N1C->getSignExtended() %
605                                   N2C->getSignExtended(), VT);
606        break;
607      case ISD::AND  : return getConstant(C1 & C2, VT);
608      case ISD::OR   : return getConstant(C1 | C2, VT);
609      case ISD::XOR  : return getConstant(C1 ^ C2, VT);
610      case ISD::SHL  : return getConstant(C1 << (int)C2, VT);
611      case ISD::SRL  : return getConstant(C1 >> (unsigned)C2, VT);
612      case ISD::SRA  : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
613      default: break;
614      }
615
616    } else {      // Cannonicalize constant to RHS if commutative
617      if (isCommutativeBinOp(Opcode)) {
618        std::swap(N1C, N2C);
619        std::swap(N1, N2);
620      }
621    }
622
623    switch (Opcode) {
624    default: break;
625    case ISD::SHL:    // shl  0, X -> 0
626      if (N1C->isNullValue()) return N1;
627      break;
628    case ISD::SRL:    // srl  0, X -> 0
629      if (N1C->isNullValue()) return N1;
630      break;
631    case ISD::SRA:    // sra -1, X -> -1
632      if (N1C->isAllOnesValue()) return N1;
633      break;
634    }
635  }
636
637  if (N2C) {
638    uint64_t C2 = N2C->getValue();
639
640    switch (Opcode) {
641    case ISD::ADD:
642      if (!C2) return N1;         // add X, 0 -> X
643      break;
644    case ISD::SUB:
645      if (!C2) return N1;         // sub X, 0 -> X
646      break;
647    case ISD::MUL:
648      if (!C2) return N2;         // mul X, 0 -> 0
649      if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
650        return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
651
652      // FIXME: This should only be done if the target supports shift
653      // operations.
654      if ((C2 & C2-1) == 0) {
655        SDOperand ShAmt = getConstant(ExactLog2(C2), MVT::i8);
656        return getNode(ISD::SHL, VT, N1, ShAmt);
657      }
658      break;
659
660    case ISD::UDIV:
661      // FIXME: This should only be done if the target supports shift
662      // operations.
663      if ((C2 & C2-1) == 0 && C2) {
664        SDOperand ShAmt = getConstant(ExactLog2(C2), MVT::i8);
665        return getNode(ISD::SRL, VT, N1, ShAmt);
666      }
667      break;
668
669    case ISD::SHL:
670    case ISD::SRL:
671    case ISD::SRA:
672      if (C2 == 0) return N1;
673      break;
674
675    case ISD::AND:
676      if (!C2) return N2;         // X and 0 -> 0
677      if (N2C->isAllOnesValue())
678	return N1;                // X and -1 -> X
679      break;
680    case ISD::OR:
681      if (!C2)return N1;          // X or 0 -> X
682      if (N2C->isAllOnesValue())
683	return N2;                // X or -1 -> -1
684      break;
685    case ISD::XOR:
686      if (!C2) return N1;        // X xor 0 -> X
687      if (N2C->isAllOnesValue()) {
688        if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1.Val)){
689          // !(X op Y) -> (X !op Y)
690          bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
691          return getSetCC(ISD::getSetCCInverse(SetCC->getCondition(),isInteger),
692                          SetCC->getValueType(0),
693                          SetCC->getOperand(0), SetCC->getOperand(1));
694        } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
695          SDNode *Op = N1.Val;
696          // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
697          // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
698          SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
699          if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
700            LHS = getNode(ISD::XOR, VT, LHS, N2);  // RHS = ~LHS
701            RHS = getNode(ISD::XOR, VT, RHS, N2);  // RHS = ~RHS
702            if (Op->getOpcode() == ISD::AND)
703              return getNode(ISD::OR, VT, LHS, RHS);
704            return getNode(ISD::AND, VT, LHS, RHS);
705          }
706        }
707	// X xor -1 -> not(x)  ?
708      }
709      break;
710    }
711
712    // Reassociate ((X op C1) op C2) if possible.
713    if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
714      if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
715        return getNode(Opcode, VT, N1.Val->getOperand(0),
716                       getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
717  }
718
719  ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
720  ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
721  if (N1CFP)
722    if (N2CFP) {
723      double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
724      switch (Opcode) {
725      case ISD::ADD: return getConstantFP(C1 + C2, VT);
726      case ISD::SUB: return getConstantFP(C1 - C2, VT);
727      case ISD::MUL: return getConstantFP(C1 * C2, VT);
728      case ISD::SDIV:
729        if (C2) return getConstantFP(C1 / C2, VT);
730        break;
731      case ISD::SREM :
732        if (C2) return getConstantFP(fmod(C1, C2), VT);
733        break;
734      default: break;
735      }
736
737    } else {      // Cannonicalize constant to RHS if commutative
738      if (isCommutativeBinOp(Opcode)) {
739        std::swap(N1CFP, N2CFP);
740        std::swap(N1, N2);
741      }
742    }
743
744  // Finally, fold operations that do not require constants.
745  switch (Opcode) {
746  case ISD::TokenFactor:
747    if (N1.getOpcode() == ISD::EntryToken)
748      return N2;
749    if (N2.getOpcode() == ISD::EntryToken)
750      return N1;
751    break;
752
753  case ISD::AND:
754  case ISD::OR:
755    if (SetCCSDNode *LHS = dyn_cast<SetCCSDNode>(N1.Val))
756      if (SetCCSDNode *RHS = dyn_cast<SetCCSDNode>(N2.Val)) {
757        SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
758        SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
759        ISD::CondCode Op2 = RHS->getCondition();
760
761        // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
762        if (LL == RR && LR == RL) {
763          Op2 = ISD::getSetCCSwappedOperands(Op2);
764          goto MatchedBackwards;
765        }
766
767        if (LL == RL && LR == RR) {
768        MatchedBackwards:
769          ISD::CondCode Result;
770          bool isInteger = MVT::isInteger(LL.getValueType());
771          if (Opcode == ISD::OR)
772            Result = ISD::getSetCCOrOperation(LHS->getCondition(), Op2,
773                                              isInteger);
774          else
775            Result = ISD::getSetCCAndOperation(LHS->getCondition(), Op2,
776                                               isInteger);
777          if (Result != ISD::SETCC_INVALID)
778            return getSetCC(Result, LHS->getValueType(0), LL, LR);
779        }
780      }
781    break;
782  case ISD::XOR:
783    if (N1 == N2) return getConstant(0, VT);  // xor X, Y -> 0
784    break;
785  case ISD::SUB:
786    if (N1.getOpcode() == ISD::ADD) {
787      if (N1.Val->getOperand(0) == N2)
788        return N1.Val->getOperand(1);         // (A+B)-A == B
789      if (N1.Val->getOperand(1) == N2)
790        return N1.Val->getOperand(0);         // (A+B)-B == A
791    }
792    break;
793  }
794
795  SDNode *&N = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
796  if (N) return SDOperand(N, 0);
797  N = new SDNode(Opcode, N1, N2);
798  N->setValueTypes(VT);
799
800  AllNodes.push_back(N);
801  return SDOperand(N, 0);
802}
803
804SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
805                                SDOperand Chain, SDOperand Ptr) {
806  SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
807  if (N) return SDOperand(N, 0);
808  N = new SDNode(ISD::LOAD, Chain, Ptr);
809
810  // Loads have a token chain.
811  N->setValueTypes(VT, MVT::Other);
812  AllNodes.push_back(N);
813  return SDOperand(N, 0);
814}
815
816
817SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
818                                SDOperand N1, SDOperand N2, SDOperand N3) {
819  // Perform various simplifications.
820  ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
821  ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
822  ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
823  switch (Opcode) {
824  case ISD::SELECT:
825    if (N1C)
826      if (N1C->getValue())
827        return N2;             // select true, X, Y -> X
828      else
829        return N3;             // select false, X, Y -> Y
830
831    if (N2 == N3) return N2;   // select C, X, X -> X
832
833    if (VT == MVT::i1) {  // Boolean SELECT
834      if (N2C) {
835        if (N3C) {
836          if (N2C->getValue()) // select C, 1, 0 -> C
837            return N1;
838          return getNode(ISD::XOR, VT, N1, N3); // select C, 0, 1 -> ~C
839        }
840
841        if (N2C->getValue())   // select C, 1, X -> C | X
842          return getNode(ISD::OR, VT, N1, N3);
843        else                   // select C, 0, X -> ~C & X
844          return getNode(ISD::AND, VT,
845                         getNode(ISD::XOR, N1.getValueType(), N1,
846                                 getConstant(1, N1.getValueType())), N3);
847      } else if (N3C) {
848        if (N3C->getValue())   // select C, X, 1 -> ~C | X
849          return getNode(ISD::OR, VT,
850                         getNode(ISD::XOR, N1.getValueType(), N1,
851                                 getConstant(1, N1.getValueType())), N2);
852        else                   // select C, X, 0 -> C & X
853          return getNode(ISD::AND, VT, N1, N2);
854      }
855    }
856
857    break;
858  case ISD::BRCOND:
859    if (N2C)
860      if (N2C->getValue()) // Unconditional branch
861        return getNode(ISD::BR, MVT::Other, N1, N3);
862      else
863        return N1;         // Never-taken branch
864    break;
865  }
866
867  SDNode *N = new SDNode(Opcode, N1, N2, N3);
868  switch (Opcode) {
869  default:
870    N->setValueTypes(VT);
871    break;
872  case ISD::DYNAMIC_STACKALLOC: // DYNAMIC_STACKALLOC produces pointer and chain
873    N->setValueTypes(VT, MVT::Other);
874    break;
875  }
876
877  // FIXME: memoize NODES
878  AllNodes.push_back(N);
879  return SDOperand(N, 0);
880}
881
882SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
883                                std::vector<SDOperand> &Children) {
884  switch (Children.size()) {
885  case 0: return getNode(Opcode, VT);
886  case 1: return getNode(Opcode, VT, Children[0]);
887  case 2: return getNode(Opcode, VT, Children[0], Children[1]);
888  case 3: return getNode(Opcode, VT, Children[0], Children[1], Children[2]);
889  default:
890    // FIXME: MEMOIZE!!
891    SDNode *N = new SDNode(Opcode, Children);
892    if (Opcode != ISD::ADD_PARTS && Opcode != ISD::SUB_PARTS) {
893      N->setValueTypes(VT);
894    } else {
895      std::vector<MVT::ValueType> V(N->getNumOperands()/2, VT);
896      N->setValueTypes(V);
897    }
898    AllNodes.push_back(N);
899    return SDOperand(N, 0);
900  }
901}
902
903SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
904                                MVT::ValueType EVT) {
905
906  switch (Opcode) {
907  default: assert(0 && "Bad opcode for this accessor!");
908  case ISD::FP_ROUND_INREG:
909    assert(VT == N1.getValueType() && "Not an inreg round!");
910    assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
911           "Cannot FP_ROUND_INREG integer types");
912    if (EVT == VT) return N1;  // Not actually rounding
913    assert(EVT < VT && "Not rounding down!");
914    break;
915  case ISD::ZERO_EXTEND_INREG:
916  case ISD::SIGN_EXTEND_INREG:
917    assert(VT == N1.getValueType() && "Not an inreg extend!");
918    assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
919           "Cannot *_EXTEND_INREG FP types");
920    if (EVT == VT) return N1;  // Not actually extending
921    assert(EVT < VT && "Not extending!");
922
923    // If we are sign extending an extension, use the original source.
924    if (N1.getOpcode() == ISD::ZERO_EXTEND_INREG ||
925        N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
926      if (N1.getOpcode() == Opcode &&
927          cast<MVTSDNode>(N1)->getExtraValueType() <= EVT)
928        return N1;
929    }
930
931    break;
932  }
933
934  EVTStruct NN;
935  NN.Opcode = Opcode;
936  NN.VT = VT;
937  NN.EVT = EVT;
938  NN.Ops.push_back(N1);
939
940  SDNode *&N = MVTSDNodes[NN];
941  if (N) return SDOperand(N, 0);
942  N = new MVTSDNode(Opcode, VT, N1, EVT);
943  AllNodes.push_back(N);
944  return SDOperand(N, 0);
945}
946
947SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
948                                SDOperand N2, MVT::ValueType EVT) {
949  switch (Opcode) {
950  default:  assert(0 && "Bad opcode for this accessor!");
951  case ISD::EXTLOAD:
952  case ISD::SEXTLOAD:
953  case ISD::ZEXTLOAD:
954    // If they are asking for an extending loat from/to the same thing, return a
955    // normal load.
956    if (VT == EVT)
957      return getNode(ISD::LOAD, VT, N1, N2);
958    assert(EVT < VT && "Should only be an extending load, not truncating!");
959    assert((Opcode == ISD::EXTLOAD || MVT::isInteger(VT)) &&
960           "Cannot sign/zero extend a FP load!");
961    assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
962           "Cannot convert from FP to Int or Int -> FP!");
963    break;
964  }
965
966  EVTStruct NN;
967  NN.Opcode = Opcode;
968  NN.VT = VT;
969  NN.EVT = EVT;
970  NN.Ops.push_back(N1);
971  NN.Ops.push_back(N2);
972
973  SDNode *&N = MVTSDNodes[NN];
974  if (N) return SDOperand(N, 0);
975  N = new MVTSDNode(Opcode, VT, MVT::Other, N1, N2, EVT);
976  AllNodes.push_back(N);
977  return SDOperand(N, 0);
978}
979
980SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
981                                SDOperand N2, SDOperand N3, MVT::ValueType EVT) {
982  switch (Opcode) {
983  default:  assert(0 && "Bad opcode for this accessor!");
984  case ISD::TRUNCSTORE:
985#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
986    // If this is a truncating store of a constant, convert to the desired type
987    // and store it instead.
988    if (isa<Constant>(N1)) {
989      SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
990      if (isa<Constant>(Op))
991        N1 = Op;
992    }
993    // Also for ConstantFP?
994#endif
995    if (N1.getValueType() == EVT)       // Normal store?
996      return getNode(ISD::STORE, VT, N1, N2, N3);
997    assert(N2.getValueType() > EVT && "Not a truncation?");
998    assert(MVT::isInteger(N2.getValueType()) == MVT::isInteger(EVT) &&
999           "Can't do FP-INT conversion!");
1000    break;
1001  }
1002
1003  EVTStruct NN;
1004  NN.Opcode = Opcode;
1005  NN.VT = VT;
1006  NN.EVT = EVT;
1007  NN.Ops.push_back(N1);
1008  NN.Ops.push_back(N2);
1009  NN.Ops.push_back(N3);
1010
1011  SDNode *&N = MVTSDNodes[NN];
1012  if (N) return SDOperand(N, 0);
1013  N = new MVTSDNode(Opcode, VT, N1, N2, N3, EVT);
1014  AllNodes.push_back(N);
1015  return SDOperand(N, 0);
1016}
1017
1018
1019/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1020/// indicated value.  This method ignores uses of other values defined by this
1021/// operation.
1022bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1023  assert(Value < getNumValues() && "Bad value!");
1024
1025  // If there is only one value, this is easy.
1026  if (getNumValues() == 1)
1027    return use_size() == NUses;
1028  if (Uses.size() < NUses) return false;
1029
1030  SDOperand TheValue(this, Value);
1031
1032  std::set<SDNode*> UsersHandled;
1033
1034  for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1035       UI != E; ++UI) {
1036    SDNode *User = *UI;
1037    if (User->getNumOperands() == 1 ||
1038        UsersHandled.insert(User).second)     // First time we've seen this?
1039      for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1040        if (User->getOperand(i) == TheValue) {
1041          if (NUses == 0)
1042            return false;   // too many uses
1043          --NUses;
1044        }
1045  }
1046
1047  // Found exactly the right number of uses?
1048  return NUses == 0;
1049}
1050
1051
1052const char *SDNode::getOperationName() const {
1053  switch (getOpcode()) {
1054  default: return "<<Unknown>>";
1055  case ISD::EntryToken:    return "EntryToken";
1056  case ISD::TokenFactor:   return "TokenFactor";
1057  case ISD::Constant:      return "Constant";
1058  case ISD::ConstantFP:    return "ConstantFP";
1059  case ISD::GlobalAddress: return "GlobalAddress";
1060  case ISD::FrameIndex:    return "FrameIndex";
1061  case ISD::BasicBlock:    return "BasicBlock";
1062  case ISD::ExternalSymbol: return "ExternalSymbol";
1063  case ISD::ConstantPool:  return "ConstantPoolIndex";
1064  case ISD::CopyToReg:     return "CopyToReg";
1065  case ISD::CopyFromReg:   return "CopyFromReg";
1066  case ISD::ImplicitDef:   return "ImplicitDef";
1067
1068  case ISD::ADD:    return "add";
1069  case ISD::SUB:    return "sub";
1070  case ISD::MUL:    return "mul";
1071  case ISD::SDIV:   return "sdiv";
1072  case ISD::UDIV:   return "udiv";
1073  case ISD::SREM:   return "srem";
1074  case ISD::UREM:   return "urem";
1075  case ISD::AND:    return "and";
1076  case ISD::OR:     return "or";
1077  case ISD::XOR:    return "xor";
1078  case ISD::SHL:    return "shl";
1079  case ISD::SRA:    return "sra";
1080  case ISD::SRL:    return "srl";
1081
1082  case ISD::SELECT: return "select";
1083  case ISD::ADD_PARTS:   return "add_parts";
1084  case ISD::SUB_PARTS:   return "sub_parts";
1085
1086    // Conversion operators.
1087  case ISD::SIGN_EXTEND: return "sign_extend";
1088  case ISD::ZERO_EXTEND: return "zero_extend";
1089  case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
1090  case ISD::ZERO_EXTEND_INREG: return "zero_extend_inreg";
1091  case ISD::TRUNCATE:    return "truncate";
1092  case ISD::FP_ROUND:    return "fp_round";
1093  case ISD::FP_ROUND_INREG: return "fp_round_inreg";
1094  case ISD::FP_EXTEND:   return "fp_extend";
1095
1096  case ISD::SINT_TO_FP:  return "sint_to_fp";
1097  case ISD::UINT_TO_FP:  return "uint_to_fp";
1098  case ISD::FP_TO_SINT:  return "fp_to_sint";
1099  case ISD::FP_TO_UINT:  return "fp_to_uint";
1100
1101    // Control flow instructions
1102  case ISD::BR:      return "br";
1103  case ISD::BRCOND:  return "brcond";
1104  case ISD::RET:     return "ret";
1105  case ISD::CALL:    return "call";
1106  case ISD::ADJCALLSTACKDOWN:  return "adjcallstackdown";
1107  case ISD::ADJCALLSTACKUP:    return "adjcallstackup";
1108
1109    // Other operators
1110  case ISD::LOAD:    return "load";
1111  case ISD::STORE:   return "store";
1112  case ISD::EXTLOAD:    return "extload";
1113  case ISD::SEXTLOAD:   return "sextload";
1114  case ISD::ZEXTLOAD:   return "zextload";
1115  case ISD::TRUNCSTORE: return "truncstore";
1116
1117  case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1118  case ISD::EXTRACT_ELEMENT: return "extract_element";
1119  case ISD::BUILD_PAIR: return "build_pair";
1120  case ISD::MEMSET:  return "memset";
1121  case ISD::MEMCPY:  return "memcpy";
1122  case ISD::MEMMOVE: return "memmove";
1123
1124  case ISD::SETCC:
1125    const SetCCSDNode *SetCC = cast<SetCCSDNode>(this);
1126    switch (SetCC->getCondition()) {
1127    default: assert(0 && "Unknown setcc condition!");
1128    case ISD::SETOEQ:  return "setcc:setoeq";
1129    case ISD::SETOGT:  return "setcc:setogt";
1130    case ISD::SETOGE:  return "setcc:setoge";
1131    case ISD::SETOLT:  return "setcc:setolt";
1132    case ISD::SETOLE:  return "setcc:setole";
1133    case ISD::SETONE:  return "setcc:setone";
1134
1135    case ISD::SETO:    return "setcc:seto";
1136    case ISD::SETUO:   return "setcc:setuo";
1137    case ISD::SETUEQ:  return "setcc:setue";
1138    case ISD::SETUGT:  return "setcc:setugt";
1139    case ISD::SETUGE:  return "setcc:setuge";
1140    case ISD::SETULT:  return "setcc:setult";
1141    case ISD::SETULE:  return "setcc:setule";
1142    case ISD::SETUNE:  return "setcc:setune";
1143
1144    case ISD::SETEQ:   return "setcc:seteq";
1145    case ISD::SETGT:   return "setcc:setgt";
1146    case ISD::SETGE:   return "setcc:setge";
1147    case ISD::SETLT:   return "setcc:setlt";
1148    case ISD::SETLE:   return "setcc:setle";
1149    case ISD::SETNE:   return "setcc:setne";
1150    }
1151  }
1152}
1153
1154void SDNode::dump() const {
1155  std::cerr << (void*)this << ": ";
1156
1157  for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1158    if (i) std::cerr << ",";
1159    if (getValueType(i) == MVT::Other)
1160      std::cerr << "ch";
1161    else
1162      std::cerr << MVT::getValueTypeString(getValueType(i));
1163  }
1164  std::cerr << " = " << getOperationName();
1165
1166  std::cerr << " ";
1167  for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1168    if (i) std::cerr << ", ";
1169    std::cerr << (void*)getOperand(i).Val;
1170    if (unsigned RN = getOperand(i).ResNo)
1171      std::cerr << ":" << RN;
1172  }
1173
1174  if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1175    std::cerr << "<" << CSDN->getValue() << ">";
1176  } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1177    std::cerr << "<" << CSDN->getValue() << ">";
1178  } else if (const GlobalAddressSDNode *GADN =
1179             dyn_cast<GlobalAddressSDNode>(this)) {
1180    std::cerr << "<";
1181    WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
1182  } else if (const FrameIndexSDNode *FIDN =
1183	     dyn_cast<FrameIndexSDNode>(this)) {
1184    std::cerr << "<" << FIDN->getIndex() << ">";
1185  } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
1186    std::cerr << "<" << CP->getIndex() << ">";
1187  } else if (const BasicBlockSDNode *BBDN =
1188	     dyn_cast<BasicBlockSDNode>(this)) {
1189    std::cerr << "<";
1190    const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1191    if (LBB)
1192      std::cerr << LBB->getName() << " ";
1193    std::cerr << (const void*)BBDN->getBasicBlock() << ">";
1194  } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(this)) {
1195    std::cerr << "<reg #" << C2V->getReg() << ">";
1196  } else if (const ExternalSymbolSDNode *ES =
1197             dyn_cast<ExternalSymbolSDNode>(this)) {
1198    std::cerr << "'" << ES->getSymbol() << "'";
1199  } else if (const MVTSDNode *M = dyn_cast<MVTSDNode>(this)) {
1200    std::cerr << " - Ty = " << MVT::getValueTypeString(M->getExtraValueType());
1201  }
1202}
1203
1204static void DumpNodes(SDNode *N, unsigned indent) {
1205  for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1206    if (N->getOperand(i).Val->hasOneUse())
1207      DumpNodes(N->getOperand(i).Val, indent+2);
1208    else
1209      std::cerr << "\n" << std::string(indent+2, ' ')
1210                << (void*)N->getOperand(i).Val << ": <multiple use>";
1211
1212
1213  std::cerr << "\n" << std::string(indent, ' ');
1214  N->dump();
1215}
1216
1217void SelectionDAG::dump() const {
1218  std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
1219  std::vector<SDNode*> Nodes(AllNodes);
1220  std::sort(Nodes.begin(), Nodes.end());
1221
1222  for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
1223    if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
1224      DumpNodes(Nodes[i], 2);
1225  }
1226
1227  DumpNodes(getRoot().Val, 2);
1228
1229  std::cerr << "\n\n";
1230}
1231
1232