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