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