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