Rename @llvm.debugger to @llvm.debugtrap.
[oota-llvm.git] / lib / CodeGen / SelectionDAG / SelectionDAGDumper.cpp
1 //===-- SelectionDAGDumper.cpp - Implement SelectionDAG::dump() -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This implements the SelectionDAG::dump method and friends.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ScheduleDAGSDNodes.h"
15 #include "llvm/Function.h"
16 #include "llvm/Intrinsics.h"
17 #include "llvm/Assembly/Writer.h"
18 #include "llvm/CodeGen/SelectionDAG.h"
19 #include "llvm/CodeGen/MachineConstantPool.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineModuleInfo.h"
22 #include "llvm/Analysis/DebugInfo.h"
23 #include "llvm/Target/TargetInstrInfo.h"
24 #include "llvm/Target/TargetIntrinsicInfo.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Target/TargetRegisterInfo.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/GraphWriter.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/ADT/StringExtras.h"
31 using namespace llvm;
32
33 std::string SDNode::getOperationName(const SelectionDAG *G) const {
34   switch (getOpcode()) {
35   default:
36     if (getOpcode() < ISD::BUILTIN_OP_END)
37       return "<<Unknown DAG Node>>";
38     if (isMachineOpcode()) {
39       if (G)
40         if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
41           if (getMachineOpcode() < TII->getNumOpcodes())
42             return TII->getName(getMachineOpcode());
43       return "<<Unknown Machine Node #" + utostr(getOpcode()) + ">>";
44     }
45     if (G) {
46       const TargetLowering &TLI = G->getTargetLoweringInfo();
47       const char *Name = TLI.getTargetNodeName(getOpcode());
48       if (Name) return Name;
49       return "<<Unknown Target Node #" + utostr(getOpcode()) + ">>";
50     }
51     return "<<Unknown Node #" + utostr(getOpcode()) + ">>";
52
53 #ifndef NDEBUG
54   case ISD::DELETED_NODE:               return "<<Deleted Node!>>";
55 #endif
56   case ISD::PREFETCH:                   return "Prefetch";
57   case ISD::MEMBARRIER:                 return "MemBarrier";
58   case ISD::ATOMIC_FENCE:               return "AtomicFence";
59   case ISD::ATOMIC_CMP_SWAP:            return "AtomicCmpSwap";
60   case ISD::ATOMIC_SWAP:                return "AtomicSwap";
61   case ISD::ATOMIC_LOAD_ADD:            return "AtomicLoadAdd";
62   case ISD::ATOMIC_LOAD_SUB:            return "AtomicLoadSub";
63   case ISD::ATOMIC_LOAD_AND:            return "AtomicLoadAnd";
64   case ISD::ATOMIC_LOAD_OR:             return "AtomicLoadOr";
65   case ISD::ATOMIC_LOAD_XOR:            return "AtomicLoadXor";
66   case ISD::ATOMIC_LOAD_NAND:           return "AtomicLoadNand";
67   case ISD::ATOMIC_LOAD_MIN:            return "AtomicLoadMin";
68   case ISD::ATOMIC_LOAD_MAX:            return "AtomicLoadMax";
69   case ISD::ATOMIC_LOAD_UMIN:           return "AtomicLoadUMin";
70   case ISD::ATOMIC_LOAD_UMAX:           return "AtomicLoadUMax";
71   case ISD::ATOMIC_LOAD:                return "AtomicLoad";
72   case ISD::ATOMIC_STORE:               return "AtomicStore";
73   case ISD::PCMARKER:                   return "PCMarker";
74   case ISD::READCYCLECOUNTER:           return "ReadCycleCounter";
75   case ISD::SRCVALUE:                   return "SrcValue";
76   case ISD::MDNODE_SDNODE:              return "MDNode";
77   case ISD::EntryToken:                 return "EntryToken";
78   case ISD::TokenFactor:                return "TokenFactor";
79   case ISD::AssertSext:                 return "AssertSext";
80   case ISD::AssertZext:                 return "AssertZext";
81
82   case ISD::BasicBlock:                 return "BasicBlock";
83   case ISD::VALUETYPE:                  return "ValueType";
84   case ISD::Register:                   return "Register";
85   case ISD::RegisterMask:               return "RegisterMask";
86   case ISD::Constant:                   return "Constant";
87   case ISD::ConstantFP:                 return "ConstantFP";
88   case ISD::GlobalAddress:              return "GlobalAddress";
89   case ISD::GlobalTLSAddress:           return "GlobalTLSAddress";
90   case ISD::FrameIndex:                 return "FrameIndex";
91   case ISD::JumpTable:                  return "JumpTable";
92   case ISD::GLOBAL_OFFSET_TABLE:        return "GLOBAL_OFFSET_TABLE";
93   case ISD::RETURNADDR:                 return "RETURNADDR";
94   case ISD::FRAMEADDR:                  return "FRAMEADDR";
95   case ISD::FRAME_TO_ARGS_OFFSET:       return "FRAME_TO_ARGS_OFFSET";
96   case ISD::EXCEPTIONADDR:              return "EXCEPTIONADDR";
97   case ISD::LSDAADDR:                   return "LSDAADDR";
98   case ISD::EHSELECTION:                return "EHSELECTION";
99   case ISD::EH_RETURN:                  return "EH_RETURN";
100   case ISD::EH_SJLJ_SETJMP:             return "EH_SJLJ_SETJMP";
101   case ISD::EH_SJLJ_LONGJMP:            return "EH_SJLJ_LONGJMP";
102   case ISD::ConstantPool:               return "ConstantPool";
103   case ISD::ExternalSymbol:             return "ExternalSymbol";
104   case ISD::BlockAddress:               return "BlockAddress";
105   case ISD::INTRINSIC_WO_CHAIN:
106   case ISD::INTRINSIC_VOID:
107   case ISD::INTRINSIC_W_CHAIN: {
108     unsigned OpNo = getOpcode() == ISD::INTRINSIC_WO_CHAIN ? 0 : 1;
109     unsigned IID = cast<ConstantSDNode>(getOperand(OpNo))->getZExtValue();
110     if (IID < Intrinsic::num_intrinsics)
111       return Intrinsic::getName((Intrinsic::ID)IID);
112     else if (const TargetIntrinsicInfo *TII = G->getTarget().getIntrinsicInfo())
113       return TII->getName(IID);
114     llvm_unreachable("Invalid intrinsic ID");
115   }
116
117   case ISD::BUILD_VECTOR:               return "BUILD_VECTOR";
118   case ISD::TargetConstant:             return "TargetConstant";
119   case ISD::TargetConstantFP:           return "TargetConstantFP";
120   case ISD::TargetGlobalAddress:        return "TargetGlobalAddress";
121   case ISD::TargetGlobalTLSAddress:     return "TargetGlobalTLSAddress";
122   case ISD::TargetFrameIndex:           return "TargetFrameIndex";
123   case ISD::TargetJumpTable:            return "TargetJumpTable";
124   case ISD::TargetConstantPool:         return "TargetConstantPool";
125   case ISD::TargetExternalSymbol:       return "TargetExternalSymbol";
126   case ISD::TargetBlockAddress:         return "TargetBlockAddress";
127
128   case ISD::CopyToReg:                  return "CopyToReg";
129   case ISD::CopyFromReg:                return "CopyFromReg";
130   case ISD::UNDEF:                      return "undef";
131   case ISD::MERGE_VALUES:               return "merge_values";
132   case ISD::INLINEASM:                  return "inlineasm";
133   case ISD::EH_LABEL:                   return "eh_label";
134   case ISD::HANDLENODE:                 return "handlenode";
135
136   // Unary operators
137   case ISD::FABS:                       return "fabs";
138   case ISD::FNEG:                       return "fneg";
139   case ISD::FSQRT:                      return "fsqrt";
140   case ISD::FSIN:                       return "fsin";
141   case ISD::FCOS:                       return "fcos";
142   case ISD::FTRUNC:                     return "ftrunc";
143   case ISD::FFLOOR:                     return "ffloor";
144   case ISD::FCEIL:                      return "fceil";
145   case ISD::FRINT:                      return "frint";
146   case ISD::FNEARBYINT:                 return "fnearbyint";
147   case ISD::FEXP:                       return "fexp";
148   case ISD::FEXP2:                      return "fexp2";
149   case ISD::FLOG:                       return "flog";
150   case ISD::FLOG2:                      return "flog2";
151   case ISD::FLOG10:                     return "flog10";
152
153   // Binary operators
154   case ISD::ADD:                        return "add";
155   case ISD::SUB:                        return "sub";
156   case ISD::MUL:                        return "mul";
157   case ISD::MULHU:                      return "mulhu";
158   case ISD::MULHS:                      return "mulhs";
159   case ISD::SDIV:                       return "sdiv";
160   case ISD::UDIV:                       return "udiv";
161   case ISD::SREM:                       return "srem";
162   case ISD::UREM:                       return "urem";
163   case ISD::SMUL_LOHI:                  return "smul_lohi";
164   case ISD::UMUL_LOHI:                  return "umul_lohi";
165   case ISD::SDIVREM:                    return "sdivrem";
166   case ISD::UDIVREM:                    return "udivrem";
167   case ISD::AND:                        return "and";
168   case ISD::OR:                         return "or";
169   case ISD::XOR:                        return "xor";
170   case ISD::SHL:                        return "shl";
171   case ISD::SRA:                        return "sra";
172   case ISD::SRL:                        return "srl";
173   case ISD::ROTL:                       return "rotl";
174   case ISD::ROTR:                       return "rotr";
175   case ISD::FADD:                       return "fadd";
176   case ISD::FSUB:                       return "fsub";
177   case ISD::FMUL:                       return "fmul";
178   case ISD::FDIV:                       return "fdiv";
179   case ISD::FMA:                        return "fma";
180   case ISD::FREM:                       return "frem";
181   case ISD::FCOPYSIGN:                  return "fcopysign";
182   case ISD::FGETSIGN:                   return "fgetsign";
183   case ISD::FPOW:                       return "fpow";
184
185   case ISD::FPOWI:                      return "fpowi";
186   case ISD::SETCC:                      return "setcc";
187   case ISD::SELECT:                     return "select";
188   case ISD::VSELECT:                    return "vselect";
189   case ISD::SELECT_CC:                  return "select_cc";
190   case ISD::INSERT_VECTOR_ELT:          return "insert_vector_elt";
191   case ISD::EXTRACT_VECTOR_ELT:         return "extract_vector_elt";
192   case ISD::CONCAT_VECTORS:             return "concat_vectors";
193   case ISD::INSERT_SUBVECTOR:           return "insert_subvector";
194   case ISD::EXTRACT_SUBVECTOR:          return "extract_subvector";
195   case ISD::SCALAR_TO_VECTOR:           return "scalar_to_vector";
196   case ISD::VECTOR_SHUFFLE:             return "vector_shuffle";
197   case ISD::CARRY_FALSE:                return "carry_false";
198   case ISD::ADDC:                       return "addc";
199   case ISD::ADDE:                       return "adde";
200   case ISD::SADDO:                      return "saddo";
201   case ISD::UADDO:                      return "uaddo";
202   case ISD::SSUBO:                      return "ssubo";
203   case ISD::USUBO:                      return "usubo";
204   case ISD::SMULO:                      return "smulo";
205   case ISD::UMULO:                      return "umulo";
206   case ISD::SUBC:                       return "subc";
207   case ISD::SUBE:                       return "sube";
208   case ISD::SHL_PARTS:                  return "shl_parts";
209   case ISD::SRA_PARTS:                  return "sra_parts";
210   case ISD::SRL_PARTS:                  return "srl_parts";
211
212   // Conversion operators.
213   case ISD::SIGN_EXTEND:                return "sign_extend";
214   case ISD::ZERO_EXTEND:                return "zero_extend";
215   case ISD::ANY_EXTEND:                 return "any_extend";
216   case ISD::SIGN_EXTEND_INREG:          return "sign_extend_inreg";
217   case ISD::TRUNCATE:                   return "truncate";
218   case ISD::FP_ROUND:                   return "fp_round";
219   case ISD::FLT_ROUNDS_:                return "flt_rounds";
220   case ISD::FP_ROUND_INREG:             return "fp_round_inreg";
221   case ISD::FP_EXTEND:                  return "fp_extend";
222
223   case ISD::SINT_TO_FP:                 return "sint_to_fp";
224   case ISD::UINT_TO_FP:                 return "uint_to_fp";
225   case ISD::FP_TO_SINT:                 return "fp_to_sint";
226   case ISD::FP_TO_UINT:                 return "fp_to_uint";
227   case ISD::BITCAST:                    return "bitcast";
228   case ISD::FP16_TO_FP32:               return "fp16_to_fp32";
229   case ISD::FP32_TO_FP16:               return "fp32_to_fp16";
230
231   case ISD::CONVERT_RNDSAT: {
232     switch (cast<CvtRndSatSDNode>(this)->getCvtCode()) {
233     default: llvm_unreachable("Unknown cvt code!");
234     case ISD::CVT_FF:                   return "cvt_ff";
235     case ISD::CVT_FS:                   return "cvt_fs";
236     case ISD::CVT_FU:                   return "cvt_fu";
237     case ISD::CVT_SF:                   return "cvt_sf";
238     case ISD::CVT_UF:                   return "cvt_uf";
239     case ISD::CVT_SS:                   return "cvt_ss";
240     case ISD::CVT_SU:                   return "cvt_su";
241     case ISD::CVT_US:                   return "cvt_us";
242     case ISD::CVT_UU:                   return "cvt_uu";
243     }
244   }
245
246     // Control flow instructions
247   case ISD::BR:                         return "br";
248   case ISD::BRIND:                      return "brind";
249   case ISD::BR_JT:                      return "br_jt";
250   case ISD::BRCOND:                     return "brcond";
251   case ISD::BR_CC:                      return "br_cc";
252   case ISD::CALLSEQ_START:              return "callseq_start";
253   case ISD::CALLSEQ_END:                return "callseq_end";
254
255     // Other operators
256   case ISD::LOAD:                       return "load";
257   case ISD::STORE:                      return "store";
258   case ISD::VAARG:                      return "vaarg";
259   case ISD::VACOPY:                     return "vacopy";
260   case ISD::VAEND:                      return "vaend";
261   case ISD::VASTART:                    return "vastart";
262   case ISD::DYNAMIC_STACKALLOC:         return "dynamic_stackalloc";
263   case ISD::EXTRACT_ELEMENT:            return "extract_element";
264   case ISD::BUILD_PAIR:                 return "build_pair";
265   case ISD::STACKSAVE:                  return "stacksave";
266   case ISD::STACKRESTORE:               return "stackrestore";
267   case ISD::TRAP:                       return "trap";
268   case ISD::DEBUGTRAP:                  return "debugtrap";
269
270   // Bit manipulation
271   case ISD::BSWAP:                      return "bswap";
272   case ISD::CTPOP:                      return "ctpop";
273   case ISD::CTTZ:                       return "cttz";
274   case ISD::CTTZ_ZERO_UNDEF:            return "cttz_zero_undef";
275   case ISD::CTLZ:                       return "ctlz";
276   case ISD::CTLZ_ZERO_UNDEF:            return "ctlz_zero_undef";
277
278   // Trampolines
279   case ISD::INIT_TRAMPOLINE:            return "init_trampoline";
280   case ISD::ADJUST_TRAMPOLINE:          return "adjust_trampoline";
281
282   case ISD::CONDCODE:
283     switch (cast<CondCodeSDNode>(this)->get()) {
284     default: llvm_unreachable("Unknown setcc condition!");
285     case ISD::SETOEQ:                   return "setoeq";
286     case ISD::SETOGT:                   return "setogt";
287     case ISD::SETOGE:                   return "setoge";
288     case ISD::SETOLT:                   return "setolt";
289     case ISD::SETOLE:                   return "setole";
290     case ISD::SETONE:                   return "setone";
291
292     case ISD::SETO:                     return "seto";
293     case ISD::SETUO:                    return "setuo";
294     case ISD::SETUEQ:                   return "setue";
295     case ISD::SETUGT:                   return "setugt";
296     case ISD::SETUGE:                   return "setuge";
297     case ISD::SETULT:                   return "setult";
298     case ISD::SETULE:                   return "setule";
299     case ISD::SETUNE:                   return "setune";
300
301     case ISD::SETEQ:                    return "seteq";
302     case ISD::SETGT:                    return "setgt";
303     case ISD::SETGE:                    return "setge";
304     case ISD::SETLT:                    return "setlt";
305     case ISD::SETLE:                    return "setle";
306     case ISD::SETNE:                    return "setne";
307
308     case ISD::SETTRUE:                  return "settrue";
309     case ISD::SETTRUE2:                 return "settrue2";
310     case ISD::SETFALSE:                 return "setfalse";
311     case ISD::SETFALSE2:                return "setfalse2";
312     }
313   }
314 }
315
316 const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
317   switch (AM) {
318   default:              return "";
319   case ISD::PRE_INC:    return "<pre-inc>";
320   case ISD::PRE_DEC:    return "<pre-dec>";
321   case ISD::POST_INC:   return "<post-inc>";
322   case ISD::POST_DEC:   return "<post-dec>";
323   }
324 }
325
326 void SDNode::dump() const { dump(0); }
327 void SDNode::dump(const SelectionDAG *G) const {
328   print(dbgs(), G);
329   dbgs() << '\n';
330 }
331
332 void SDNode::print_types(raw_ostream &OS, const SelectionDAG *G) const {
333   OS << (void*)this << ": ";
334
335   for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
336     if (i) OS << ",";
337     if (getValueType(i) == MVT::Other)
338       OS << "ch";
339     else
340       OS << getValueType(i).getEVTString();
341   }
342   OS << " = " << getOperationName(G);
343 }
344
345 void SDNode::print_details(raw_ostream &OS, const SelectionDAG *G) const {
346   if (const MachineSDNode *MN = dyn_cast<MachineSDNode>(this)) {
347     if (!MN->memoperands_empty()) {
348       OS << "<";
349       OS << "Mem:";
350       for (MachineSDNode::mmo_iterator i = MN->memoperands_begin(),
351            e = MN->memoperands_end(); i != e; ++i) {
352         OS << **i;
353         if (llvm::next(i) != e)
354           OS << " ";
355       }
356       OS << ">";
357     }
358   } else if (const ShuffleVectorSDNode *SVN =
359                dyn_cast<ShuffleVectorSDNode>(this)) {
360     OS << "<";
361     for (unsigned i = 0, e = ValueList[0].getVectorNumElements(); i != e; ++i) {
362       int Idx = SVN->getMaskElt(i);
363       if (i) OS << ",";
364       if (Idx < 0)
365         OS << "u";
366       else
367         OS << Idx;
368     }
369     OS << ">";
370   } else if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
371     OS << '<' << CSDN->getAPIntValue() << '>';
372   } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
373     if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
374       OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
375     else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
376       OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
377     else {
378       OS << "<APFloat(";
379       CSDN->getValueAPF().bitcastToAPInt().dump();
380       OS << ")>";
381     }
382   } else if (const GlobalAddressSDNode *GADN =
383              dyn_cast<GlobalAddressSDNode>(this)) {
384     int64_t offset = GADN->getOffset();
385     OS << '<';
386     WriteAsOperand(OS, GADN->getGlobal());
387     OS << '>';
388     if (offset > 0)
389       OS << " + " << offset;
390     else
391       OS << " " << offset;
392     if (unsigned int TF = GADN->getTargetFlags())
393       OS << " [TF=" << TF << ']';
394   } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
395     OS << "<" << FIDN->getIndex() << ">";
396   } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
397     OS << "<" << JTDN->getIndex() << ">";
398     if (unsigned int TF = JTDN->getTargetFlags())
399       OS << " [TF=" << TF << ']';
400   } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
401     int offset = CP->getOffset();
402     if (CP->isMachineConstantPoolEntry())
403       OS << "<" << *CP->getMachineCPVal() << ">";
404     else
405       OS << "<" << *CP->getConstVal() << ">";
406     if (offset > 0)
407       OS << " + " << offset;
408     else
409       OS << " " << offset;
410     if (unsigned int TF = CP->getTargetFlags())
411       OS << " [TF=" << TF << ']';
412   } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
413     OS << "<";
414     const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
415     if (LBB)
416       OS << LBB->getName() << " ";
417     OS << (const void*)BBDN->getBasicBlock() << ">";
418   } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
419     OS << ' ' << PrintReg(R->getReg(), G ? G->getTarget().getRegisterInfo() :0);
420   } else if (const ExternalSymbolSDNode *ES =
421              dyn_cast<ExternalSymbolSDNode>(this)) {
422     OS << "'" << ES->getSymbol() << "'";
423     if (unsigned int TF = ES->getTargetFlags())
424       OS << " [TF=" << TF << ']';
425   } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
426     if (M->getValue())
427       OS << "<" << M->getValue() << ">";
428     else
429       OS << "<null>";
430   } else if (const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(this)) {
431     if (MD->getMD())
432       OS << "<" << MD->getMD() << ">";
433     else
434       OS << "<null>";
435   } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
436     OS << ":" << N->getVT().getEVTString();
437   }
438   else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
439     OS << "<" << *LD->getMemOperand();
440
441     bool doExt = true;
442     switch (LD->getExtensionType()) {
443     default: doExt = false; break;
444     case ISD::EXTLOAD:  OS << ", anyext"; break;
445     case ISD::SEXTLOAD: OS << ", sext"; break;
446     case ISD::ZEXTLOAD: OS << ", zext"; break;
447     }
448     if (doExt)
449       OS << " from " << LD->getMemoryVT().getEVTString();
450
451     const char *AM = getIndexedModeName(LD->getAddressingMode());
452     if (*AM)
453       OS << ", " << AM;
454
455     OS << ">";
456   } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
457     OS << "<" << *ST->getMemOperand();
458
459     if (ST->isTruncatingStore())
460       OS << ", trunc to " << ST->getMemoryVT().getEVTString();
461
462     const char *AM = getIndexedModeName(ST->getAddressingMode());
463     if (*AM)
464       OS << ", " << AM;
465
466     OS << ">";
467   } else if (const MemSDNode* M = dyn_cast<MemSDNode>(this)) {
468     OS << "<" << *M->getMemOperand() << ">";
469   } else if (const BlockAddressSDNode *BA =
470                dyn_cast<BlockAddressSDNode>(this)) {
471     OS << "<";
472     WriteAsOperand(OS, BA->getBlockAddress()->getFunction(), false);
473     OS << ", ";
474     WriteAsOperand(OS, BA->getBlockAddress()->getBasicBlock(), false);
475     OS << ">";
476     if (unsigned int TF = BA->getTargetFlags())
477       OS << " [TF=" << TF << ']';
478   }
479
480   if (G)
481     if (unsigned Order = G->GetOrdering(this))
482       OS << " [ORD=" << Order << ']';
483
484   if (getNodeId() != -1)
485     OS << " [ID=" << getNodeId() << ']';
486
487   DebugLoc dl = getDebugLoc();
488   if (G && !dl.isUnknown()) {
489     DIScope
490       Scope(dl.getScope(G->getMachineFunction().getFunction()->getContext()));
491     OS << " dbg:";
492     // Omit the directory, since it's usually long and uninteresting.
493     if (Scope.Verify())
494       OS << Scope.getFilename();
495     else
496       OS << "<unknown>";
497     OS << ':' << dl.getLine();
498     if (dl.getCol() != 0)
499       OS << ':' << dl.getCol();
500   }
501 }
502
503 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
504   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
505     if (N->getOperand(i).getNode()->hasOneUse())
506       DumpNodes(N->getOperand(i).getNode(), indent+2, G);
507     else
508       dbgs() << "\n" << std::string(indent+2, ' ')
509              << (void*)N->getOperand(i).getNode() << ": <multiple use>";
510
511   dbgs() << '\n';
512   dbgs().indent(indent);
513   N->dump(G);
514 }
515
516 void SelectionDAG::dump() const {
517   dbgs() << "SelectionDAG has " << AllNodes.size() << " nodes:";
518
519   for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
520        I != E; ++I) {
521     const SDNode *N = I;
522     if (!N->hasOneUse() && N != getRoot().getNode())
523       DumpNodes(N, 2, this);
524   }
525
526   if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
527   dbgs() << "\n\n";
528 }
529
530 void SDNode::printr(raw_ostream &OS, const SelectionDAG *G) const {
531   print_types(OS, G);
532   print_details(OS, G);
533 }
534
535 typedef SmallPtrSet<const SDNode *, 128> VisitedSDNodeSet;
536 static void DumpNodesr(raw_ostream &OS, const SDNode *N, unsigned indent,
537                        const SelectionDAG *G, VisitedSDNodeSet &once) {
538   if (!once.insert(N))          // If we've been here before, return now.
539     return;
540
541   // Dump the current SDNode, but don't end the line yet.
542   OS.indent(indent);
543   N->printr(OS, G);
544
545   // Having printed this SDNode, walk the children:
546   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
547     const SDNode *child = N->getOperand(i).getNode();
548
549     if (i) OS << ",";
550     OS << " ";
551
552     if (child->getNumOperands() == 0) {
553       // This child has no grandchildren; print it inline right here.
554       child->printr(OS, G);
555       once.insert(child);
556     } else {         // Just the address. FIXME: also print the child's opcode.
557       OS << (void*)child;
558       if (unsigned RN = N->getOperand(i).getResNo())
559         OS << ":" << RN;
560     }
561   }
562
563   OS << "\n";
564
565   // Dump children that have grandchildren on their own line(s).
566   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
567     const SDNode *child = N->getOperand(i).getNode();
568     DumpNodesr(OS, child, indent+2, G, once);
569   }
570 }
571
572 void SDNode::dumpr() const {
573   VisitedSDNodeSet once;
574   DumpNodesr(dbgs(), this, 0, 0, once);
575 }
576
577 void SDNode::dumpr(const SelectionDAG *G) const {
578   VisitedSDNodeSet once;
579   DumpNodesr(dbgs(), this, 0, G, once);
580 }
581
582 static void printrWithDepthHelper(raw_ostream &OS, const SDNode *N,
583                                   const SelectionDAG *G, unsigned depth,
584                                   unsigned indent) {
585   if (depth == 0)
586     return;
587
588   OS.indent(indent);
589
590   N->print(OS, G);
591
592   if (depth < 1)
593     return;
594
595   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
596     // Don't follow chain operands.
597     if (N->getOperand(i).getValueType() == MVT::Other)
598       continue;
599     OS << '\n';
600     printrWithDepthHelper(OS, N->getOperand(i).getNode(), G, depth-1, indent+2);
601   }
602 }
603
604 void SDNode::printrWithDepth(raw_ostream &OS, const SelectionDAG *G,
605                             unsigned depth) const {
606   printrWithDepthHelper(OS, this, G, depth, 0);
607 }
608
609 void SDNode::printrFull(raw_ostream &OS, const SelectionDAG *G) const {
610   // Don't print impossibly deep things.
611   printrWithDepth(OS, G, 10);
612 }
613
614 void SDNode::dumprWithDepth(const SelectionDAG *G, unsigned depth) const {
615   printrWithDepth(dbgs(), G, depth);
616 }
617
618 void SDNode::dumprFull(const SelectionDAG *G) const {
619   // Don't print impossibly deep things.
620   dumprWithDepth(G, 10);
621 }
622
623 void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
624   print_types(OS, G);
625   for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
626     if (i) OS << ", "; else OS << " ";
627     OS << (void*)getOperand(i).getNode();
628     if (unsigned RN = getOperand(i).getResNo())
629       OS << ":" << RN;
630   }
631   print_details(OS, G);
632 }