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